0
votes

I've set up a login form using both jsp and servlet. I used the servlet as the controller and the jsp as the model. Here are the codes:

JSP:

<%@page import="com.horizon.entity.Users"%>
<%@page import="com.horizon.da.UserDA"%>
<%@page import="com.horizon.service.UserService"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome to Horizon Airways</title>

</head>
<body>
<h2 align="center">Welcome to</h2><br>
<h1 align="center">Horizon Airways</h1><br><hr>
<form action="./login.do" method="post">
<table align="center">
<tr>
<th>User name: </th>
<td><input type="text" name="username"></td>
</tr>
<tr>
<th>Pass word: </th>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td align="center">
<input type="submit" value="Login">

</td>
<td align="center"><input type="reset" value="Clear"></td>
</tr>
</table>
</form>
</body>
</html>

Servlet:

package com.horizon.servlet;

import java.io.IOException;

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

import com.horizon.da.UserDA;
import com.horizon.entity.Users;
import com.horizon.service.UserService;



public class ControllerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public ControllerServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        HttpSession session = request.getSession();
            if(path.equals("/login.do")) {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                UserService service = new UserService();
                UserDA da = new UserDA();
                service.setDA(da);
                Users user = da.verifyUser(username, password);
                session.setAttribute("user", user);
                String address = null;
                if(user.getRole() == "Business Manager")
                    address = "./view.jsp";
                else if(user.getRole() == "Counter Assistant")
                    address = "./manage.jsp";
                RequestDispatcher dispatcher = request.getRequestDispatcher(address);
                dispatcher.forward(request, response);
            }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

And here's the error:

Nov 28, 2014 12:00:19 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [ControllerServlet] in context with path [/HorizonAirways] threw exception java.lang.NullPointerException at com.horizon.servlet.ControllerServlet.doGet(ControllerServlet.java:43) at com.horizon.servlet.ControllerServlet.doPost(ControllerServlet.java:49) 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.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 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:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

I appreciate anyone who helps me here.

3

3 Answers

0
votes

Probably your address is empty.

if(user.getRole() == "Business Manager")
    address = "./view.jsp";
else if(user.getRole() == "Counter Assistant")
  address = "./manage.jsp";
else
  System.println.out(address);   //Try to print the address here and see what's going
0
votes

Your problem is here:

String address = null;//<-- is null, if below conditions didn't match
if(user.getRole() == "Business Manager")
    address = "./view.jsp";
else if(user.getRole() == "Counter Assistant")
    address = "./manage.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(address);//<-- might be null
dispatcher.forward(request, response);//<-- raises NPE, if null

P.S. avoid using == use .equals() instead. Kindly verify the user role here.

You can try this:

String address = "./invalid.jsp";//<-- default page, if role not match
if(user.getRole().equals("Business Manager"))
    address = "./view.jsp";
else if(user.getRole().equals("Counter Assistant"))
    address = "./manage.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward(request, response);
0
votes

You are getting NPE at line 43 in doGet() as said in stacktrace. As others said use equals() rather than == , I recommend use equalsIgnoreCase() , it is more reliable when it comes to case Of Stringreturned by user.getRole().

It won't really matter if your user.getRole() returns Business Manager OR business manager OR BUSINESS MANAGER .

Real reason you should not return == is :- It is used to check if two object reference variables [NOT two Objects] refers to the same object or not.

An object reference variable contains the address of the Object it refers to. Object reference variable is not an Object. It is just a mapping to access the real Object.

So, In you case here user.getRole() == "Business Manager" , the "Business Manager" is a String literal and intern() is called on it implicitly which returns reference location of String literal in String Pool/intern Pool & user.getRole() returns a String Object reference variable which points to a String Object in Heap. So, according to == they do not have same Address.

So your check user.getRole() == "Business Manager" fails.

You should use equalsIgnoreCase() or you can call intern() on user.getRole() Like this if(user.getRole().intern() == "Business Manager") OR if("Business Manager" == user.getRole().intern()) .