Example of Login Form in Servlet Tutorial

Example of Login form in Servlet Tutorial. Let's see a simple example of login and logout in servlet.

Example of Login Form in Servlet Tutorial

Example of Login Form in Servlet Tutorial

Here, we are going to create the simple example to create the login form using servlet. We have used oracle10g as the database. There are 5 files required for this application.

  • index.html
  • FirstServlet.java
  • LoginDao.java
  • SecondServlet.java
  • web.xml

You must need to create a table userreg with name and pass fields. Moreover, it must have contained some data. The table should be as:

  1. create table userreg(name varchar2(40),pass varchar2(40));  

index.html

 

  1. <form action="servlet1" method="post">  
  2. Name:<input type="text" name="username"/><br/><br/>  
  3. Password:<input type="password" name="userpass"/><br/><br/>  
  4. <input type="submit" value="login"/>  
  5. </form>  

FirstServlet.java

 

  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3.   
  4. import javax.servlet.RequestDispatcher;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10.   
  11. public class FirstServlet extends HttpServlet {  
  12. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  13.         throws ServletException, IOException {  
  14.   
  15.     response.setContentType("text/html");  
  16.     PrintWriter out = response.getWriter();  
  17.           
  18.     String n=request.getParameter("username");  
  19.     String p=request.getParameter("userpass");  
  20.           
  21.     if(LoginDao.validate(n, p)){  
  22.         RequestDispatcher rd=request.getRequestDispatcher("servlet2");  
  23.         rd.forward(request,response);  
  24.     }  
  25.     else{  
  26.         out.print("Sorry username or password error");  
  27.         RequestDispatcher rd=request.getRequestDispatcher("index.html");  
  28.         rd.include(request,response);  
  29.     }  
  30.           
  31.     out.close();  
  32.     }  
  33.  

  LoginDao.java

 

  1. import java.sql.*;  
  2.   
  3. public class LoginDao {  
  4. public static boolean validate(String name,String pass){  
  5. boolean status=false;  
  6. try{  
  7. Class.forName("oracle.jdbc.driver.OracleDriver");  
  8. Connection con=DriverManager.getConnection(  
  9. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  10.       
  11. PreparedStatement ps=con.prepareStatement(  
  12. "select * from userreg where name=? and pass=?");  
  13. ps.setString(1,name);  
  14. ps.setString(2,pass);  
  15.       
  16. ResultSet rs=ps.executeQuery();  
  17. status=rs.next();  
  18.           
  19. }catch(Exception e){System.out.println(e);}  
  20. return status;  
  21. }  
  22. }  

WelcomeServlet.java

 

  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3.   
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. public class WelcomeServlet extends HttpServlet {  
  10. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  11.     throws ServletException, IOException {  
  12.   
  13.     response.setContentType("text/html");  
  14.     PrintWriter out = response.getWriter();  
  15.           
  16.     String n=request.getParameter("username");  
  17.     out.print("Welcome "+n);  
  18.           
  19.     out.close();  
  20.     }  
  21.   
  22.