Login and Logout Using JSP in Java

Introduction

This article explains how to create a Login and Logout in JSP. The NetBeans IDE is used to create this application.

Login and Logout in JSP

For creating this app we need to create the following pages.

  • home.html
  • process.jsp
  • logout.html
  • logoutprocess.jsp
  • emp.sql

1. home.html

This file is used to get data from the user and then send the user data to the "process.jsp" page.

2. process.jsp

This page is used to validate the user, in other words he/she is a registered user or not. For validation we use a database table "emp", using this table we check whether user records are in the database table or not. If records are not found then a error message is generated and the page is redirected to the home ("home.jsp") page, else if a record is matched then we either can redirect them to another page or we can simply display a confirmation message that "you are successfully logged in". In this article we simply display a confirmation message or display after record matching.

We also include a new page named "logout.html", this page is included when the user data is matched. This page provides a "logout" button.

3. logout.html

This page is developed to provide a "logout" button. When the user clicks on this button a request is submitted to the "logoutprocess.jsp" page and they are logged out from our website.

4. logoutprocess.jsp

In this page we invalidate the user session. After session invalidation a message is passed ("you are successfully logged out...") to notify the user that they are successfully logged out through our website and a link is passed through which they directly redirect to the home ("home.jsp") page.

5. emp.sql

This database table is used to store user records like his/her name, mail-id, password, country, etcetera.

Syntax

create table emp
(
    uname varchar2(20),
    umail varchar2(30),
    upass varchar2(20),
    ucountry varchar2(20)
);

Insert some row as in the following:

EMP Table

Let's start creating this application

Follow several steps and you will be done.

Step 1

Open the NetBeans IDE.

NetBeans IDE

Step 2

Now choose "Java-web" -> "web-application" as in the following.

Java Web Application

Step 3

Type your project name. As I typed "Login&LogoutApp" as in the following.

Project name wizard

Step 4

After click on "Next" choose the Java version and server wizard as in the following.

server wizard

Step 5

Now delete your default "index.jsp" file and create a new "home.html" file and write the following code there.

home.jsp

<%@ page import="java.util.*" %>

<HTML> 

    <HEAD>

        <TITLE>Login Page</TITLE>

    </HEAD>

    <BODY >

        <form method="post" action="process.jsp">

            <center>

                <h1 style="color:blue">Welcome To Demo Web-Page</h1>

                <b style="color:blue">Login Here</b><br>

                <table border="1" width="2" bgcolor="khaki" style="color:red">

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

                    <tr><td><b>Password</b></td> <td><INPUT type="password" name="upass"></td></tr>

                    <tr><td><input type="submit" value="Login"></td>

                        <td><input type="reset" value="Reset"></td>

                </table>

        </form>

    </BODY>

</HTML>

Step 6

Create a new JSP page named "process.jsp" and write the following code.

process.jsp

<%@ page language="java" import="java.sql.*"%>

<%

    String name = request.getParameter("uname");

    String pass = request.getParameter("upass");

    try {

        Class.forName("oracle.jdbc.driver.OracleDriver");

        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:XE", "sandeep", "welcome");

        PreparedStatement ps = con.prepareStatement("select * from emp where uname=? and upass=?");

        ps.setString(1, name);

        ps.setString(2, pass);

        ResultSet rs = ps.executeQuery();

        int x = 0;

        while (rs.next()) {

            if (rs.getString(1).equals(name) && rs.getString(3).equals(pass)) {

                x = 1;

            } else {

                x = 2;

            }

        }

        if (x == 1) {

 

%>

<jsp:include page="logout.html"/>

<%                HttpSession s = request.getSession();

    out.println("<center><h1>Welcome: " + name + "</h1>");

    out.println("<br/><b>You are successfully login........ ");

} else {

    out.println("<center>" + "<b>Either You Enter Wrong UserName or Password</b>");

%>

<jsp:include page="home.jsp"/>

<%}

 

    } catch (Exception ex) {

        out.println(ex);

    } 

%>

Step 6

Create another html page named "logout" and write following code there.

logout.html

<!DOCTYPE html>

<html>

    <head>

        <title>Logout Page</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">

    </head>

    <form method="link" action="logoutprocess.jsp">

    <body align="right">

        <input type="submit" value="Logout">

    </body>

</html>

Step 7

Create another JSP page named "logoutprocess" and write the following code there.

logoutprocess.jsp

<html><body bgcolor="khaki">

        <%

            session.invalidate();

        %>

        <h1><font color="Red">You are Sucessfully logged out...</font></h1>

        <a href="home.jsp">Go-Back To Home Page</a>

    </body>

</html>

Step 8

Now your project is ready to run.

Right-click on the "home.jsp" file and select "Run", the following output should be generated there.

Output

Step 9

Now type a user name and password (passed in the database table) in our form.

Filling form

Step 10

Click on the "Login" button and the following output is shown there.

Login Page

Step 11

Now you are successfully logged in. when you want to log out then click on the "logout" button. When the logout button is clicked the following is generated.

Logout

Step 12

Now click on the given link to reach the home page. After reaching the home page, we start the login again, now this time we provide the wrong username and password. What happens is shown below.

Wrong form filling

Error Output

Note:

As you can see above, when we provide the wrong detail then it redirects to the home page. Since there is a property of JSP called "include" that I can define in the previous article, through which we can include other pages in our current page.

Similarly we can login with another user.

Up Next
    Ebook Download
    View all
    Learn
    View all