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 Name Description
Loginname Identification of the person filling the Login form.
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]
Loginname Password
laura cath
mirella mrsbean
bintu 12345
Following is the source code of the HTML page i.e. 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/ login" 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>
The following is the source code for "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(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
ServletOutputStream out=response.getOutputStream();
String url="jdbc:odbc:MyDsn";
try
{
String login=request.getParameter("Loginid");
String pass=request.getParameter("Password");
Class.forName("sun.jdbc.odbc.J dbcOdbcDriver");
conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from login where loginname='" +login +"'
response.setContentType("text/ html");
response.setStatus(HttpServletResponse.SC_OK);
if(rs.next()==false)
{
out.println("<BR><BR><BR><BR><BR>");
out.println("<html><head><title>Login check</ title></ head><body>");
out.println("<CENTER><B>Unknown User</ B>");
out.println("<BR><BR>");
out.println("<h3>Access Denied</ h3></ CENTER>");
out.println("<BR><BR><BR>");
out.println("<CENTER><Input Type=Button Value=Back></ CENTER>");
out.println("</ body></ html>");
}
else
{
out.println("<html><head><title>Login Check</ title></ head><body>");
out.println("<BR><BR><BR><BR><BR>");
out.println("<CENTER><B>Welcome " +" " +rs.getString("loginname") +"</ B></ C
out.println("<CENTER>");
out.println("<h3>You have been Authenticated</ h3></ CENTER>");
out.println("<BR><BR><BR>");
out.println("<CENTER><Input Type=Button Value=HOME></ CENTER>");
out.println("</ body></ html>");
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
Diagram 24.24 Output of login.class for an invalid username and password
Connect JSP with mysql
In this example we will show you how to connect to MySQL database from
your JSP code. First, you need to create database and then write jsp code to
connect jsp to database.
1. Create a database:
First create a database named usermaster in mysql. Before running the jsp
code you need to paste mySqlConnector.jar in lib directory of jdk.
mysql> create database usermaster;
2. Connect JSP with mysql:
Here is the jsp code.
ConnectJspToMysql.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>Connection with mysql database</title>
</head>
<body>
<h1>Connection status </h1>
<%
try {
/* Create string of connection url within specified
format with machine name,
port number and database name. Here machine
name id localhost and
database name is usermaster. */
String connectionURL =
"jdbc:mysql://localhost:3306/usermaster";
// declare a connection by using Connection
interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstanc
e();
/* Create a connection by using getConnection()
method that takes parameters of
string type connection url, user name and password
to connect to database. */
connection =
DriverManager.getConnection(connectionURL,
"root", "root");
// check weather connection is established or not by
isClosed() method
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL
server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</font>
</body>
</html>
Output:
Output of the program when connection is established with specified mysql
database :