servlet类的doPost方法没有给出所需的结果

时间:2017-05-28 03:36:37

标签: java jsp servlets

我已经创建了一个jsp登录页面表单,并且我将我的逻辑放在了doPost()中。但似乎它不起作用。  甚至控制台中也不会显示doPost()方法中的System.out.println语句结果。  JSP页面::( LogIn.jsp)

<body>
    <form method="post" action="LogIn">
        <table>
            <tr>
                <td colspan=2 align="center"
                    style="font-weight: bold; font-size: 20pt;" align="center"><b>User
                        Login Form</b></td>
            </tr>
            <tr>
                <td colspan=2></td>
            </tr>
            <tr>
                <td>User Name:</td>
                <td><input type="text" id="txtUserName" /></td>
            </tr>

            <tr>
                <td>Password:</td>
                <td><input type="password" id="txtpassword" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" id="btnSubmit" value="Submit" /></td>
            </tr>
        </table>
    </form>

</body>

Servlet类(LogIn.java)

@WebServlet("/LogIn")
public class LogIn extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LogIn() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());

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

        String UserName = request.getParameter("txtUserName");
        String Password = request.getParameter("txtpassword");

        System.out.println(UserName);
        System.out.println(Password);

        if (UserName == "Servlet" && Password == "admin") {         
            response.sendRedirect("Home.jsp");

        Enumeration paramNames = request.getParameterNames();

        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();           
            String[] paramValues = request.getParameterValues(paramName);

            // Read single valued data
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                {
                    System.out.println("no values");
                }
                else
                    System.out.println(paramValue);
            } else {
                // Read multiple valued data
                System.out.println(".....");

                for (int i = 0; i < paramValues.length; i++) {
                    System.out.println( paramValues[i]);
                }
            }
        }
    }
}

Web.xml文件

<servlet>
<servlet-name>LogIn</servlet-name>
<servlet-class>LogIn</servlet-class>    
</servlet> 
<servlet-mapping>
<servlet-name>LogIn</servlet-name> 
<url-pattern>/LogIn</url-pattern>  
</servlet-mapping>

1 个答案:

答案 0 :(得分:2)

 request.getParameter("txtUserName");

在输入标记中搜索 name =“txtUserName”属性,对于密码字段也是如此,这是不存在的,修改JSP代码如下,它应该工作:

<body>
    <form method="post" action="LogIn">
        <table>
            <tr>
                <td colspan=2 align="center"
                    style="font-weight: bold; font-size: 20pt;" align="center"><b>User
                        Login Form</b></td>
            </tr>
            <tr>
                <td colspan=2></td>
            </tr>
            <tr>
                <td>User Name:</td>
                <td><input type="text" id="txtUserName" name="txtUserName"/></td>
            </tr>

            <tr>
                <td>Password:</td>
                <td><input type="password" id="txtpassword" name="txtpassword" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" id="btnSubmit" value="Submit" /></td>
            </tr>
        </table>
    </form>

</body>