HttpSession
java.servlet.http Package
1. The classes and interfaces of javax.servlet.http package handle servlets that communicates using HTTP.2. These servlets are also called as HTTP servlets.
3. The following are the commonly used interfaces of the javax.servlet.http package:
a) HttpServletRequest Interface
b) HttpServletResponse Interface
c) HttpSession Interface
HttpSession Interface
The HttpSession interface contains methods to maintain the state of an end user across a Web application.An object of the HttpSession interface provides support for tracking and managing the session of an end user.
The following describes various methods of the HttpSession interface:
1. public void setAttribute (String name, Object value)
Binds the object with a name and stores the name/value pair as an attribute of the HttpSession object. If an attribute already exists, then this method replaces the existing attribute.
2. public Object getAttribute(String name)
Retrieves the String object specified in the parameter ,from the session object . If no object is found for the specified attribute , then the getAttribute() method returns null.
3. public Enumeration getAttributeNames ()
Returns an Enumeration that contains the name of the all the objects that are bound as attribute to the session object.
Basic Functioning of Session

Creating Simple Program in Servlet for using Session.In this we will receive data from index.jsp and make it global object by Session and after that call it in welcome.jsp.
Step 1 : Create index.jsp with two fields and form action.
Step 2 : Create a new file web.xml under WebContent/WEB-INF folder and write below code.
Step 3 : Create a package com.javat.SessionDemo under Java resources-> src folder and then create the servlet class Login.java. Once you ready then write the code given below.
In this code we will receive data from index page and store in variables after that make it Global.
Step 4: Create a new file welcome.jsp under Webpages and write the code given below.
Call the data that we have Stored using Session.
No comments:
Post a Comment