Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UseBean, scope question.

 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, i'll check it and will ask if i have any problem. thanks again
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic