In this article we will learn how to do a simple
login page using JSP. In this login page, we use java script for validation of the blank form. Here we use a HTML page where a user can enter his/her credentials (userid
and password) and after submitting the button action, goes to the doLogin.jsp
page where data is being checked in the database and the valid user name is
shown as output in a welcome.jsp page.
Steps to follow for connecting to DBMS
1. Load the driver class into the runtime environment by using the forName() method of the java.lang class. This method accepts a string parameter which contains the class
name. This method throws a java.lang class not found exception. Class is a
predefined class and forName is the static method of the class, which is used to
load the driver into memory for connectivity.
Ex:- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
2. Create a reference to java.sql.connection by using the getConnection() method of the java.sql. DriverManager class. This method accepts three parameters as the path of the
driver in the JDBC:ODBC bridge. We require a DSN(Data source name) in this
position. Dsn can be created in control panel
Ex: Connection con=DriverManager.getConnection("jdbc:odbc:dsnname","system","password");
The getConnection() method throws a java.sql.sql exception.
3. Create an object of java.sql statement by using the create statement () method of
connection. The statement is being used to execute various sql commands.
Ex:- Statement stmt=con.createStatement();
4. Use an appropriate method of statement to execute a SQL command for select command
use executeQuery() method and for insert, update and delete use executeUpdate()
method.
Creation of dsn(database source name) for Oracle
Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn
tab-click add button-select a driver for which you want to set up data source
(for Oracle- Oracle in XE)-select it and click finish-give any name in data
source name textbox-then click ok button.
Note:- Here Username=system, Password=pintu and Dsn name=dsn1
Table creation with values
Create table login (userid varchar(50),password varchar(50))
insert into login values('Raj Kumar','raj123')
insert into login values('Ravi Kumar','ravi123')
insert into login values('Rahul Kumar','rahul123')
login.html
<html>
<head>
<script type="text/javascript">
function ccheck()
{
uid=document.f1.uid.value;
cpass=document.f1.cpass.value;
if(uid=="" || uid==null)
{
alert("Plz. Enter Your User ID");
document.f1.uid.focus();
return false;
}
if(cpass=="" || cpass==null)
{
alert("Plz. Enter Your Password");
document.f1.cpass.focus();
return false;
}
return true;
}
</script>
<title>Login Page in Jsp</title>
</head>
<body onload="document.f1.uid.focus()">
<h2>LOGIN PAGE IN JSP</h2>
<form id="f1" name="f1" action="doLogin.jsp" method="post" onsubmit="return
ccheck()">
<table>
<tr>
<td><b>User ID</b></td>
<td><input name="uid" type="text"/></td>
</tr>
<tr>
<td><b>Password</b></td>
<td><input name="cpass" type="password"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /> </td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
</html>
doLogin.jsp
<%@ page import="java.sql.*" %>
<%! String cid="",pass="";Connection con; %>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
Statement stmt=con.createStatement();
ResultSet rs;
cid=request.getParameter("uid");
pass=request.getParameter("cpass");
//System.out.println("cid="+cid+" pass="+pass);
rs=stmt.executeQuery("select * from login where userid='"+cid+"' and
password='"+pass+"' ");
if(rs.next())
{
session.setAttribute("scid",cid);
con.close();
response.sendRedirect("welcome.jsp");
}
else
{
%>
<html>
<body>
<h1>Invalid UserID or Password</h1>
<jsp:include page="login.html" />
<%
}
%>
</body>
</html>
welcome.jsp
<%@ page import="java.sql.*" %>
<%! String scid=""; %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Login Page</title>
</head>
<body>
<%
scid=(String)session.getAttribute("scid");
%>
<div id="welcome">
<h2><span>Welcome:::<strong><font color='Red'><%= scid
%></font></strong></span></h2>
</div>
</body>
</html>
Running the application
Run the tomcat then write the below link in the URL
http://localhost:8081/jsp/
Here jsp is the Context path, which we mentioned in the server.xml file, which
is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf)
directory.
After giving the url a set a listing will come, here only three appears
As login.html, doLogin.jsp and welcome.jsp.
Output
Thanks for reading