User Authentication Form Using Servlet
The following is an authentication form, which validates the Login ID and
Password keyed in by the user and returns an appropriate page generated by a
Servlet code. First of all you will create one table in M.S Access as shown below:
Table Definitions:
Table Name : login[SchoolDemo.mdb]
Primary Key : Nil
Foreign Key : Nil
Column Definition:
Column Name Data Type Width
Loginname Text 20
Password Text 20
Table Description:
Column
Description
Name
Loginna
Identification of the person filling the Login form.
me
Password Password entered by the person
Explanation:
When the user submits the login name and password. A
check is made in the table for that user name and
password. If the user name and password match a
single entry a welcome page will be returned, or else
access denied page will be returned.
After create table in M.S. Access database now you
will create DSN.
Test Records:
login[SchoolDemo.mdb]
Logi
nna Password
me
laura cath
mirel
mrsbean
la
bintu 12345
Login.HTML
<HTML>
<HEAD>
<TITLE> SCT's
User Authentication
Form </TITLE>
</HEAD>
<BODY
Background="images
/grid1.gif">
<BR><BR>
<CENTER>
<H2><B> User's
Authentication Page
</B></H2>
</CENTER>
<FORM
Action="../servlets/lo
gin"
Method="POST">
<BR><BR>
<TABLE
Border="0"
CellPadding="5"
CellSpacing="0"
Width="400"
Align="Center">
<FONT
Color="#000000"
Size="2">
<TR>
<TD
Align="Right">
<B>Login
Name:</B>
</TD>
<TD>
<INPUT
Type="Text"
Name="Loginid"
Size="20">
</TD>
</TR>
<TR>
<TD
Align="Right">
<B>Password:</B>
</TD>
<TD>
<INPUT
Type="Password"
Name="Password"
Size="20">
</TD>
</TR>
</FONT>
</TABLE>
<BR><BR>
<P></P>
<P></P>
<CENTER>
<Input
Type="Submit"
Value="Submit"
Name="Submit">
<Input
Type="Reset"
Value="Reset"
Name="Reset">
</CENTER>
</FORM>
</BODY>
</HTML>
Login.java:
import java.sql.*;
import java.io.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public class login
extends HttpServlet
{
Connection conn;
public void
service(HttpServletR
equest request,
HttpServletResponse
response)
throws IOException
{
ServletOutputStream
out=response.getOutp
utStream();
String
url="jdbc:odbc:MyDs
n";
try
{
String
login=request.getPara
meter("Loginid");
String
pass=request.getPara
meter("Password");
Class.forName("sun.j
dbc.odbc.JdbcOdbcD
river");
conn=DriverManager
.getConnection(url);
Statement
stmt=conn.createStat
ement();
ResultSet
rs=stmt.executeQuery
("select * from login
where loginname='"
+ login + "' and
password= '" + pass +
"'");
response.setContentT
ype("text/html");
response.setStatus(Ht
tpServletResponse.S
C_OK);
if(rs.next()==false)
{
out.println("<BR><B
R><BR><BR><BR>
");
out.println("<html><
head><title>Login
check</title></head>
<body>");
out.println("<CENTE
R><B>Unknown
User</B>");
out.println("<BR><B
R>");
out.println("<h3>
Access
Denied</h3></CENT
ER>");
out.println("<BR><B
R><BR>");
out.println("<CENTE
R><Input
Type=Button
Value=Back></CEN
TER>");
out.println("</body>
</html>");
}
else
{
out.println("<html><
head><title>Login
Check</title></head>
<body>");
out.println("<BR><B
R><BR><BR><BR>
");
out.println("<CENTE
R><B>Welcome " +
""+
rs.getString("loginna
me") +
"</B></CENTER>");
out.println("<CENTE
R>");
out.println("<h3>
You have been
Authenticated</h3><
/CENTER>");
out.println("<BR><B
R><BR>");
out.println("<CENTE
R><Input
Type=Button
Value=HOME></CE
NTER>");
out.println("</body>
</html>");
}
conn.close();
}
catch(SQLException
SQLExcp)
{
out.println("SQLExce
ption: " + SQLExcp);
}
catch(ClassNotFound
Exception
clsNotFndExcp)
{
System.out.println("C
annot find the class:
"+ clsNotFndExcp);
}
}
}
To run the above servlet program , first of all run Apache Tomcat from "Start -> All
Programs -> Apache Tomcat 4.0 -> Start Tomcat."
Now compile Java Servlet Program using below command as shown diagram
24.22
Diagram 24.22 Compile Java Servlet
Now, copy "SimpleServlet.class" File and then paste it under
"C:/Program Files/Apache Tomcat 4.0/webapps/examples/WEB-INF/classes/"
Now, open your any web browser and type html URL where you saved your login.html file. for
example:
Diagram 24.23 Output of Login.html
Diagram 24.23 Output of login.class for a valid username and password