aspose file tools*
The moose likes JSP and the fly likes JSP Not Reading Arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Not Reading Arraylist" Watch "JSP Not Reading Arraylist" New topic
Author

JSP Not Reading Arraylist

Vishal Kumar
Greenhorn

Joined: Jan 13, 2005
Posts: 19
Hi Friends -

I have a servlet code that passes an arraylist object to a JSP.

package com.test;

import java.util.ArrayList;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import java.io.IOException;

public class LoadArrayList extends HttpServlet{

public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ArrayList al = new ArrayList();
al.add("value 1");
al.add("value 2");
al.add("value 3");
al.add("value 4");

// HttpServletRequest request = null;
// HttpServletResponse response = null;
HttpSession session = request.getSession();
request.setAttribute("productNames", al);
RequestDispatcher dispatcher = request.getRequestDispatcher("BookList.jsp");
dispatcher.forward(request, response);
}
}


However the JSP does not recieve the arraylist and gives NPE.

<%@ page language="java" import="java.util.*" %>

<%
String path=request.getContextPath();
String basePath=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath %>">
<title>Book list page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table>

<%
ArrayList al=(ArrayList)request.getAttribute("productNames");

// Collection collection = (Collection)request.getAttribute("al");

for (Iterator iter = al.iterator(); iter.hasNext();) {

out.println("<tr>");
out.println("<td>" + iter.next() + "</td>");
out.println("</tr>");
}
%>

</table>
</body>
</html>

Please suggest what I am missing. Thanks.
Vishal Kumar
Greenhorn

Joined: Jan 13, 2005
Posts: 19
I would like to not use struts and see that there are similar posting using struts or action classes. Thanks again.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by VISHAL CHITKARA:

ArrayList al=(ArrayList)request.getAttribute("productNames");



change your above code with this then try..



Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Vishal Kumar
Greenhorn

Joined: Jan 13, 2005
Posts: 19
Thanks Saif for the reply. I also have a import on the JSP
<%@ page language="java" import="com.test.*" %>
warning that PACKAGE com.test is never used

1. Do I need to reference the class if the servlet is using dispatcher to call the JSP ?

I have read the FAQ on these and see that there is no typos.

Any leads ?
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

when jsp is called first time its compile and load the .class file in memory.. like a simple java file. first time its found the null value in your request.getAttribute() so we must check the null value first to prevent throwing NPE.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

did you try my last posted code ??
Vishal Kumar
Greenhorn

Joined: Jan 13, 2005
Posts: 19
Thanks Saif for the reply.

I made the change and do not get NPE anymore. However nothing gets displayed (blank screen with no error or content) which means that the arraylist is not getting populated.

Should I have used a constructor() or is doGet() the standard ?

Thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

VISHAL CHITKARA

Please do us all a favor and change your dislay name to use mixed or lower case. Our eyes will thank you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

Have you 100% proven that the following line:

ArrayList al=(ArrayList)request.getAttribute("productNames");

results in an al value of null?

Upon first inspection I see no issues with how you are setting up the scoped variable.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

Sigh. "Vishal", I asked that you lowercase your name, not change it to something invalid.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Vishal Kumar
Greenhorn

Joined: Jan 13, 2005
Posts: 19
Never mind. I got it working. For those interested in this implementation, the best place to implement is JSP and not servlet - unless you need struts or some action.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

"Vishal", My request that you change your display name to adhere to JavaRanch standards was not a suggestion. Valid display names are mandatory for participation on the Ranch. Please change your display name as instructed prior to your next post.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP Not Reading Arraylist