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[[Link]]
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[[Link]]
Logi
nna Password
me
laura cath
mirel
mrsbean
la
bintu 12345
[Link]
<HTML>
<HEAD>
<TITLE> SCT's
User Authentication
Form </TITLE>
</HEAD>
<BODY
Background="images
/[Link]">
<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>
[Link]:
import [Link].*;
import [Link].*;
import
[Link].*;
import
[Link].*;
public class login
extends HttpServlet
{
Connection conn;
public void
service(HttpServletR
equest request,
HttpServletResponse
response)
throws IOException
{
ServletOutputStream
out=[Link]
utStream();
String
url="jdbc:odbc:MyDs
n";
try
{
String
login=[Link]
meter("Loginid");
String
pass=[Link]
meter("Password");
[Link]("sun.j
[Link]
river");
conn=DriverManager
.getConnection(url);
Statement
stmt=[Link]
ement();
ResultSet
rs=[Link]
("select * from login
where loginname='"
+ login + "' and
password= '" + pass +
"'");
[Link]
ype("text/html");
[Link](Ht
tpServletResponse.S
C_OK);
if([Link]()==false)
{
[Link]("<BR><B
R><BR><BR><BR>
");
[Link]("<html><
head><title>Login
check</title></head>
<body>");
[Link]("<CENTE
R><B>Unknown
User</B>");
[Link]("<BR><B
R>");
[Link]("<h3>
Access
Denied</h3></CENT
ER>");
[Link]("<BR><B
R><BR>");
[Link]("<CENTE
R><Input
Type=Button
Value=Back></CEN
TER>");
[Link]("</body>
</html>");
}
else
{
[Link]("<html><
head><title>Login
Check</title></head>
<body>");
[Link]("<BR><B
R><BR><BR><BR>
");
[Link]("<CENTE
R><B>Welcome " +
""+
[Link]("loginna
me") +
"</B></CENTER>");
[Link]("<CENTE
R>");
[Link]("<h3>
You have been
Authenticated</h3><
/CENTER>");
[Link]("<BR><B
R><BR>");
[Link]("<CENTE
R><Input
Type=Button
Value=HOME></CE
NTER>");
[Link]("</body>
</html>");
}
[Link]();
}
catch(SQLException
SQLExcp)
{
[Link]("SQLExce
ption: " + SQLExcp);
}
catch(ClassNotFound
Exception
clsNotFndExcp)
{
[Link]("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 "[Link]" 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 [Link] file. for
example:
Diagram 24.23 Output of [Link]
Diagram 24.23 Output of [Link] for a valid username and password