简体   繁体   English

发布表单时来自Tomcat的意外结果

[英]Unexpected result from tomcat when posting a form

I deploy my simple java web application on Tomcat 7, but something is wrong. 我在Tomcat 7上部署了简单的Java Web应用程序,但是出了点问题。

When I submit the form to the servlet with method POST , the tomcat actually invokes doGet() not doPost() as expected. 当我使用POST方法将表单提交给servlet时,tomcat实际上会按预期调用doGet()而不是doPost() Here is my code: 这是我的代码:

index.html: 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: 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 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 后来,我将doGet()更改为

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 . 尽管我在表单中输入了username和password,但username和password为null

The problem is solved:). 问题已经解决了:)。 And thanks for your comments. 并感谢您的评论。

I use Netbeans to deploy my web application, and it automatically deploys other irrelevant applications which I developed in the past for me. 我使用Netbeans部署我的Web应用程序,它会自动部署我过去为我开发的其他不相关的应用程序。 After I applied Maven command mvn clean to all these irrelevant web projects, it worked as expected. 在将Maven命令mvn clean应用于所有这些不相关的Web项目之后,它按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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