2
votes

I passed bean object from a servlet to a jsp file and want to use bean value as follows but it throws runtime error. What am I doing wrong?

 <%@ page import = "beans.UserInfo" %>
 <%
   UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
   String userName = userInfo.getUserName();
 %>

Exeption is as follows:

exception

org.apache.jasper.JasperException: java.lang.NullPointerException org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:491) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.NullPointerException org.apache.jsp.Login_jsp._jspService(Login_jsp.java:60) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

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

1
You should include exception stack trace.kv-prajapati

1 Answers

2
votes

Your code in doPost or doGet to should be as follows where you're constructing beans.UserInfo and issue forward method of Dispatcher.

 bean.UserInfo info=new bean.UserInfo();
 //set the bean properties
 session.setAttribute("userInfo",info);
 request.getRequestDispatcher("/your_page.jsp")
    .forward(request,response);