简体   繁体   English

Spring - 无法映射servlet。 它返回404 - Not Found

[英]Spring - unable to map the servlet. It returns 404 - Not Found

I am trying to make a simple servlet, but I am getting the error status of 404 Not Found. 我正在尝试创建一个简单的servlet,但我得到404 Not Found的错误状态。

This is my servlet code 这是我的servlet代码

package servlets;

import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(name = "ServletExample", urlPatterns = "/theServlet")
public class ServletExample extends javax.servlet.http.HttpServlet {
    private String message;

    @Override
    public void init() throws ServletException {
        message = "Hello from the servlet";
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //set response type
        response.setContentType("text/html");

        //actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println("<h1>" + message + "</h1>");
    }

    @Override
    public void destroy() {
        //do nothing
    }

}

and this is my web.xml file: 这是我的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>ServletExample</servlet-name>
        <servlet-class>servlets.ServletExample</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletExample</servlet-name>
        <url-pattern>/Example</url-pattern>
    </servlet-mapping>
</web-app>

The url's I tried: 网址我尝试过:

http://localhost:8080/Project/Example

http://localhost:8080/Example

http://localhost:8080/theServlet

http://localhost:8080/Project/theServlet

尝试删除web.xml中关于sevlet mapping ..和.. balises的行,因为你已经在类中定义了这个配置,你可以通过http:// localhost:8080 / Project / theServlet调用servlet。

Why you are using both annotations and web.xml. 为什么要同时使用注释和web.xml。 A “best practice” is to try to use one or the other. “最佳实践”是尝试使用其中一种。 You can entirely drop web.xml file if you choose to rely on annotations for all of your Servlet configurations. 如果您选择依赖所有Servlet配置的注释,则可以完全删除web.xml文件。 But I still encourage you to keep that file, or at least have one like shown in the example below: 但我仍然鼓励你保留该文件,或至少有一个如下例所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" 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">
    <display-name>Servlet with Annotations Application</display-name>
</web-app>

And try calling http://localhost:8080/Project/theServlet 并尝试调用http:// localhost:8080 / Project / theServlet

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

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