简单 Servlet HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET

Але*_*ндр 0 java servlets

我开始研究servlet。代码服务程序:

package arver;

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

/**
 * Created by 35717 on 30.03.2016.
 */
public class MainServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        PrintWriter out = resp.getWriter();
        out.print("servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
Run Code Online (Sandbox Code Playgroud)

文件 web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">

        <servlet>
            <servlet-name>MainServlet</servlet-name>
            <servlet-class>arver.MainServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>MainServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    </web-app>
Run Code Online (Sandbox Code Playgroud)

服务器响应:HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET

类型状态报告

此 URL 不支持消息 HTTP 方法 GET

描述 请求的资源不允许指定的 HTTP 方法。

Apache Tomcat/9.0.0.M4

为什么我收到 HTTP 状态 405 - 此程序中的此 URL 错误不支持 HTTP 方法 GET。

Sac*_*rse 5

我们扩展HttpServlet@Override doPost但在我们的实现中我们不调用它的super,因为调用super 会给出这个消息。

当你super.doGet(request, response);在你的servlet的doGet()方法,你实际上调用doGet()了的HttpServlet类。所以放弃超级电话。不需要。

只需删除这些行:

super.doGet(req, resp);
super.doPost(req, resp);
Run Code Online (Sandbox Code Playgroud)