Session simply means a particular interval of time.
Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet.
Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize to particular user.
HTTP is stateless that means each request is considered as the new request. It is shown in the figure given below:
Why use Session Tracking?
To recognize the user It is used to recognize the particular user.
Session Tracking Techniques
There are four techniques used in Session tracking:
- Cookies
- Hidden Form Field
- URL Rewriting
- HttpSession
Servlet Login and Logout Example using Cookies
A cookie is a kind of information that is stored at client side.
In the previous page, we learned a lot about cookie e.g. how to create cookie, how to delete cookie, how to get cookie etc.
Here, we are going to create a login and logout example using servlet 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.
- index.html
- link.html
- login.html
- LoginServlet.java
- LogoutServlet.java
- ProfileServlet.java
- web.xml
File: index.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="ISO-8859-1">
- <title>Servlet Login Example</title>
- </head>
- <body>
-
- <h1>Welcome to Login App by Cookie</h1>
- <a href="login.html">Login</a>|
- <a href="LogoutServlet">Logout</a>|
- <a href="ProfileServlet">Profile</a>
-
- </body>
- </html>
File: link.html
- <a href="login.html">Login</a> |
- <a href="LogoutServlet">Logout</a> |
- <a href="ProfileServlet">Profile</a>
- <hr>
File: login.html
- <form action="LoginServlet" method="post">
- Name:<input type="text" name="name"><br>
- Password:<input type="password" name="password"><br>
- <input type="submit" value="login">
- </form>
File: LoginServlet.java
- package com.javatpoint;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class LoginServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out=response.getWriter();
-
- request.getRequestDispatcher("link.html").include(request, response);
-
- String name=request.getParameter("name");
- String password=request.getParameter("password");
-
- if(password.equals("admin123")){
- out.print("You are successfully logged in!");
- out.print("<br>Welcome, "+name);
-
- Cookie ck=new Cookie("name",name);
- response.addCookie(ck);
- }else{
- out.print("sorry, username or password error!");
- request.getRequestDispatcher("login.html").include(request, response);
- }
-
- out.close();
- }
-
- }
File: LogoutServlet.java
- package com.javatpoint;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class LogoutServlet extends HttpServlet {
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out=response.getWriter();
-
-
- request.getRequestDispatcher("link.html").include(request, response);
-
- Cookie ck=new Cookie("name","");
- ck.setMaxAge(0);
- response.addCookie(ck);
-
- out.print("you are successfully logged out!");
- }
- }
File: ProfileServlet.java
- package com.javatpoint;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class ProfileServlet extends HttpServlet {
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out=response.getWriter();
-
- request.getRequestDispatcher("link.html").include(request, response);
-
- Cookie ck[]=request.getCookies();
- if(ck!=null){
- String name=ck[0].getValue();
- if(!name.equals("")||name!=null){
- out.print("<b>Welcome to Profile</b>");
- out.print("<br>Welcome, "+name);
- }
- }else{
- out.print("Please login first");
- request.getRequestDispatcher("login.html").include(request, response);
- }
- out.close();
- }
- }
File: web.xml
No comments:
Post a Comment