Java 服务小程序 java.lang.NullPointerException 在 doGet()
Java Servlet java.lang.NullPointerException at doGet()
我有一个 HTTP 500 - java servlet 的内部服务器错误。而且我没有在论坛上为我的案例找到解决方案。我使用的是 IntelliJ Ultimate 2020.3 和 Apache Tomcat Server 9.0.43。这是我的项目文件结构和代码。我一直在寻找解决方案 3 个小时了。拜托,我需要帮助。
servlet class
package com.sdzee.servlets;
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 java.io.IOException;
@WebServlet(name="Test",value="/test2")
public class Test extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.getServletContext().getRequestDispatcher("/../../main/webapp/test.jsp").forward(req,resp);
}
}
test.jsp
文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body>
<p>JSP test</p>
</body>
</html>
web.xml
中的 servlet 定义
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.sdzee.servlets.Test</servlet-class>
<init-param>
<param-name>Auteur</param-name>
<param-value>Moi</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test2</url-pattern>
</servlet-mapping>
</web-app>
结果:
我认为 getRequestDispatcher("/../../main/webapp/test.jsp") 应该是 getRequestDispatcher("/test.jsp")
我有一个 HTTP 500 - java servlet 的内部服务器错误。而且我没有在论坛上为我的案例找到解决方案。我使用的是 IntelliJ Ultimate 2020.3 和 Apache Tomcat Server 9.0.43。这是我的项目文件结构和代码。我一直在寻找解决方案 3 个小时了。拜托,我需要帮助。
servlet class
package com.sdzee.servlets;
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 java.io.IOException;
@WebServlet(name="Test",value="/test2")
public class Test extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.getServletContext().getRequestDispatcher("/../../main/webapp/test.jsp").forward(req,resp);
}
}
test.jsp
文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body>
<p>JSP test</p>
</body>
</html>
web.xml
中的 servlet 定义<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.sdzee.servlets.Test</servlet-class>
<init-param>
<param-name>Auteur</param-name>
<param-value>Moi</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test2</url-pattern>
</servlet-mapping>
</web-app>
结果:
我认为 getRequestDispatcher("/../../main/webapp/test.jsp") 应该是 getRequestDispatcher("/test.jsp")