Ich habe viele ähnliche Fragen gelesen und alle vorgeschlagenen Methoden zur Behebung des Problems versucht, aber es ist erfolglos. Jetzt nicht habe ich keine Ideen wirklich, was damit zu tun (localhost: 8080/testWebApp/home):Ich kann keinen RequestDispatcher bekommen
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)
Ich verwende Maven 3.3.9 und tomcat7 7.0.70-1. Mein Projektverzeichnis hat die folgende Hierarchie:
webApp/
pom.xml
src/
main/
java/
test/web/servlet/
TestServlet.java
webapp/
index.html
WEB-INF/
views/
home.jsp
web.xml
Hier ist der 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);
}
}
Hier 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>
Einige nützliche Zeilen von pom.xm l:
<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>
Vielen Dank für Ihre Hilfe!
Wenn Sie nur über die 'NullPointerException' fragen, diese ist ein [Duplikat] (/ questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). Was Sie fragen sollten, ist, warum der 'RequestDispatcher' null ist. – EJP
Danke für die Korrektur! – user3856196