Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Session Login and Logout


We can bind the objects on HttpSession instance and get the objects by using setAttribute and getAttribute methods.

In the previous page, we have learnt about what is HttpSession, How to store and get data from session object etc.

Here, we are going to create a real world login and logout application without using database code. We are assuming that password is admin123.


Visit here for login and logout application using cookies only servlet login and logout example using cookies


In this example, we are creating 3 links: login, logout and profile. User can’t go to profile page until he/she is logged in. If user is logged out, he need to login again to visit profile.

In this application, we have created following files.

  1. index.html
  2. link.html
  3. login.html
  4. LoginServlet.java
  5. LogoutServlet.java
  6. ProfileServlet.java
  7. web.xml

File: index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“ISO-8859-1”>
  5. <title>Servlet Login Example</title>
  6. </head>
  7. <body>
  8. <h1>Login App using HttpSession</h1>
  9. <a href=“login.html”>Login</a>|
  10. <a href=“LogoutServlet”>Logout</a>|
  11. <a href=“ProfileServlet”>Profile</a>
  12. </body>
  13. </html>

File: link.html

  1. <a href=“login.html”>Login</a> |
  2. <a href=“LogoutServlet”>Logout</a> |
  3. <a href=“ProfileServlet”>Profile</a>
  4. <hr>

File: login.html

  1. <form action=“LoginServlet” method=“post”>
  2. Name:<input type=“text” name=“name”><br>
  3. Password:<input type=“password” name=“password”><br>
  4. <input type=“submit” value=“login”>
  5. </form>

File: LoginServlet.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. import javax.servlet.http.HttpSession;
  8. public class LoginServlet extends HttpServlet {
  9.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  10.                     throws ServletException, IOException {
  11.         response.setContentType(“text/html”);
  12.         PrintWriter out=response.getWriter();
  13.         request.getRequestDispatcher(“link.html”).include(request, response);
  14.         String name=request.getParameter(“name”);
  15.         String password=request.getParameter(“password”);
  16.         if(password.equals(“admin123”)){
  17.         out.print(“Welcome, “+name);
  18.         HttpSession session=request.getSession();
  19.         session.setAttribute(“name”,name);
  20.         }
  21.         else{
  22.             out.print(“Sorry, username or password error!”);
  23.             request.getRequestDispatcher(“login.html”).include(request, response);
  24.         }
  25.         out.close();
  26.     }
  27. }

File: LogoutServlet.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. import javax.servlet.http.HttpSession;
  8. public class LogoutServlet extends HttpServlet {
  9.         protected void doGet(HttpServletRequest request, HttpServletResponse response)
  10.                                 throws ServletException, IOException {
  11.             response.setContentType(“text/html”);
  12.             PrintWriter out=response.getWriter();
  13.             request.getRequestDispatcher(“link.html”).include(request, response);
  14.             HttpSession session=request.getSession();
  15.             session.invalidate();
  16.             out.print(“You are successfully logged out!”);
  17.             out.close();
  18.     }
  19. }

File: ProfileServlet.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. import javax.servlet.http.HttpSession;
  8. public class ProfileServlet extends HttpServlet {
  9.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  10.                       throws ServletException, IOException {
  11.         response.setContentType(“text/html”);
  12.         PrintWriter out=response.getWriter();
  13.         request.getRequestDispatcher(“link.html”).include(request, response);
  14.         HttpSession session=request.getSession(false);
  15.         if(session!=null){
  16.         String name=(String)session.getAttribute(“name”);
  17.         out.print(“Hello, “+name+” Welcome to Profile”);
  18.         }
  19.         else{
  20.             out.print(“Please login first”);
  21.             request.getRequestDispatcher(“login.html”).include(request, response);
  22.         }
  23.         out.close();
  24.     }
  25. }

File: web.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  3. xmlns=“http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
  4. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=“WebApp_ID” version=“2.5”>
  5.   <servlet>
  6.     <description></description>
  7.     <display-name>LoginServlet</display-name>
  8.     <servlet-name>LoginServlet</servlet-name>
  9.     <servlet-class>LoginServlet</servlet-class>
  10.   </servlet>
  11.   <servlet-mapping>
  12.     <servlet-name>LoginServlet</servlet-name>
  13.     <url-pattern>/LoginServlet</url-pattern>
  14.   </servlet-mapping>
  15.   <servlet>
  16.     <description></description>
  17.     <display-name>ProfileServlet</display-name>
  18.     <servlet-name>ProfileServlet</servlet-name>
  19.     <servlet-class>ProfileServlet</servlet-class>
  20.   </servlet>
  21.   <servlet-mapping>
  22.     <servlet-name>ProfileServlet</servlet-name>
  23.     <url-pattern>/ProfileServlet</url-pattern>
  24.   </servlet-mapping>
  25.   <servlet>
  26.     <description></description>
  27.     <display-name>LogoutServlet</display-name>
  28.     <servlet-name>LogoutServlet</servlet-name>
  29.     <servlet-class>LogoutServlet</servlet-class>
  30.   </servlet>
  31.   <servlet-mapping>
  32.     <servlet-name>LogoutServlet</servlet-name>
  33.     <url-pattern>/LogoutServlet</url-pattern>
  34.   </servlet-mapping>
  35. </web-app>

download this example (developed using Eclipse IDE)

Output

httpsessionloginoutput1

httpsessionloginoutput2.jpg

httpsessionloginoutput3.jpg

httpsessionloginoutput4.jpg

httpsessionloginoutput5.jpg

httpsessionloginoutput6.jpg

If again you click on the profile link, you need to login first.

Comments

comments