【问题标题】:doPost method of servlet class not giving required resultservlet类的doPost方法没有给出所需的结果
【发布时间】:2017-05-28 03:36:36
【问题描述】:

我创建了一个 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>

【问题讨论】:

  • 你为什么要从 doPost 调用 doGet?
  • @MichaelMarkidis 对不起,我不明白你的评论。请你解释一下。
  • servlet 中doPost 方法的第一行是doGet(request, response); 为什么要从post 方法中调用get 方法?
  • 另外,如果您尝试直接从浏览器访问您的 servlet,会发生什么情况。?
  • doPost() 中调用doGet(request, response); 毫无意义。此外,您的表单中没有定义名称属性。此post 可能对您入门有所帮助。

标签: java jsp servlets


【解决方案1】:
 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>

【讨论】:

    猜你喜欢
    • 2015-03-04
    • 2013-10-14
    • 1970-01-01
    • 2020-04-04
    • 2014-04-14
    • 2011-08-18
    • 2012-02-27
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多