0

I deploy my simple java web application on Tomcat 7, but something is wrong.

When I submit the form to the servlet with method POST, the tomcat actually invokes doGet() not doPost() as expected. Here is my code:

index.html:

<html>
    <body>
        <form action="http://localhost:8084/authentication" method="post">
            <input type="text" name="username">
            <input type="password" name="password">
            <input type="submit">
        </form>
    </body>
</html>

AuthenticationServlet.java:

public class AuthenticationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
        response.sendError(405, "Method GET is not allowed");
    }

    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
        // unreachable
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username == null || password == null) {
            response.sendError(400, "username and password are required");
            return;
        }

        ...
    }
}

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">
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>foo.bar.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <servlet>
        <servlet-name>authentication</servlet-name>
        <servlet-class>foo.bar.AuthenticationServlet</servlet-class>
    </servlet>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <servlet-name>authentication</servlet-name>
    </filter-mapping>
    <servlet-mapping>
        <servlet-name>authentication</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Later, I changed the doGet() into

protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
       throws ServletException, IOException {
   doPost(request, response);
}

Though I've type username and password in the form, the username and password is null.

4

1 に答える 1

0

問題は解決しました:)。そして、あなたのコメントに感謝します。

Netbeansを使用してWebアプリケーションをデプロイすると、過去に開発した他の無関係なアプリケーションが自動的にデプロイされます。mvn cleanこれらの無関係なWebプロジェクトすべてにMavenコマンドを適用した後、期待どおりに機能しました。

于 2013-03-04T09:51:40.673 に答える