【问题标题】:Error 405 when on java servlet在 java servlet 上时出现错误 405
【发布时间】:2013-09-07 10:50:20
【问题描述】:

我已经创建了我的第一个 servlet,但似乎我做的不对。 这是我的 servlet 类:

@SuppressWarnings("serial")
public class Login extends HttpServlet
{

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        out.println("this is a sample");
        out.flush();
        super.doPost(req, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        super.doGet(req, resp);
    }   
}

这是我的 web.xml:

<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">
   <display-name>Login</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>com.hudly.servlets.Login</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servle

t-映射>

我正在尝试浏览这里:localhost:8080/HudlyServer/Login,我得到了这个:

HTTP Status 405 - HTTP method GET is not supported by this URL

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource.

Apache Tomcat/7.0.42

我应该怎么做才能解决这个问题?

【问题讨论】:

  • 不要调用 super.doPost() 和 super.doGet()。它们的存在只是为了被覆盖。

标签: java tomcat servlets web.xml


【解决方案1】:

您可能更喜欢将 GET 和 POST 处理组合到一个方法中。

这可能更好的原因是,典型的表单/或请求处理生命周期在 GET 和 POST 之间有许多共同点;而不同的部分,可以通过检查req.getMethod()是否等于POST来切换。

例如:

abstract public class BaseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        processRequest( req, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    {
        processRequest( req, response);
    }

    abstract protected void processRequest (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}

在表单控制器中,processRequest() 会变成这样:

protected void processRequest (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Form form = createForm();
    bindAndValidate( form, req);
    if (isSubmit()) {       // POST-specific.
        boolean complete = handleSubmit( form, req, resp);
        if (complete)
            return;
    }
    showForm( form, req, resp);
}

如您所见,提交处理(检查它是否完全有效,并做某事)是请求处理中唯一特定于 POST 的部分。

表单应该使用 GET 参数进行初始化,以便您可以重定向到它们,并且不正确/无效的提交应该只是重新显示表单。因此,流程是“BIND,SUBMIT,SHOW”,流程的退出点“在中”。

【讨论】:

    【解决方案2】:

    从您的源代码中删除 super.doPost() /super.doGet() 并编写您的实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多