3

I've read lots of similar questions and tryed all suggested methods for fixing the problem, however it's unsuccessfully. Now I really don't have any ideas what to do with this (localhost:8080/testWebApp/home):

HTTP Status 500 - ... exception java.lang.NullPointerException test.web.servlet.TestServlet.doGet(TestServlet.java:37) javax.servlet.http.HttpServlet.service(HttpServlet.java:624) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

I use maven 3.3.9 and tomcat7 7.0.70-1. My project directory has the following hierarchy:

webApp/ pom.xml src/ main/ java/ test/web/servlet/ TestServlet.java webapp/ index.html WEB-INF/ views/ home.jsp web.xml 

Here is the TestServlet code:

package test.web.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletOutputStream; @WebServlet(urlPatterns = { "/home"}) public class TestServlet extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { super.init(config); } public TestServlet() { super(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/views/home.jsp"); //The following attempts are unsuccessful too: //RequestDispatcher dispatcher = request.getRequestDispatcher("/home.jsp"); //RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/views/home.jsp"); dispatcher.forward(request, response); // error (37 line) } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } 

Here is web.xml code:

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_4.dtd"> <web-app> <welcome-file-list> <welcome-file>home</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> 

home.jsp code:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Home Page</title> </head> <body> <%-- <jsp:include page="main.jsp"></jsp:include> --%> <h3>Home</h3> <br><br> <b>You have the following options:</b> <ul> <li>Login</li> </ul> </body> </html> 

Some useful lines from pom.xml:

 <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <server>tomcat-7.0.70-1</server> <url>http://localhost:8080/manager/text</url> <path>/testWebApp</path> </configuration> </plugin> 

Thank you for help!

2
  • 1
    If you're just asking about the NullPointerException, this is a duplicate. What you should be asking is why the RequestDispatcher is null. Commented Sep 16, 2016 at 0:43
  • Thanks for the correction! Commented Sep 16, 2016 at 7:56

3 Answers 3

3

You cannot get a RequestDispatcher for arbitrary files in your application. The argument to getRequestDispatcher() must be a valid URL path for your application, not a relative file system Location. This is why the dispatcher is null.

If you want to have your JSPs residing under WEB-INF (which is a good idea), then you must create appropriate <servlet-mapping> elements for them.

Sign up to request clarification or add additional context in comments.

Comments

0

After deeper investigation of servlet documentation (especially mapping topic) I made some modifications of web.xml and TestServlet.java. It's important that:

The WEB-INF directory is not part of the public document tree of the application. No file contained in the WEB-INF directory can be served directly to a client by the container. In my case views folder is inside WEB-INF (for user access limitation from the outside).

So, it's need to add the following lines (web.xml):

... <servlet> <servlet-name>home</servlet-name> <jsp-file>/WEB-INF/views/home.jsp</jsp-file> <init-param> <param-name>home</param-name> <param-value>Home Page</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>home</servlet-name> <url-pattern>/homepg</url-pattern> </servlet-mapping> ... 

And corrected TestServlet.java:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/homepg"); 

P.S. Thanks gsl for pointing in the proper direction!

1 Comment

Yeah, thx! :-) One more: Have a look at ServletContext.getNamedDispatcher(name): This one gives a dispatcher based on the servlet name. You get the servlet by refering to its name "home"; then there is no need to defined a servlet-mapping, which is not needed anyway since you do not really want to create a URL space entry for it.
0

You are dispatching the request to /WEB-INF/views/home.jsp url, but in your WEB-INF/views folder you have Home.jsp.

Just change the file name from Home.jsp to home.jsp and re-deploy the application, it should work fine.

Output

Note: You are dispatching the request to a valid path i.e, /WEB-INF/views/home.jsp

2 Comments

Hm, when I got the error the first thing I tryed - was checking all the paths. Also in my case it was not the problem and I had views/home.jsp, not views/Home.jsp. Anyway, thank you for answer!
Sorry, it's somthing like a capital letter at the beginning of the line and then it really looks like a mistake. I'll edit this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.