NullPointerException with RequestDispatcher
Hi all,
I'm developing a login servlet but have a recurring problem with the RequestDispatcher throwing up a NullPointerException. I call the RequestDispatcher using this method:
RequestDispatcher dispatcher =null;
if (dbpass.equals(password))
{
session = request.getSession(true);
session.setAttribute("user",uID);
session.setAttribute("dept",uDept);
dispatcher = getServletContext().getRequestDispatcher("/index.html");
}
else
{
dispatcher = getServletContext().getRequestDispatcher("/loginjsp.jsp");
}
dispatcher.forward(request, response);
The layout of the directory is:
WEBAPP
- index.html
- loginjsp.jsp
|
-WEB-INF
The servlet is mapped as though it were in the root webapp directory. What I've read on here suggests to me the problem lies in the path I'm giving the RequestDispatcher, but I've tried almost all the other possible paths I can think of and still get the exception error.
Any thoughts would be appreciated!
[1408 byte] By [
Jonno1985a] at [2007-11-27 11:50:50]

# 1
Can you check whether loginjsp.jsp is accessible or not? You can check this by using http://{webapp}/loginjsp.jsp.
If this works then please paste stack trace so that I would able to help
# 2
'loginjsp.jsp' is available, it's the page containing the login form which feeds into the servlet - the idea here is that if the password is wrong the user gets redirected back to the original login page - 'loginjsp.jsp'.
Here is the stack trace as requested, hope it helps:
ERROR TP-Processor12 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/wzq06pgu-webapp].[Login2] - Servlet.service() for servlet Login2 threw exception
java.lang.NullPointerException
at LoginServlet.doPost(LoginServlet.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:307)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:385)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:748)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:678)
at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:871)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
# 3
Weird.
How does the servlet mapping look like? What says request.getRequestURI() inside this servlet?
# 4
Here's a copy of the web.xml file with the mapping:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>Login2</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login2</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
As far as I'm aware that should mean that the servlet context is simply the webapp directory where the other pages are held. As for request.getRequestURI(), that method is not invoked anywhere in the servlet.
# 5
<servlet-class>LoginServlet</servlet-class>
Servlets require a package. Put it in a package.
> As for request.getRequestURI(), that method is not invoked anywhere in the
> servlet.
That was not my question :)
# 6
DO the following
request.getRequestDispatcher("/loginjsp.jsp");
It will solve your problem.
API of GenericServlet's getServletContext() says that
Returns a reference to the ServletContext in which this servlet is running. See ServletConfig.getServletContext().
This method is supplied for convenience. It gets the context from the servlet's ServletConfig object.
Check whether servlet config is null or not by calling getServletConfig() .
# 7
> DO the following
> request.getRequestDispatcher("/loginjsp.jsp");
>
> It will solve your problem.
I replaced the lines:
dispatcher = getServletContext().getRequestDispatcher("/index.html");
and
dispatcher = getServletContext().getRequestDispatcher("/loginjsp.jsp");
with:
dispatcher = request.getRequestDispatcher("/index.html");
and
dispatcher = request.getRequestDispatcher("/loginjsp.jsp");
but I'm still getting a NullPointerException error. As I'm no longer calling the getServletContext() method would I need to change the path to the pages I want to load?
# 8
>but I'm still getting a NullPointerException error. As I'm no longer calling the >getServletContext() method would I need to change the path to the pages I >want to load?
You should not get any NPE after changing it to request.getRequestDispatcher(), instead if resource i.e. loginjsp.jsp is not there , you would get PageNot Found 404 error.
to verify, I just created a servlet and in doGet method, I wrote following code
RequestDispatcher dispatcher = request.getRequestDispatcher("/page1.jsp");
dispatcher.forward(request, response);
And when I tried to invoke servlet by http://localhost:8080/classify/ClassiTrendServlet
It gave me 404 error because I dont have page1.jsp in the web directory.
Let me ask you to add an init() method
ServletConfig servletConfig; //instance variable
public void init(ServletConfig config) throws ServletException {
servletConfig = config;
super.init();
}
And in doGet method add following lines
ServletContext context = servletConfig.getServletContext();
System.out.println(context);
String path1 = context.getRealPath("/loginpage.jsp");
System.out.println(path1);
Let me know what is the output.
# 9
This is the output I got:
org.apache.catalina.core.ApplicationContextFacade@5b3ba312
/stuweb3/home/wzq06pgu/webapp/loginjsp.jsp
# 10
Finally got it working! Thanks for the help, it was very much appreciated :)