简体   繁体   中英

Java Servlet java.lang.NullPointerException at doGet()

I have an HTTP 500 - Internal server error for java servlet. And I didn't found a solution on forums for my case. I'm using IntelliJ Ultimate 2020.3 and Apache Tomcat Server 9.0.43. Here's my projet files structure and my code. I have been looking for a solution for 3 hours now. Please,I need help. 文件结构

The 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);
    }

}

the test.jsp file

<%@ 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>

the servlet definition in web.xml

<?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>

and the results: 在此处输入图像描述

I think getRequestDispatcher("/../../main/webapp/test.jsp") should be getRequestDispatcher("/test.jsp")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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