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.