• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP Not Reading Arraylist

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to not use struts and see that there are similar posting using struts or action classes. Thanks again.
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by VISHAL CHITKARA:

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



change your above code with this then try..

 
Vishal Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you try my last posted code ??
 
Vishal Kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
VISHAL CHITKARA

Please do us all a favor and change your dislay name to use mixed or lower case. Our eyes will thank you.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic