Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Quickguide Tomcat Servlet to authenticate user

Related Tutorials


 
 

Share on Google+Share on Google+

Servlet to authenticate user

Advertisement
For security everyone want to restrict the unauthenticated user for access the web site.

Servlet to Authenticate User

     

For security everyone want to restrict the unauthenticated user for access the web site. Now in this example there is a common way of restricting access to websites is by using a use name and there password to be authenticate the connection that checked to database. When the valid username and password from the database then client will be entered with authenticate user and password.

When we want that someone else should handle the response of our servlet, then there we should use sendRedirect() method. 

In sendRedirect whenever the client send any request it goes to the container, In that case container decides whether concerned servlet can handle the request or not ,If  not then the servlet decides that the request can be handle by other the servlet. Then the servlet calls the sendRedirect() method of the response object and sends back the response to the browser. The browser search  the status code and look for that servlet which  handle the request. If again the browser makes a new request, but with the name of that servlet which  handle the request and the result will be displayed to client browser. In this process the client is unaware of the processing. 


The code of the program is given below:

<html>
<head>
<title>Servlet to authenticate user</title>
</head>
<body bgcolor="#999966">
<h2 align="">Servlet to Authenticate User</h2> <hr>
<form action="/userregister/authenticateUserServlet" method="POST">
<p>Enter User ID:&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="username" size="20"></p>
<p>Enter password:&nbsp; <input type="text" name="password" size="20"></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="Submit" name="B1"></p>
</form>
</body>
</html>

authenticateUserServlet .java

package myservlets;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class authenticateUserServlet extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String name = request.getParameter("username");
String password = request.getParameter("password");

try{
String driver = "org.gjt.mm.mysql.Driver";
Class.forName(driver).newInstance();

Connection con=null;
ResultSet rst=null;
Statement stmt=null;

String url="jdbc:mysql://localhost/jsp?user=root&password=root";
con=DriverManager.getConnection(url);
stmt=con.createStatement();
String query = "select uname from login where uname='"+name+"' and password='"+password+"'";
System.out.println(query);
ResultSet rs = stmt.executeQuery(query);

if(rs.next()){

response.sendRedirect("/userregister/ValidentryServlet");
}
else{
pw.println("Please insert veiled Username and Password!");
}
}

catch(Exception e){
System.out.println(e.getMessage());
}
}
}

ValidentryServlet .java

package myservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ValidentryServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
pw.println("Welcome to World of Knowledge!" + " ");
pw.println("Roseindia Technologies Pvt.Ltd. ");
}
}

web.xml file for this program:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">
<description>
Servlet to Authenticate User
</description>
<display-name>Servlet to Authenticate User</display-name>
<servlet>
<servlet-name>authenticateUserServlet</servlet-name>
<servlet-class>myservlets.authenticateUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>authenticateUserServlet</servlet-name>
<url-pattern>/authenticateUserServlet</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>ValidentryServlet</servlet-name>
<servlet-class>myservlets.ValidentryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidentryServlet</servlet-name>
<url-pattern>/ValidentryServlet</url-pattern>
</servlet-mapping>
</web-app>

This is the Authenticate User name and password:

This is the output of the above input.

The output of the program is given below:

This is the output of the above input.

The output of the program is given below:

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 8, 2008

Related Tutorials

Discuss: Servlet to authenticate user   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com