0

我有以下课程...

package org.me.test
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/servlet")
public class TestServlet extends HttpServlet {
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    resp.setContentType("text/plain");
    resp.getWriter().write("Test Servlet");
    super.doGet(req, resp);
}
}

和以下 Web.xml...

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
  version="3.0"> 
<servlet>
    <display-name>srv</display-name>
    <servlet-name>srv</servlet-name>
    <servlet-class>
                 org.me.test.TestServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>srv</servlet-name>
    <url-pattern>/srv/*</url-pattern>
</servlet-mapping>
</web-app>

但我得到...

错误 405:此 URL 不支持 HTTP 方法 GET

4

1 回答 1

2

您收到 405 错误是因为您super.doGet()doGet(). 尝试删除该行。

doGet()基类中的默认实现HttpServlet返回 405 错误。要支持 GET 操作,您必须在doGet() 调用父实现的情况下进行覆盖,否则它会返回与没有覆盖时相同的错误。

于 2013-09-26T03:28:06.230 回答