TwitterFacebookGoogle PlusLinkedInRSS FeedEmail

Tuesday, December 18, 2012

Login system using SERVLET and JSP




















 This post is about creating a Login system using Servlet(as controller) and JSP(as view). To understand this post basic knowledge of Servlet, JSP, JDBC type 4 connectivity and Oracle 10g database is required. In this system we are using Oracle Database 10g Express Edition, if you have some other database then you have to do some minor changes for connectivity with the database.

Create a table in the database and name it as login table  

CREATE TABLE login(
username VARCHAR2(20),
password VARCHAR2(20) );

Add record to the table 

INSERT INTO login VALUES('program','diary');

Create a Login page using html save it as login.html


Create Validate.java servlet



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

public class Validate extends HttpServlet {

public void doPost(HttpServletRequest request,HttpServletResponse 
 response) 
      throws ServletException,IOException
{
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  try{
    String name=request.getParameter("uid");
    String pass=request.getParameter("pid");
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:8080:XE",
             "username","password");
 Statement s=con.createStatement();
ResultSet rs=s.executeQuery("select * from login where    
                     username='"+name+"' ");

   if(rs.next())
      {
               
 if( (name.trim().equals(rs.getString(1).trim())) && (   
       pass.trim().equals(rs.getString(2).trim()) )  )
    {

//New session creation
  HttpSession session=request.getSession(true);
  //setting attribute on session
  session.setAttribute("user",name);
 //send request to Welcome.jsp page

RequestDispatcher view =   
      request.getRequestDispatcher("Welcome.jsp");
       
view.forward(request, response);
                
   }
  else
    {
 out.println("<div style='font-size:30px; color:red'>"
          +"Userid and password does not matched "+"</div>");
 RequestDispatcher view =
    request.getRequestDispatcher("login.html");
    
view.include(request, response);
         }
      }
 else
  {
 out.println("<div style='font-size:30px; color:red'>"+"Please fill 
               userid and password"+"</div>");
 RequestDispatcher view=request.getRequestDispatcher("login.html");
    view.include(request, response);
   }
     }catch(Exception e){}
     finally{
     out.close();
 }
 }
}

Create  Welcome.jsp page

After Validation is done by Validate.java servlet transfer the control to Welcome.jsp


Create  Logout.java servlet


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

public class Logout extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
 response.setContentType("text/html;charset=UTF-8");
 PrintWriter out = response.getWriter();
 try {
 HttpSession session=request.getSession();
 session.invalidate();
 out.println("<div style='float:left;font-size:25px;'>"+"click here to "
       +"<a href='login.html'>login</a></div>");
 out.println("<h1 align='center'>"+"Logout Sucessfully..."+"</h1>");
 }finally {            
 out.close();
 }
 }
}


For logout Logout.java servlet is called, it will destroy the session object


0 comments: