Escolar Documentos
Profissional Documentos
Cultura Documentos
Password Log in
DZone Forums > Community > Languages & Frameworks > Java
Unable to pass arraylist data from servlet to jsp.
Notices
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.
(#1 (permalink))
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.
---------------------------------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()
{
…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......................
<%@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>
<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........
--------------------------------------------------------------------------------
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
(#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");
(#3 (permalink))
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()
{
//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>
<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>
Posting Rules
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
…dzone.com/…/502-unable-pass-arrayli… 5/6
2/2/2011 Unable to pass arraylist data from servl…
…dzone.com/…/502-unable-pass-arrayli… 6/6