Você está na página 1de 6

2/2/2011 Unable to pass arraylist data from servl…

User Name User Name Remember Me?

Password Log in

FAQ Community Calendar Today's Posts Search

DZone Forums > Community > Languages & Frameworks > Java
Unable to pass arraylist data from servlet to jsp.

Notices

Welcome to the DZone forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free
community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other
special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

LinkBack Thread Tools Search this Thread Display Modes

(#1 (permalink))

Unable to pass arraylist data from servlet to jsp. - 07-14-2008, 07:14 AM


vish_1x1
Member
Hello Everyone,
Posts: 2
Thanks: 0 I am a new java user and i have a problem where i have made a servlet "Conn.java".
Thanked 0 Times in 0 Posts In which i have established a connection and i m retrieving data from database using dsn.
Join Date: Jul 2008
This data i store in a array list called row setArray.
when i run just the servlet with " out.println("<h1>name======" + row setArray + "</h1>"); "
statement in it; it displays all the records in the arraylist on the explorer.

but when i uncomment the above line and try to pass the array list data to my index.jsp then i am
unable to do so.
I get null pointer exception error.

below is my servlet code and follw ed by index.jsp code.

---------------------------------Conn.java--------------
package IRIS;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.String;
import java.sql.*;
import java.util.*;
import javax.servlet.*;

/**
*
* @author vishal
* @version
*/
public class Conn extends HttpServlet {

public Conn()
{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throw s ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ArrayList rowArray = new ArrayList();
ArrayList rowsetArray = new ArrayList();
Connection con;
PreparedStatement ps;
Statement stmt=null;
ResultSet rs;
String name="";

…dzone.com/…/502-unable-pass-arrayli… 1/6
2/2/2011 Unable to pass arraylist data from servl…
String colName="";
ResultSet rs1=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connection Estabished");
con=DriverManager.getConnection("Jdbc:Odbc:dsn_adm in","sa","sa");
System.out.println("Connection Estabished1");
stmt=con.createStatement();
//ResultSetMetaData rsmd=null;
rs1=stmt.executeQuery("select * from employee");

while(rs1.next())
{
row Array.clear();
String str = rs1.getString("FirstName");
row Array.add(str);
row setArray.add(rowArray.clone());

}
request.setAttribute("myArrayList",row setArray);
RequestDispatcher requestDispatcher=getServletContext().getRequestDi spatcher("/Prism/w eb/index.jsp");
requestDispatcher.forw ard(request,response);

}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();

}
//out.println("<h1>name======" + row setArray + "</h1>");

//return name;
// out.close();
}

note i have not mentioned the httpservlet methods here but they are there in my code.
i am using netbeans 5.0......................

-------------------------------------------Code for index.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="IRIS.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page session="true"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>

<html>
<head>
<**** **********="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h1></h1>

<!--/ <img src="Images/Logo_Final Splash.gif" w idth="450" height="200" alt="Logo_Final Splash"/>


-->

<table border="1">
<tr><td><B>Emp Names</B></td></tr>

<%
ArrayList pageArray = (ArrayList) request.getAttribute("myArrayList");
String myString="";
if(pageArray.isEmpty()==false)
{
for(int i = 0; i < pageArray.size(); i++)
{
myString = (String) pageArray.get(i);
}
}
else
{
System.out.println("Array is empty");
}
%>

…dzone.com/…/502-unable-pass-arrayli… 2/6
2/2/2011 Unable to pass arraylist data from servl…
<tr><td><%=myString%></td></tr>

</table>

</body>
</html>
---------------------------------below is the error I get........

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this reque st.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:370)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:362)

root cause

java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(index_jsp.jav a:81)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:362)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.

--------------------------------------------------------------------------------

Apache Tomcat/5.5.9

-----------Please let me know w hat is wrong.plsssssssssssss

