Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Authentication in Web Application

In this example we will show you how to authenticated the user against database user name and password. This program constists of a JSP page and a Servlet to authenticate the user against database password.


Authentication in Web Application


Example for Authenticating user in JSP based application using servlet and MySql

In this example we will show you how to authenticated the user against database user name and password. This program constists of a JSP page and a Servlet to authenticate the user against database password.

User enters the username and password on the JSP page and clicks on the the "Check" button. On the form submit event data is posted to the Servlet for authenticating the user. Servlet makes JDBC connection and authenticate the user.

We are using tomcat server for running Servlet and JSP page. You can use any browser to test the application

Authentication means that user can be recognized that the user really exist for which he is inputting values. We are using two files Authentication.jsp and and we are making the application in "webapps/JSPMultipleForms" in tomcat server. We have used MySQL for database connection and by putting values in "user" table we have authenticated the input of user. Table structure code for "user" table is given as below:

User Table Structure of MySQL:

`user` varchar(256) default NULL,
`password` varchar(256) default NULL

/*Data for the table `user` */

insert into `user`(`user`,`password`) values ('amit','kumar');

Authentication.jsp is calling Servlet to authenticate user's input. Servlet "" which will first establishes the JDBC connection to the database on the machine whose IP address is The database used in this application is messagepaging. You can change the database before running the example at your machine.  Then program authenticates the user against database. If input given by user is correct then a message will flash on the browser that "User Authenticated" else "You are not an authentic person".

The code for is given below:


import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Authentication extends HttpServlet{
  private ServletConfig config;
  public void init(ServletConfig config)
  throws ServletException{
  public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException{
  PrintWriter out = response.getWriter();
  String connectionURL = "jdbc:mysql://";
  Connection connection=null;
  ResultSet rs;
  String userName=new String("");
  String passwrd=new String("");
  try {
 // Load the database driver
  // Get a Connection to the database
  connection = DriverManager.getConnection(connectionURL, "root""root"); 
  //Add the data into the database
  String sql = "select user,password from User";
  Statement s = connection.createStatement();
  s.executeQuery (sql);
  rs = s.getResultSet();
  while ( ()){
  rs.close ();
  s.close ();
  }catch(Exception e){
  System.out.println("Exception is ;"+e);
  if(userName.equals(request.getParameter("user")) && 
  out.println("User Authenticated");
  out.println("You are not an authentic person");

2. Authentication.jsp

<%@ page language="java" %>
      Please enter your Name and Password here:
  <form name="frm" action="/JSPMultipleForms/Authentication" method="Post" >
      Name:&nbsp;&nbsp;&nbsp;<input type="text" name="user" value=""/>
      Password:<input type="text" name="pass" value=""/>
      &nbsp;&nbsp;&nbsp;<input type="submit" value="Check" />

For invoking servlet in tomcat server we have to include the following lines into "web.xml" file.

3. web.xml

<!--web.xml code -->



To Run the above example you have to follow these steps:

1.Create and Save
2.Compile and place the class file into classes folder.
3.Insert the servlet name and mapping in web.xml.
4.Create and Save Authentication.jsp file in JSPMultipleForms folder.
5.Deploy the Tomcat Server.
6.Type the following line in address bar "http://localhost:8080/JSPMultipleForms/Authentication.jsp"

Output :


User authenticated

User not authenticated

Download Source Code


Share on Google+Share on Google+

Authentication in Web Application

Posted on: June 24, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Authentication in Web Application   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 27, 2011
about this code

this code works only if there if 1 persons data in database....if no of data is more this code does not work........
October 18, 2011
u dumbasses....

u guys are such an asshole that u cnt even write a code properly........u morons..........atleast have sm diginity and dnt play wid newbeis the need to learn and u r just waste of time.........
May 17, 2014
regarding this code

difficulty in understanding this code,unable this run program in to import .xml files and create table in mysql in eclipse plzzzz