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

时间:2016-03-30 08:57:33

标签: java servlets

我开始研究servlet。 代码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);
    }
}

文件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>

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

输入状态报告

消息HTTP方法此URL不支持GET

description请求的资源不允许使用指定的HTTP方法。

Apache Tomcat / 9.0.0.M4

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

2 个答案:

答案 0 :(得分:1)

使用get或Post方法

<强> MainServlet.java

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 doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{        
        PrintWriter out = resp.getWriter();
        out.print("servlet");
    }
}

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">
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

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

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

    </web-app>

网址必须如下: http://localhost:8080/Project name / MainServlet

答案 1 :(得分:0)

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

当你做super.doGet(请求,回复);在你的Servlet的doGet()方法中,你实际上调用了HttpServlet类的doget()。所以放弃超级电话。它不需要。

只需删除这些行:

super.doGet(req, resp);
super.doPost(req, resp);