Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Login Example


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. import javax.servlet.RequestDispatcher;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. public class FirstServlet extends HttpServlet {
  9. public void doPost(HttpServletRequest request, HttpServletResponse response)
  10.         throws ServletException, IOException {
  11.     response.setContentType(“text/html”);
  12.     PrintWriter out = response.getWriter();
  13.     String n=request.getParameter(“username”);
  14.     String p=request.getParameter(“userpass”);
  15.     if(LoginDao.validate(n, p)){
  16.         RequestDispatcher rd=request.getRequestDispatcher(“servlet2”);
  17.         rd.forward(request,response);
  18.     }
  19.     else{
  20.         out.print(“Sorry username or password error”);
  21.         RequestDispatcher rd=request.getRequestDispatcher(“index.html”);
  22.         rd.include(request,response);
  23.     }
  24.     out.close();
  25.     }
  26. }

LoginDao.java

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

WelcomeServlet.java

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

Comments

comments