userimage
sam

what is the solution to this problem when i am trying to set value in request scope and try to forward the request to another JSP page


package com.sam;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class Registration_servlet_Cities
 */

public class Registration_servlet_Cities extends HttpServlet {

	private static final long serialVersionUID = 1L;

	List cities=null;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	cities = new ArrayList();
	cities.add("Hyderabad");
	cities.add("Chenni");
	cities.add("Bengalore");
	cities.add("Pune");
	cities.add("Mumbai");
	cities.add("Delhi");

	request.setAttribute("cities", cities);
	RequestDispatcher rd =request.getRequestDispatcher("Registration_Custmer.jsp");
	rd.forward(request, response);
	}
}

In JSP trying to render the scopped values by using JSP scriptlet code

output: error message


HTTP Status 500 - An exception occurred processing JSP page /Registration_Custmer.jsp at line 73
--------------------------------------------------------------------------------
type Exception report

message An exception occurred processing JSP page /Registration_Custmer.jsp at line 73
description The server encountered an internal error that prevented it from fulfilling this request.
exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /Registration_Custmer.jsp at line 73


73:                              for(String city :s)
74:                              {                         
75:                                 out.print("
"+city+"
");
76:                                }


Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:580)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause

java.lang.NullPointerException
org.apache.jsp.Registration_005fCustmer_jsp._jspService(Registration_005fCustmer_jsp.java:159)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

userimage

You should put the citis names in session scope not in request scope as you doing


rd.forward(request, response);

So it will not hold your request scoped data or else you can use include methode of RequestDispacher.

Answer is