繁体   English   中英

Java Servlet java.lang.NullPointerException 在 doGet()

[英]Java Servlet java.lang.NullPointerException at doGet()

我有一个 HTTP 500 - java servlet 的内部服务器错误。 而且我没有在论坛上为我的案例找到解决方案。 我正在使用 IntelliJ Ultimate 2020.3 和 Apache Tomcat 服务器 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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM