2

I've created a jsp login page form and I've put my logic inside the doPost(). But seems it is not working. Even the System.out.println statement result in doPost() method is not shown in the console. JSP page::(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 class (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 file

<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>
user4221591
  • 2,084
  • 7
  • 34
  • 68

1 Answers1

2
 request.getParameter("txtUserName");

searches for the name = "txtUserName" attribute in input tag and similarly for password field as well, which is not present, modify the JSP code as below and it should work:

<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>
Kukatla
  • 21
  • 3