Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
JSP
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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 Authentication.java 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:

CREATE TABLE `user` (
`user` varchar(256) default NULL,
`password` varchar(256) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `user` */

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

Authentication.jsp is calling Servlet to authenticate user's input. Servlet "Authentication.java" which will first establishes the JDBC connection to the database on the machine whose IP address is 192.168.10.59. 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 Authentication.java is given below:

1. Authentication.java

import java.io.*;
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{
     this.config=config;
     }
  public void doPost(HttpServletRequest request, HttpServletResponse response) 
             
throws ServletException,IOException{
      
    PrintWriter out = response.getWriter();
    String connectionURL = "jdbc:mysql://192.168.10.59/messagepaging";
    Connection connection=null;
    ResultSet rs;
    String userName=new String("");
    String passwrd=new String("");
    response.setContentType("text/html");
    try {
       // Load the database driver
      Class.forName("com.mysql.jdbc.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.next ()){
        userName=rs.getString("user");
        passwrd=rs.getString("password");
      }
      rs.close ();
      s.close ();
      }catch(Exception e){
      System.out.println("Exception is ;"+e);
      }
      if(userName.equals(request.getParameter("user")) && 
            passwrd.equals(request.getParameter(
"pass"))){
        out.println("User Authenticated");
      }
      else{
        out.println("You are not an authentic person");
      }
  }
}  

2. Authentication.jsp

<%@ page language="java" %>
Please enter your Name and Password here:
<br>
<br>
<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" />
</form>

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

3. web.xml

<!--web.xml code -->

<servlet>
<servlet-name>Authentication</servlet-name>
<servlet-class>Authentication</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Authentication</servlet-name>
<url-pattern>/Authentication</url-pattern>
</servlet-mapping>

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

1.Create and Save Authentication.java.
2.Compile Authentication.java 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 :

Authentiocation.jsp

User authenticated

User not authenticated

Download Source Code

                          

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
multithreads
html:form
Java Count words,reve
If tag Struts2
Murach�����?
data types used in jav
Declare
Linux Caixa MаÐ?????Ð
functions
update mass mysql
send email jsp
scroll bar
dynamic array value
list folder contents
difference between flu
java file stream
creating table in pdf
dynamic table
agards
data input stream
class diagrams in UML
selecting option value
PHP Date and time func
Java Count Vowels in t
how to write in pdf us
Heap Sort in Java
FCKediter
get url
struts2 checkbox
Photoshop Color Color
regex progam to find v
getter setter methods
Photoshop Photo Effect
Java code for media pl
Javascript Links and B
ร ย�ย�ร 
remember me option in
applet requesting the
spring hibernate web
PDF Tutorials
<s:textarea>
program to validate us
class not found
remove spaces from a s
mysql connection excep
surce codes in j2me
how to override a vari
write to text file lin
JTable size
XMLmerging
jpf
Replace an object with
pthread.h
Combattons la programm
how to generate report
j2ee architecture
Photoshop Photo Effect
how retrive a data fro
Flex in IBM RAD
Icon
ร�ย�ย�l
convert string array t
JNI objective question
breakà¹?à¸???à¹?à¸???à
Murach��?��???
showInputDialog
update struts
bpm
retrive data from data
sub procedure
form bean
foreach
convert excel to csv i
jboss connection pool
convert byte to char
database
digital clok program i
ASP Miscellaneous Incl
strutes
this keyword in get a
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.