JSP Session

In this tutorial you will learn about the JSP Session and its application with practical example.

JSP Session

This is the instance of javax.servlet.http.HttpSession which provides a way to identify a user across more than one page request. It behaves like same as session in servlet. The session is used for Add, Remove attributes and also we can get session information of logged in user.

Table Of Contents

#Setting Attributes in Session: With the help of setAttribute(String name, Object value) Method we can set our data in session.

#Getting Attributes from Session: With the help of getAttribute(String name) Method we can get our data from session. It returns the object bound with the specified name in the session.

Example:Let’s get understand it by an example. Create index.jsp file in your project like below

#index.jsp

#Output: The output of this index.jsp file will look like below image.

15

#session.jsp: From here we will set attributes in our session, check below code.

 

#Output: When you will click on index.jsp’s “click” button so this code will run and the output of this above code will look like below image and also we have set attributes in session from above code.

16

#session_second.jsp: When you will click on “Next Page” of “session.jsp” page, The below code will run. Here we will get session attributes which you have set from “session.jsp” page.

#Output: The output of this code will look like below image.

17

 

In this tutorial we have learn about the JSP Session and its application with practical example. I hope you will like this tutorial.