Saturday, October 9, 2010

JSP Login Example


JSP Login Example

Problem:

            Get input such as username and password from user.

            Check given username as sureshkumar and password as murugan.

Solution:

            Step 1:

                        Create jsp page(Home.jsp)

                        Get username and password from user.

Home.jsp

 

 


< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > JSP Home < /title >
< /head >
< body >
< h1 > Login Example < /h1 >
< form method="post" action=http://localhost:8084/DataBasePro/jsp/validate.jsp >
< table >
< tr >

< td > UserName < /td >
< td > < input type="text" name="uname" > < /td >

< /tr >

< tr >
< td > Password < /td >
< td > < input type="password" name="pwd" > < /td >
< /tr >

< tr >
< td > < input type="submit" value="Login" > < /td >
< td > < input type="reset" value="Clear" > < /td >
< /tr >
< /table >
< /form >
< /body >
< /html >


validate.jsp

< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > JSP Validate Page < /title >
< /head >
< body >
< h1 > Login Example < /h1 >

< %
String username=request.getParameter("uname");
String password=request.getParameter("pwd");

if(username.equals("sureshkumar")&&(password.equals("murugan")))
{
out.println("You Are Valid User");
}
else
{
out.println("You Are Not Valid User");
}
% >
< /body >
< /html >


Explain:



            Using Home.jsp, we will get username and password.



Using validate.jsp, we will check the given username as sureshkumar and password as murugan. If it is true, the You Are Valid User message will display.
Otherwise, the You Are Not Valid User message will display.































Note: you give username as suresh , you will get message You Are Not Valid User.

No comments:

Post a Comment