Online Courses
Free Tutorials  Go to Your University  Placement Preparation 

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
 
Best answer

HttpSession Interface

Container creates a session id for each user. The container uses this id to identify the particular user. 

An object of HttpSession can be used to perform two tasks: 

  • bind objects
  • view and manipulate information about a session, such as the session identifier, creation time, and last accessed time

HttpSession

MethodDescription
public HttpSession getSession()Returns the current session associated with this request, or if the request does not have a session, creates one
public HttpSession getSession(boolean create)Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session
public String getId()Returns a string containing the unique identifier value
public long getCreationTime()returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT
public long getLastAccessedTime()Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.
public void invalidate()Invalidates this session then unbinds any objects bound to it

Example Code

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);

HttpSession session=request.getSession();

session.setAttribute("uname",n);

out.print("<a href='servlet2'>visit</a>");

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 response){

try{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

HttpSession session=request.getSession(false);

String n=(String)session.getAttribute("uname");

out.print("Hello "+n);

out.close();

                }catch(Exception e){System.out.println(e);}

}

}

index.html

<form action="servlet1">

Name:<input type="text" name="userName"/><br/>

<input type="submit" value="go"/>

</form>

web.xml

<?xml version="1.0"?>

<web-app>

<servlet>

<servlet-name>s1</servlet-name>

<servlet-class>FirstServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s1</servlet-name>

<url-pattern>/servlet1</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>s2</servlet-name>

<servlet-class>SecondServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s2</servlet-name>

<url-pattern>/servlet2</url-pattern>

</servlet-mapping>

</web-app>

Output

Output 1

Output 2

Output 3


For more RTU V Sem Advance Java Lab Experiments Click here


3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...