2 WT Servlets Session MGT
2 WT Servlets Session MGT
DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING
Subject Name: Web Technology
Day: 27
Topics Covered: Java Servlet- Session
Management
1
Faculty Name: Dr. Avinash Dwivedi Programe Name: B.Tech (CSE,AI &ML)
Prerequisites, Objectives and Outcomes
• Disadvantage of Cookies
– It will not work if cookie is disabled from the browser.
– Only textual information can be set in Cookie object.
• Note: Gmail uses cookie technique for login. If you
disable the cookie, gmail won't work.
Cookies API
• <form action="servlet1">
• Name:<input type="text" name="userName"/
><br/>
• <input type="submit" value="go"/>
• </form>
FirstServlet.java
• import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
• public class FirstServlet extends HttpServlet {
• public void doGet(HttpServletRequest request, HttpServletResponse response){
• try{
• response.setContentType("text/html");
• PrintWriter out = response.getWriter();
• String n=request.getParameter("userName");
• out.print("Welcome "+n);
• //creating form that have invisible textfield
• out.print("<form action='servlet2'>");
• out.print("<input type='hidden' name='uname' value='"+n+"'>");
• out.print("<input type='submit' value='go'>");
• out.print("</form>");
• out.close();
• }catch(Exception e){System.out.println(e);}
• } }
SecondServlet.java
• import java.io.*;
• import javax.servlet.*;
• import javax.servlet.http.*;
• public class SecondServlet extends HttpServlet {
• public void doGet(HttpServletRequest request, HttpServletResponse resp
onse)
• try{
• response.setContentType("text/html");
• PrintWriter out = response.getWriter();
• //Getting the value from the hidden field
• String n=request.getParameter("uname");
• out.print("Hello "+n);
• out.close();
• }catch(Exception e){System.out.println(e);}
• } }
URL Rewriting
• In URL rewriting, we append a token or identifier to the URL
of the next Servlet or the next resource. We can send
parameter name/value pairs using the following format:
• url?name1=value1&name2=value2&??
• A name and a value is separated using an equal = sign, a
parameter name/value pair is separated from another
parameter using the ampersand(&). When the user clicks the
hyperlink, the parameter name/value pairs will be passed to
the server. From a Servlet, we can use getParameter() method
to obtain a parameter value.
• the following format:
• url?name1=value1&name2=value2&??
• A name and a value is separated using an equal = sign, a
parameter name/value pair is separated from another
parameter using the ampersand(&). When the user clicks the
hyperlink, the parameter name/value pairs will be passed to
the server. From a Servlet, we can use getParameter() method
to obtain a parameter value.
Advantage of URL Rewriting
Methods Description
long getCreationTime() returns the time when the session was
created, measured in milliseconds since
midnight January 1, 1970 GMT.
String getId() returns a string containing the unique
identifier assigned to the session.
long getLastAccessedTime returns the last time the client sent a
() request associated with the session
int getMaxInactiveInterval returns the maximum time interval, in
() seconds.
void invalidate() destroy the session
boolean isNew() returns true if the session is new else false