MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create a login form with JSP

This article will show you how you may design a simple Login form with JSP. We make use of session for storing session information.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

We will use above these key points to make this login form:

  1. We used Jsp directive i.e. page and defined 2 attributes which are : contentType and pageEncoding
  2. contentType is for defining the type of content used
  3. pageEncoding define the encoding scheme used
  4. We make a really simple html form with 2 fields which are username and password
  5. The important thing is we need to define the name attribute since this will be used to retrieve the values of respective fields in other pages
  6. The value of name attribute for username is “username”
  7. The value of name attribute for password is “password”
  8. We submit the form to LoginCheck.jsp where our credentials gets verified.

Listing 1: Login Form

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Login Page</h1>
        <center>
            <h2>Signup Details</h2>
            <form action="LoginCheck.jsp" method="post">
            <br/>Username:<input type="text" name="username">
            <br/>Password:<input type="password" name="password">
            <br/><input type="submit" value="Submit">
            </form>
        </center>
    </body>
</html>

Now we will code the LoginCheck.jsp

Listing 2: LoginCheck.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
        String username=request.getParameter("username");
        String password=request.getParameter("password");
       
        if((username.equals("anurag") && password.equals("jain")))
            {
            session.setAttribute("username",username);
            response.sendRedirect("Home.jsp");
            }
        else
            response.sendRedirect("Error.jsp");
        %>
    </body>
</html>

The validation was performed in following manner:

  1. We again defined the page directive attribute here
  2. We make use of request.getParameter(“Form name attribute”) to obtain the field values
  3. We obtain the username using request.getParameter("username") and store this in variable name username.
  4. Similarly, we obtain the password using request.getParameter("password") and store this in variable name password.
  5. Now we compare the user obtained user name and password with the correct credentials which is anurag(username) and jain(password). I have compared directly with plain visible username and password, but please do not ue like this, please make use of database for username and password comparison. I have not used that for simplicity.
  6. Now we define a session variable which stores the username value. This session will be valid till the user session expires.
  7. We use response.sendRedirect to call some other page.
  8. If credential are correct we show the home page otherwise we call the error page

Now we will code the home page:

Listing 3: Home.jsp

<%@page contentType="text/html" pageEncoding="UTF-8" errorPage="Error.jsp"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>       
        <br/><br/><br/><br/><br/>
        <center>
            <h2>
            <%
            String a=session.getAttribute("username").toString();
            out.println("Hello  "+a);
            %>
            </h2>
            <br/>
            <br/>
            <br/><br/><br/><br/><br/>
        <a href="logout.jsp">Logout</a>
        </center>

    </body>
</html>

We have included some new things here. We will discuss all

  • In page directive we added a new attribute called errorPage. Its value is set to "Error.jsp". So if there is any error in Home.jsp then the error page will be called. This is an important feature for error handling
  • We use the session attribute to obtain the username and show that on screen
  • If the user is directly accessing the Home.jsp from his browser then we will be redirected to Error page because in Home.jsp when program tries to receive the session attribute and try to convert that in String then a null pointer exception occurs and since we defined the error page so the page is redirected to error page.
  • We add a logout button for logout purpose. This call the logout.jsp
  • We write code for logout.jsp now

    Listing 4: Logout.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <%
    
            session.removeAttribute("username");
            session.removeAttribute("password");
            session.invalidate();
            %>
            <h1>Logout was done successfully.</h1>
            
        </body>
    </html>
    

    Here ,

    1. We used session.removeAttribute to remove both of the attributes. So that when control comes to this page then user session is killed and if he retries to enter home.jsp directly then error message will be shown to him.

    Lastly we define the error page.

    Listing 5: Error.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Some Error has occured,Please try again later...</h1>
        </body>
    </html>
    

    We have shown a error message when user comes to this page.

    We will discuss more about exciting things in coming articles. Hope you like this article. See you soon with a new article



    My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

    What did you think of this post?

    Did you like the post?

    Help us to keep publishing good contents like this.

    SUPPORT US

    funded

    remaining

    [Close]
    To have full access to this post (or download the associated files) you must have MrBool Credits.

      See the prices for this post in Mr.Bool Credits System below:

    Individually � in this case the price for this post is US$ 0,00 (Buy it now)
    in this case you will buy only this video by paying the full price with no discount.

    Package of 10 credits - in this case the price for this post is US$ 0,00
    This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

    Package of 50 credits � in this case the price for this post is US$ 0,00
    This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


    > More info about MrBool Credits
    [Close]
    You must be logged to download.

    Click here to login