0

I am trying to populate a bean class (UserBean.java in package lp) when a user submits login credentials on Login.jsp. This bean class is then accessed from a servlet (ConfimUserDetails.java also in package lp).
I cannot get access to the getUid() and getPassword() methods of the bean class from within the Servlet.

Here is all my code:

I have placed the following inside Login.jsp in a bid to map form data to the bean class.

<%@ page import="lp.UserBean" %>

<jsp:useBean id="ubean" class="lp.UserBean" scope="session"/>

<jsp:setProperty name="ubean" property="*"/> 

<form method="post" action="/TestServlet/ConfirmUserDetails">
    <table align="center">
        <tr>
            <th >Enter </th>
            <th>Login Details</th>
        </tr>
        <tr>
            <td >UserID : </td>
            <td ><input type="text" name="uid" size="15" maxlength="8" /></td>
        </tr>
        <tr>
            <td >Password : </td>
            <td ><input type="Password" name="password" size="15" maxlength="20" /></td>
         </tr>
         <tr>
            <td><input type="submit" value="Login" /></td>
        </tr>
    </table>
</form>

The doPost() in ConfirmUserDetails.java (the receiving servlet) looks as follows:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    HttpSession session = request.getSession(); 

    UserBean userBean;
    userBean = (UserBean)session.getAttribute("ubean");

    /*
    if (session.getAttribute("ubean") != null) {
        userBean = (UserBean)session.getAttribute("ubean");
    } else {
        System.out.println("bean did not get picked up-so lets create one");
        userBean = new UserBean();
        session.setAttribute("ubean", userBean);
    }
    */
    UID = userBean.getUid();
    Password=userBean.getPassword();

    PrintWriter out = response.getWriter();
    out.println(UID);

    try{
        cub = new ConfirmUserBean();
        cub= dcm.getUserData(UID,Password);
    }
    catch (Exception npe){
        System.out.println("something went wrong here");
        npe.printStackTrace();
    }

I have imported the bean class above in the servlet (import lp.UserBean;)

The Login.jsp page loads fine. Then when I hit submit, I get a NullPointerException.

Here is the stack trace - I am using Eclipse and Tomcat:

May 24, 2013 7:03:00 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [lp.ConfirmUserDetails] in context with path [/TestServlet] threw exception
java.lang.NullPointerException
    at lp.ConfirmUserDetails.doPost(ConfirmUserDetails.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

In case you wanted to see the top bit of my servlet:

public class ConfirmUserDetails extends HttpServlet {

    private ConfirmUserBean cub;
    private DataConnectionManager dcm;
    private String UID;
    private String Password;
    private UserBean userBean;


    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        dcm = new DataConnectionManager();
        dcm.setDBURL("jdbc:mysql://localhost:3306/loans");
        dcm.setDBUserName("lalinP");
        dcm.setDBPassword("lalin");

        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException e){
            System.out.println("driver could not be loaded");
            e.printStackTrace();
        }
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • what is line no. 70 in `ConfirmUserDetails` class `at lp.ConfirmUserDetails.doPost(ConfirmUserDetails.java:70)`? Also your `UserBean` won't contain any value for `uid` and `password` in your Servlet since the `` will only create the `UserBean` if it is not present in session and will set the values which are actually empty when `Login.jsp` is loaded. – Prakash K May 24 '13 at 10:06
  • Can you paste line code at line no 70?It seems that problem is with UseBean object. – Rohan May 24 '13 at 10:38
  • Line 70 reads " UID = userBean.getUid();" – Lalin Pethiyagoda May 24 '13 at 12:34

1 Answers1

0

You seem to be expecting that <jsp:setProperty> will set all request parameters as bean properties before the servlet is invoked. This is not true. You have to collect and set the request parameters yourself (or to pick a MVC framework which does this all automagically, such as JSF, Spring MVC, etc).

The <jsp:setProperty> is only executed when the particular line in the JSP is executed by the JSP engine. When you're submitting the form to a servlet, that will thus only happen when the servlet forwards to the particular JSP, which is too late.

Get rid of <jsp:useBean> and <jsp:getProperty> altogether. They are useless and only confusing when you're already using a front controller servlet. You need to create and manage the beans yourself in the servlet. Using a MVC framework or Apache Commons BeanUtils may come in handy to reduce boilerplate code.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I see.. so, one is not able to store form data in a bean directly using the setproperty attribute at the time of submitting the form. I was under the impression that one could.Please can you explain when the setproperty attribute could be used on a JSP page.. Also, then, the best way in this situation is to grab the data using request.GetParameter()?... Thank you once again for your help. – Lalin Pethiyagoda May 24 '13 at 12:37
  • Follow the "See also" links for all answers on those side-questions. – BalusC May 24 '13 at 12:38
  • As to the best way, this is IMO using an existing MVC framework. To see a Hello World example of JSF, check the JSF wiki page: http://stackoverflow.com/tags/jsf/info. Spring MVC (and Struts2, Wicket, Play, Echo, etc..etc..) have a similar approach (i.e. just a "view" (the JSP or Facelet file) and a "model" (the Javabean class), you don't need to write the controller servlet, the MVC framework supplies it), but they are all beyond me so I can't point out other examples for them. – BalusC May 24 '13 at 12:40
  • Thank you so much for you help...The life cycle of the setProperty attribute makes perfect sense now. – Lalin Pethiyagoda May 24 '13 at 12:55