简体   繁体   English

JSP Servlet不执行doPost方法

[英]JSP servlet not executing doPost method

I have recently started programming in java and have been trying out some JSP development. 我最近开始用Java编程,并一直在尝试一些JSP开发。 I am trying to make a login page which uses the POST method to transfer data to the servlet. 我试图制作一个使用POST方法将数据传输到servlet的登录页面。 Here is my code: 这是我的代码:

<form method="POST" name ="loginForm"  action="userAuth">
            <input type="hidden" name="userAction" value="login">
            Username: <input type="text" name="txtUsername"> <br>
            Password    : <input type="password" name="txtPassword">
            <br><input type="submit"  value="Login">

</form>

The above code is from the initial login page. 上面的代码来自初始登录页面。

The code below is from the userAuth.java file. 下面的代码来自userAuth.java文件。

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
    String userAction = request.getParameter("userAction");
    if (userAction.equals("Login")) {
    String userName = request.getParameter("txtUsername");
    String passWord = request.getParameter("txtPassword");

    if (userName.equals("hello") && passWord.equals("hello")) {
    response.sendRedirect("Homepage.jsp");
    }
    }
}

The problem I have is that when I input the correct username and password, the doPost method is not executed, so none of the redirects take place. 我的问题是,当我输入正确的用户名和密码时,不会执行doPost方法,因此没有任何重定向发生。 Rather only the ProcessRequest method is executed which just displays the initial template to the web browser. 而是仅执行仅将初始模板显示到Web浏览器的ProcessRequest方法。

Thank you in advance. 先感谢您。

PS I am using Apache Tomcat 8.0.27.0 PS我正在使用Apache Tomcat 8.0.27.0

I have solved the issue... 我已经解决了这个问题...

The issue was with the following line 问题在于以下行

<input type="hidden" name="userAction" value="**login**">

and the subsequent processing in the second block: 以及第二个块中的后续处理:

if (userAction.equals("**Login**")) {}

The login value didnt have an uppercase L. 登录值没有大写的L。

Just changed this. 刚刚改变了这一点。

What does the processRequest() method do? processRequest()方法有什么作用? If executes as you say then the server gives a response to the client and the remaining block of code does not execute. 如果按照您说的执行,则服务器将响应客户端,其余代码块将不执行。 Have you tried to run without this function? 您是否尝试过不使用此功能运行?

Hide the process request method. 隐藏流程请求方法。

Like this: 像这样:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //processRequest(request, response);
    String userAction = request.getParameter("userAction");
    if (userAction.equals("Login")) {
        String userName = request.getParameter("txtUsername");
        String passWord = request.getParameter("txtPassword");
    }
}

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

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