• Post Reply Bookmark Topic Watch Topic
  • New Topic

useBeans tag-in Servlets-how to.

 
Matthew X. Brown
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For various reasons, I needed to replace a jsp with a servlet. Does anyone know how to map over the following:
<jsp:useBean id="bnRegister" class="com.foo.bar.UserBean" scope="request">
</jsp:useBean>
to a servlet. Is doing this:
com.foo.bar.UserBean bnRegister= new com.foo.bar.UserBean(); session.setAttribute(bnRegister);
really the same as this? I don't think it is. Also, how to change the scope values in the servlet? What if I want to chang the value to "page". Thanks for the help in advance!!!
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easiest way to prove to yourself what <jsp: useBean ... /> is equivalent to is to check out the resultant .java file that your application server creates for you.
Whenever you run a JSP page, your app server translates that into an XML file, then into a servlet, and then a compiled .class -- so you can easily check out what the app server translates the <jsp: useBean ... /> line into, 'cause that's what is actually being run.
------------------
- Jessica Bradley
HP Bluestone
 
Matthew X. Brown
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you-I've used that method quite a bit for debugging- thanks for reminding me of it......
But in addition, I guess I was wondering if there were equivalent methods in servlets for using beans and storing them.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to replace the use:
<jsp:useBean id="bnRegister" class="com.foo.bar.UserBean" scope="request">
As you have already figured out, in a servlet you have to take care of scope yourself.
com.foo.bar.UserBean bnRegister= new com.foo.bar.UserBean();
For session scope you would have to add it to the session with a name:
session.setAttribute("theRegister", bnRegister);
(but only if a "theRegister" didn't already exist);
For application scope, you would have to add it to the ServletContext (if one didn't already exist)
for "page" or "request" scope, you would make bnRegister a local variable in your doPost or doGet. If forwarding to another servlet - for request scope, add it to the request.
Bill

------------------
author of:
 
Matthew X. Brown
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill-that clears it up completely!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!