In the previous few chapters, we have seen what JavaBeans are and how to use them inside JSP Pages. As with any object, there is a scope, i.e., the places where the object is visible. In this chapter, we are going to take a look at the scope of the JavaBeans that are declared inside a JSP.
So, lets get started!!!
JavaBean Scope in a JSP
JavaBeans have scope, just like all variables. You declare this in the jsp:useBean element when you use the bean for the first time. There are multiple scopes possible for a bean.
They are:
1. Page
2. Request
3. Session
4. Application
We have started with Page which is the least visible and went on till Application which is most Visible. To make it easier to understand, take a look at the picture below. It shows you how the scopes are arranged.
Bean objects are only available/accessible in the scope they were declared/created to be visible.
Let us look at these scopes in a little more detail.
Page Scope
If a bean is declared with Page scope, it is equivalent to local variables in regular java code. If a bean is declared with page scope, the reference to the bean disappears once the JSP page is processed. It cannot be referenced by any other JSP or servlet, even if a forward or include is used.
Ex:
< jsp : useBean id="address" class="com.test.AddressBean" scope="page" / >
Request Scope
If a bean is declared with Request scope, it can be accessed by any JSP or Servlet within the same request. The reference remains alive in any other servlet or JSP that is called by jsp:include and jsp:forward or using the RequestDispatcher object. You can declare a bean in Request scope as follows:
Ex:
< jsp : useBean id="address" class="com.test.AddressBean" scope="request" / >
Session Scope
If a bean is declared with Session scope, it is available to all the JSP & Servlets that are accessed by a single user. Even if the user navigates across multiple pages, the beans are available for use. You can declare a bean with session scope as follows:
Ex:
< jsp : useBean id="address" class="com.test.AddressBean" scope="session" / >
Application Scope
Application scope is the widest or most visible scope. A bean declared with application scope is visible to just about any servlet or JSP in the whole applications context, across user sessions. Though, this might sound awesome, we usually do not declare beans with this scope because it can end up affecting us instead of being beneficial. For ex: if the value entered by one user in his web page is visible to another guy, it would be disastrous, wouldn’t it? So, we need to be cautious before using this scope. We can declare a bean with application scope as follows:
Ex:
< jsp : useBean id="address" class="com.test.AddressBean" scope="application" / >
Previous Chapter: Chapter 41 - Modifying Bean Properties
Next Chapter: Chapter 43 - Accessing JavaBeans in JSP
Topics Covered in the Blog - Synopsis
Showing posts with label session scope. Show all posts
Showing posts with label session scope. Show all posts
Thursday, April 14, 2011
Tuesday, March 15, 2011
Chapter 15: The Session
In the previous chapter we took a look at how the HTTP Request can help us move data from the JSP to the Servlet and vice versa and the methods that are available in the Request object that would help us handle the data it carries.
In this chapter, we are going to look at the Session which is a super-set of the request and can contain data that needs to be retained across different pages.
So, lets get started!!!
The HTTP Session:
The Session is the place where data can be stored and retrieved across multiple hits from the same browser for a certain period of time. For example, lets say you want to login to your online banking website and the browser asks you to enter your login id and password after every click? It would be irritating isn’t it?
Exactly yes.
Thankfully we have the Http session.
When we logon to our bank website and enter our login id and password, the application saves our credentials in the session and uses it when we navigate between screens on the banks website.
Note: For reasons of safety and security, usually sessions in sensitive websites like a bank website or a stock brokers site, the session expires automatically after 5 minutes of inactivity or if the user presses the back or refresh buttons.
Getting the Session Object:
The session object can be obtained from the Request using the below line of code:
HttpSession session = request.getSession();
Setting & Getting Attributes from the Session:
We can set and retrieve attributes from the session using the setAttribute & getAttribute methods.
Ex:
session.setAttribute(“Key”, value); //Setting the value
Object obj = session.getAttribute(“Key”); //Getting the value
Here, “Key” is the identifier that will be used to locate the value saved in the session. The “Key” can be used to retrieve the value using the getAttribute() method.
Other Session Methods:
Some other methods available in the Session are:
1. getId() – Every session has a unique id and this method is used to get that id
2. getCreationTime() – To find out when the session was created
3. getLastAccessedTime() – To find out when the session was accessed last
4. getAttributeNames() – To retrieve all the values stored in the session as an Enumeration
To wrap up, sessions are what you can use to track a single user over a short time period (say 5 or 10 mins usually). You get the session object (HttpSession) from the request object. To track multiple users in your application, you can use the Context. Don't worry, that's our next stop…
For now, this chapter is over!!!
Previous Chapter: Chapter 14 - The Request Object
Next Chapter: Chapter 16 - Servlet Context
In this chapter, we are going to look at the Session which is a super-set of the request and can contain data that needs to be retained across different pages.
So, lets get started!!!
The HTTP Session:
The Session is the place where data can be stored and retrieved across multiple hits from the same browser for a certain period of time. For example, lets say you want to login to your online banking website and the browser asks you to enter your login id and password after every click? It would be irritating isn’t it?
Exactly yes.
Thankfully we have the Http session.
When we logon to our bank website and enter our login id and password, the application saves our credentials in the session and uses it when we navigate between screens on the banks website.
Note: For reasons of safety and security, usually sessions in sensitive websites like a bank website or a stock brokers site, the session expires automatically after 5 minutes of inactivity or if the user presses the back or refresh buttons.
Getting the Session Object:
The session object can be obtained from the Request using the below line of code:
HttpSession session = request.getSession();
Setting & Getting Attributes from the Session:
We can set and retrieve attributes from the session using the setAttribute & getAttribute methods.
Ex:
session.setAttribute(“Key”, value); //Setting the value
Object obj = session.getAttribute(“Key”); //Getting the value
Here, “Key” is the identifier that will be used to locate the value saved in the session. The “Key” can be used to retrieve the value using the getAttribute() method.
Other Session Methods:
Some other methods available in the Session are:
1. getId() – Every session has a unique id and this method is used to get that id
2. getCreationTime() – To find out when the session was created
3. getLastAccessedTime() – To find out when the session was accessed last
4. getAttributeNames() – To retrieve all the values stored in the session as an Enumeration
To wrap up, sessions are what you can use to track a single user over a short time period (say 5 or 10 mins usually). You get the session object (HttpSession) from the request object. To track multiple users in your application, you can use the Context. Don't worry, that's our next stop…
For now, this chapter is over!!!
Previous Chapter: Chapter 14 - The Request Object
Next Chapter: Chapter 16 - Servlet Context
Labels:
getting scwcd certified,
http session,
httpsession,
j2ee session,
jsp and servlets,
scwcd,
scwcd certification,
session scope,
the session
| Reactions: |
Monday, March 7, 2011
Chapter 13: Web Application Scope
In the previous Chapters we saw what the HTTP Request object is and how to read stuff from it. In this chapter, we are going to discuss a very important topic. “Scope” – Yes, the Web Application Scope. It is just like basic java programming where you can or cannot see a particular variable depending on its visibility. In Web parlance, the scope of an object defines how long it exists and how you can access it.
So, lets get started!!!
Scope
For the exam, you need to worry about 3 different scopes. They are:
• Request
• Session &
• Application
The scope would play an important part of any web application.
Let us look at the details of these 3 Scopes.
You know that you can set and retrieve attributes (objects) in the request or the session or to the context. So, the purpose of this chapter is to let us identify what attribute is accessible where.
For ex: if you use the request.setAttribute(), you will be able to access it only until this request is alive. Once the request is complete, you will no longer be able to read the attribute that you just set into the request.
But, if you set an attribute into the session you will be able to read it across multiple requests. Similarly if you use the context, you will be able to read it across multiple sessions.
Dont worry if we did not cover the details of the request, scope or the context because, that is exactly what the subsequent chapters are going to do!!!
Previous Chapter: Chapter 12 - Retrieving HTTP Request Header Information
Next Chapter: Chapter 14 - The Request Object
So, lets get started!!!
Scope
For the exam, you need to worry about 3 different scopes. They are:
• Request
• Session &
• Application
The scope would play an important part of any web application.
Let us look at the details of these 3 Scopes.
| Scope | Accessibility | Lifetime |
|---|---|---|
| Request | Current, included, or forwarded pages | Until the response is returned to the user. |
| Session | All requests from same browser within session timeout | Until session timeout or session ID invalidated (such as user quits browser). |
| Application | All request to same Web application | Life of container or explicitly killed (such as container administration action). |
For ex: if you use the request.setAttribute(), you will be able to access it only until this request is alive. Once the request is complete, you will no longer be able to read the attribute that you just set into the request.
But, if you set an attribute into the session you will be able to read it across multiple requests. Similarly if you use the context, you will be able to read it across multiple sessions.
Exam Trivia:
Request is the least available whereas a session is a bit more available and the context is globally available. But, it is not advisable to put all your stuff into the context. Keep only information that you would need across all the users of the website in the context. Keep information that you would need as long as a user is browsing (say his login info) in the session and page specific information in the request.
Dont worry if we did not cover the details of the request, scope or the context because, that is exactly what the subsequent chapters are going to do!!!
Previous Chapter: Chapter 12 - Retrieving HTTP Request Header Information
Next Chapter: Chapter 14 - The Request Object
Subscribe to:
Posts (Atom)
© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

