Servlets-Sharing of objects in scope

We have seen that servlets may interact by calling each other’s methods, redirecting, and dispatching (forwarding) their requests. Servlets...


We have seen that servlets may interact by calling each other’s methods, redirecting, and dispatching (forwarding) their requests. Servlets may also communicate by accessing objects (attributes) which they have in common with other servlets. Attributes are available to servlets within the scope of request, session, and application. This section describes the ways in which objects may be shared at these different levels.

Request level scope

We have already seen how request level scope can be implemented, through the RequestDispatcher forward method. Here, the request object stays in scope as it is passed from resource to resource.

But in addition to the objects that are natively part of the request, how do we pass other objects along with the request? We do this by using the request.setAttribute method. This method allows us to store an object type by name, which we can later reference by that name as we forward the request to another servlet for processing. The object we store can be of a user-defined type, such as a JavaBean, which is accessible within the called servlet. Objects at this level remain in scope as long as the request is active, which is until the server sends back the response.

Code below shows how we can set a request attribute called count. This code would be found in a calling (forwarding from) servlet in the request dispatching process.

//calledCount is the value we are storing to the attribute
int calledCount;
//Cast our int object to Integer, because cannot store native types
request.setAttribute("count", new Integer(calledCount));

Code below shows how we can retrieve this attribute. This code would be found in a called (forwarded to) servlet in the request dispatching process.

//getting count attribute, and casting back to an int
Integer tempCount = (Integer)request.getAttribute("count");
int calledCount = tempCount.intValue();
out.println("Called count is: " + calledCount);

Read Session Level Scopes also
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: Servlets-Sharing of objects in scope
Servlets-Sharing of objects in scope
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/servlets-sharing-of-objects-in-scope.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/servlets-sharing-of-objects-in-scope.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content