简体   繁体   中英

Error 405 when on java servlet

I have created my first servlet but it seems I didn't do something right. Here is my servlet class:

@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);
    }   
}

here is my 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-mapping>

I am trying to browse here: localhost:8080/HudlyServer/Login and I am getting this :

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

What should I do to fix this?

You may prefer to combine GET & POST handling into a single method.

The reason this might be better, is that typical form/ or request-processing lifecycle has many parts in common between GET & POST; and the parts that are distinct, can be switched by checking whether req.getMethod() equals POST.

For example:

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;
}

In form controllers, processRequest() would then go something like:

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);
}

As you can see, SUBMIT-handling (checking it's fully valid, and doing something) is the only part of request-processing which is specific to POST.

Forms should initialize with GET parameters so you can redirect to them, and incorrect/invalid submits should just reshow the form.. thus, the flow is "BIND, SUBMIT, SHOW" with the exit point from the flow "in the middle".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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