0

最初のサーブレットを作成しましたが、何かが正しくなかったようです。ここに私のサーブレットクラスがあります:

@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

これを修正するにはどうすればよいですか?

4

2 に答える 2

2

GET と POST の処理を​​ 1 つのメソッドに結合することをお勧めします。

これがより良い理由は、典型的なフォーム/またはリクエスト処理のライフサイクルには、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);
}

ご覧のとおり、SUBMIT 処理 (完全に有効であることを確認し、何かを実行すること) は、POST に固有の要求処理の唯一の部分です。

フォームは GET パラメーターで初期化して、リダイレクトできるようにする必要があります。また、正しくない/無効な送信はフォームを再表示する必要があります。したがって、フローは「BIND、SUBMIT、SHOW」であり、フローの出口ポイントは「中間」にあります。

于 2013-09-07T11:05:08.347 に答える
1

ソース コードからsuper.doPost()/を削除し、実装を記述します。super.doGet()

于 2013-09-07T11:01:05.443 に答える