Databases| SQL| MySQL| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Login Authentication in JSP 
 

In this example we will show you how to authenticate and login user against database username and password.

 

Login Authentication in JSP

                         

Example program for Login Authentication using JSP, Servlet, MySQL

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

User enters the username and password on the JSP page and clicks on the "Sign-In" button. On the form submit event, data is posted to the servlet for authenticating the user. Servlet makes JDBC connection and authenticate the user. Before submitting data to servlet, javascript is ensuring that none of the fields must be empty.

We are using tomcat server for running Servlet and JSP page. You can use any browser to test the application. We are using two files AuthenticLogin.jsp and LoginAuthentication.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 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');

AuthenticLogin.jsp is calling Servlet "LoginAuthentication" to authenticate user's input. Servlet "LoginAuthentication.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 "Welcome" and user name  else "Please enter correct username and password" and there will be given a link to login again.

The code for LoginAuthentication.java is given as below:

1. LoginAuthentication.java

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginAuthentication 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("WELCOME "+userName);
      }
      else{
        out.println("Please enter correct username and password");
        out.println("<a href='AuthenticLogin.jsp'><br>Login again</a>");
      }
  }
}  

2. AuthenticLogin.jsp

<%@ page language="java" %>
<html>
<head>
<title>Login Page</title>
<script language = "Javascript">
function Validate(){
var user=document.frm.user
var pass=document.frm.pass

if ((user.value==null)||(user.value=="")){
alert("Please Enter user name")
user.focus()
return false
}
if ((pass.value==null)||(pass.value=="")){
alert("Please Enter password")
pass.focus()
return false
}
return true
}
</script>
</head>
<body>
<h1>Login
<br>
</h1>
<form name="frm" action="/JSPMultipleForms/LoginAuthentication" method="Post" onSubmit="return Validate()" >
Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="user" value=""/><br>
Password:<input type="password" name="pass" value=""/><br>
<br>&nbsp;&nbsp;&nbsp;<input type="submit" value="Sign-In" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" />
</form>
</body>
</html>

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>LoginAuthentication</servlet-name>
<servlet-class>LoginAuthentication</servlet-class>
</servlet>

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

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

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

Output:

If correct entries were not made then you have to login again.

Download Source Code

                         

» View all related tutorials
Related Tags: c database design com ide data software io get install this id secure war tab work databases to base ast

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 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

}catch(Exception e){
System.out.println("Exception is ;"+e);
}
i think System.out.println is not valid for browser output
else every thing is fine
Thanks

Posted by narendra on Friday, 12.12.08 @ 02:20am | #82673

ur program is correct but i want is that the same authentication in sql server 2000 instead of mysql

Posted by arun on Thursday, 12.4.08 @ 07:17am | #82390

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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 | Flex 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.