Issue
I'm trying to set up a very basic JEE project with Eclipse+Tomcat using a single servlet and JSP. However I'm getting a HTTP 500 Internal Server Error when trying to access its URL http://localhost:8080/test/toto
. The exception message being displayed is:
Cannot invoke "javax.servlet.RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)" because the return value of "javax.servlet.ServletContext.getRequestDispatcher(String)" is null.
Here is my Test servlet code:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
this.getServletContext().getRequestDispatcher( "/WEB-INF/test.jsp" ).forward( request, response );
}
}
The test.jsp file should just display a message : "Generated by a JSP"
Here's the content of web.xml
for reference :
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.sdzee.servlets.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/toto</url-pattern>
</servlet-mapping>
</web-app>
The corresponding Tomcat log reads :
0:0:0:0:0:0:0:1 - - [10/Dec/2020:14:29:47 +0100] "GET /test/toto HTTP/1.1" 500 1606
This is the project arborescence
Edit: Other things I recently tried: Reinstall Eclipse+Tomcat on another pc and start again, move test.jsp directly to WebContent and access http://localhost:8080/test/test.jsp
: results in a HTTP 404 ressource not found error.
I've just noticed that before I create my servlet I can access whatever file I create under WebContent at http://localhost:8080/test/whatever.jsp
but I can't do that anymore once I add the servlet and establish the mapping in web.xml
Solution
I was editing the web.xml
file of the Tomcat server and not the one of the project, that's why nothing was working as expected.
Answered By - deque
Answer Checked By - Mildred Charles (JavaFixing Admin)