(#2 (permalink))

07-14-2008, 04:28 PM
deuso
Member
Actually, attributes are passed among pages or servlets by the session object.
Posts: 1 this line has problem~~~~~ request.setAttribute("myArrayList",row setArray);
Thanks: 0 try to obtain a session object by calling the getSession() method in request to set the attribute..
Thanked 0 Times in 0 Posts request.getSession().setAttribute("myArrayList",ro wsetArray);
Join Date: Jul 2008
Also, the code in JSP should changes as well:
ArrayList pageArray = (ArrayList) session.getAttribute("myArrayList");

Last edited by deuso; 07-14-2008 at 04:43 PM.

(#3 (permalink))

Unable to pass arraylist data from servlet to jsp. - 07-15-2008, 08:17 AM


vish_1x1
Member
This is the modified code. i get out put in this servlet on explorer.
Posts: 2 but when i expect the same in jsp...i get null values.
Thanks: 0
Thanked 0 Times in 0 Posts the line out.println(pageArray.size()); on jsp page gives 1 as size.
Join Date: Jul 2008 which is not the case in the servlet class, there is shows size as 110
which is the true value.

below is my servlet code follow ed by index.jsp code.


-----------

package IRIS;

…dzone.com/…/502-unable-pass-arrayli… 3/6
2/2/2011 Unable to pass arraylist data from servl…

import java.io.*;
import java.net.*;

import javax.servlet.http.*;
import java.lang.String;
import java.sql.*;
import java.util.*;
import javax.servlet.*;

/**
*
* @author vishal
* @version
*/
public class Conn extends HttpServlet {

public Conn()
{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throw s ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ArrayList rowArray = new ArrayList();
ArrayList rowsetArray = new ArrayList();
Connection con;
PreparedStatement ps;
Statement stmt=null;
ResultSet rs;
String name="";
String colName="";
ResultSet rs1=null;
try
{
String str[]=new String[200];
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connection Estabished");
con=DriverManager.getConnection("Jdbc:Odbc:dsn_adm in","sa","sa");
System.out.println("Connection Estabished1");
stmt=con.createStatement();
rs1=stmt.executeQuery("select * from employee");
System.out.println("<tr><td>name</td></tr>");
int n =rs1.getRow ();
while(rs1.next())
{
str[n] = rs1.getString("FirstName");
row Array.add(str[n]);
//out.println("<tr><td><br></br>" + row Array + "</td></tr>");
n++;
}
for (int i=0;i<rowArray.size();i++)
{
//out.println("<tr><td><br>name======" + row Array.get(i) + "</td></tr>");
request.getSession().setAttribute("myArrayList",ro wArray);
RequestDispatcher requestDispatcher=getServletContext().getRequestDi spatcher("/index.jsp");
requestDispatcher.forw ard(request,response);
}
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();

//return name;
// out.close();
}

}
----------------------------

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="IRIS.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page session="true"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>

…dzone.com/…/502-unable-pass-arrayli… 4/6
2/2/2011 Unable to pass arraylist data from servl…

<html>
<head>
<**** **********="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h1></h1>

<!--/ <img src="Images/Logo_Final Splash.gif" w idth="450" height="200" alt="Logo_Final Splash"/>


-->

<table border="1">
<tr><td><B>Emp Names</B></td></tr>
<%
try
{
String myString ="";
ArrayList pageArray = new ArrayList();
pageArray.add(request.getSession().getAttribute("m yArrayList"));
//pageArray.add(request.getAttribute("myArrayList")) ;
for (int i=0;i<pageArray.size();i++)
{
out.println(pageArray.get(i));
}
out.println(pageArray.size());
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
%>
</table>

</body>
</html>

« Previous Thread | Next Thread »

Posting Rules

You may not post new threads


You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Forum Rules

Similar Threads
Thread Thread Starter Forum Replies Last Post

Pass events through the layers of a JLayeredPane lndeveloper Java 0 07-18-2008 06:17 AM

javax.servlet.ServletException: Servlet.init() for servlet jsp


ganeshduke Java 0 07-01-2008 01:30 AM
threw exception

javax.servlet.ServletException: Servlet.init() for servlet jsp


ganeshduke Java 0 07-01-2008 01:29 AM
threw exception

How download files with servlet? sreenivas.angadi Java 0 05-07-2008 06:28 AM

code to monitor new files in folder with file name in the


sahuabinash Java 0 04-04-2008 08:26 AM
arraylist:

C opyright 1997-2009, DZone, Inc.


vBulletin Skin developed by: vBStyles.com

…dzone.com/…/502-unable-pass-arrayli… 5/6
2/2/2011 Unable to pass arraylist data from servl…

DZone.com - Archiv e - Top

…dzone.com/…/502-unable-pass-arrayli… 6/6

Você também pode gostar