my dog learned polymorphism*
The moose likes JSP and the fly likes UseBean, scope question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "UseBean, scope question." Watch "UseBean, scope question." New topic
Author

UseBean, scope question.

Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
i want to know what does the exactly meaning of the scope that we set in usebean tag. (application, request, page).

Aparently i know that it means that the object will be available for the particular time ... but what i want to ask , leme explain.

Suppose i have to print some data in jsp page which is in a bean class being populated by some other class or servlet on request. now what i do is make reportBean object at the startup of page, put it in session and then send request to the servlet, the servlet collect it from session, populate it and then re-put it in session and send the response back to JSP page. The jsp page collect it from session and then add apropriate HTML tags to display the data.

I want to ask, if i set the scope of bean to request, will the same object available in servlet without puting in to sesssion.

OR, what is the best way to implement what i have explained above. As for as it think, i am totaly on wrong track. help me out.
[ September 21, 2006: Message edited by: Muhammad Ali Amin ]

The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If you put it into the request scope, it will only be available when the request is made.
So unless you put it somewhere else again, it will die after the request is over.

I don't understand why you create the bean from the first page. If you populate it in a servlet, you could create it in the servlet, put it request scope, forward to the JSP page and retrieve it from request scope. If you use it only for this JSP, then I think it would be better to put it in request scope, rather than session scope.


[My Blog]
All roads lead to JavaRanch
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
As far as I can see it qualifies for request scope. Do it simple.

- make a request, for that particular reportBean, to servlet
- send the request to the appropriate handler, a java class
- make an object and populate it
- send it back to servlet
- forward it to your display jsp
[ September 21, 2006: Message edited by: Adeel Ansari ]
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Originally posted by Satou kurinosuke:

I don't understand why you create the bean from the first page. If you populate it in a servlet, you could create it in the servlet, put it request scope, forward to the JSP page and retrieve it from request scope. If you use it only for this JSP, then I think it would be better to put it in request scope, rather than session scope.


Ok, suppose i want to populate in servlet, how can i declare a bean in a servlet with SPECIFIED scope?

secondly, i had some initial values in reportBean object, which are collected by JSP page, and depending on these values the servlet decided what to do. so what exact syntax can be of initiliazing a bean with scope specified, accessing it in servlet and the forwarding it to JSP and re-using it in JSP?



P.S:
Sorry for bugging you for this basic thing, but i don't know how to do it ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Ok, suppose i want to populate in servlet, how can i declare a bean in a servlet with SPECIFIED scope?

Calling setAttribute() on either request(passed as a parameter to your servlet), or session (using getSession()), or application (using getServletContext())
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Originally posted by Satou kurinosuke:

Calling setAttribute() on either request(passed as a parameter to your servlet), or session (using getSession()), or application (using getServletContext())


i guess, it is the same approach i am using.

setAttribute() will ultimately put object in session. isn't it so?

What i actually wanted to know is


what does "scope="request"" means.

if we set private before a variable in the class, then it means it scope is limited with in the class.

and if in useBean Tag, the scope is telling the it is visible with in the request, then are we able to get the same object in servlet? and if yes then how can we get it servlet??
[ September 22, 2006: Message edited by: Muhammad Ali Amin ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Muhammad Ali Amin:
what does "scope="request"" means.


It means that the JSP will look for an instance of your object in request scope. If it doesn't find it there, it will instanciate on itself (providing you've set a class attribute) and bind it to reqest scope.

Looking at the generated servlet source will reveal all of this.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Originally posted by Ben Souther:


It means that the JSP will look for an instance of your object in request scope. If it doesn't find it there, it will instanciate on itself (providing you've set a class attribute) and bind it to reqest scope.

Looking at the generated servlet source will reveal all of this.


so you mean that i can access the same object in servlet by call getAttribute() method of servlet?

Now want to clear one more thing. JSP saves the object in request, servlet get it by calling getAttribute(), process/populate it and then ... will it automatically save it again in request/response attribute or i have to do it explicitly? to make the object available in next called JSP?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Unless you are forwarding the request, the bean will vanish after the request has been treated. This is usually a one way ticket, going from the servlet to the jsp.

Servlet, req.setAttribute --> forward/redirect --> JSP, getAttribute

Once the JSP has finished, the bean will vanish.
If you really need the information around several pages, maybe you should put it in session instead.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
No, you do not have to explicitly add the attribute again. You are dealing with object reference. so, when you update it, you are actually updating the object that you have a reference/handle to.

Originally posted by Muhammad Ali Amin:


Now want to clear one more thing. JSP saves the object in request, servlet get it by calling getAttribute(), process/populate it and then ... will it automatically save it again in request/response attribute or i have to do it explicitly? to make the object available in next called JSP?


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Thanks guys, i'll check it and will ask if i have any problem. thanks again
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Muhammad Ali Amin:
so you mean that i can access the same object in servlet by call getAttribute() method of servlet?


Just to be clear:
There is no getAttribute method to any of the servlet interfaces.

The getAttribute and setAttribute methods belong to the scoped objects available in a servlet app.
These include (HttpServletReqeust, HttpSession, ServletContext,).
 
Consider Paul's rocket mass heater.
 
subject: UseBean, scope question.
 
Similar Threads
Help with MVC implementation
jsp:useBean type requirements
share data from servlet back to jsp
JavaBean vs Post from a JSP
include directive confusion