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:

Let's start creating this application
Follow several steps and you will be done.
Step 1
Open the NetBeans IDE.

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

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

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

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");







Join the conversation! Your thoughts help the community grow.