简体   繁体   中英

Pass value servlet to jsp

I want to pass value servlet to jsp. set value in servlet and use in jsp page

I getting NullPointerException below is my code

jsp

<body>
<%
  String message = (String) request.getAttribute("message");
  out.println("Servlet communicated message to JSP: "+ message);

  Vector vecObj = (Vector) request.getAttribute("vecBean");
  out.println("Servlet to JSP communication of an object: "+vecObj.get(0));
%>
</body>

java

public class ServletToJSP extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //communicating a simple String message.
        String message = "Example source code of Servlet to JSP communication.";
        request.setAttribute("message", message);

        //communicating a Vector object
        Vector vecObj = new Vector();
        vecObj.add("Servlet to JSP communicating an object");
        request.setAttribute("vecBean",vecObj);

        //Servlet JSP communication
        RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("javaPapers.jsp");
        reqDispatcher.forward(request,response);
      }
    }

Error Log I didnt do any chnages in web.xml I didnt do any chnages in web.xml

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

18:   out.println("Servlet communicated message to JSP: "+ message);
19:  
20:   Vector vecObj = (Vector) request.getAttribute("vecBean");
21:   out.println("Servlet to JSP communication of an object: "+vecObj.get(0));
22: %>
23: </body>
24: </html>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.NullPointerException
    org.apache.jsp.javaPapers_jsp._jspService(javaPapers_jsp.java:83)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.

You might be getting NullPointerException because " reqDispatcher.forward(request,response);" creates a new request altogether and vecObj is becoming null because it does not exist in the request object. Instead of using reqDispatcher.forward use response.redirect .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM