• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

USEBEAN-tag and servlet context - problems

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I have a JSP with this USEBEAN-tag:
<jsp:useBean id="MyCommandBean" class="mypackage.MyCommandBean" scope="session" />
The called servlet have this:
private javax.servlet.ServletContext cx;
mypackage.MyCommandBean MyCommandBean=null;
MyCommandBean = (mypackage.MyCommandBean)

cx.getAttribute("MyCommandBean");
if (MyCommandBean == null)
{
MyCommandBean=(mypackage.MyCommandBean)
java.beans.Beans.instantiate(
getClass().getClassLoader(),"mypackage.MyCommandBean");
}

request.setAttribute("MyCommandBean", MyCommandBean);
My question is this:
Why is MyCommandBean always null when the servlet is invoked from the JSP ?
I thought that the scope "session" made the Bean mentioned in the USEBEAN-tag stay in the servlet context.
Is there a way I can do a request.setAttribute - command in the JSP ?
My problem is that all the information in the CommandBean is lost when the JSP calls the servlet.
Can anyone help me ?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When your JSP invokes the servlet, the servlet receives a new request. So any attributes you have on the JSP's request are not visible to the servlet and vice versa.
What's the bigger picture of what you are trying to do?
hth,
bear
 
Erik Lindholm
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one quite big CommandBean that I use in the
JSPs to fill out values on the screen with USEBEAN and "<jsp:getProperty....." - tags.
The user change values in the fields and the new values are supposed to be shipped to the servlet
(Servlet-A) for being processed. The servlet will then redirect to another JSP that consist of a lot of the same fields + some new fields. This JSP will then call another servlet (Servlet-B). In some cases this scenario will continue in another 2 levels. Since I have a lot of the same fields in the JSPs, I find it best to use one big CommandBean.
I know that I can store all the variables in input-fields in the JSP of type="hidden", but this makes my JSPs very big. I would like to avoid that.
I have all together 4 JSPs and 4 servlets. I want all of these modules to be able to read this big CommandBean (with the same request values).
I learned from you that when a JSP calls a servlet there is made a new request and therefore my CommandBean is initialized.
Is there any techniques to achive what I want ?
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you just add it to the session and then accesing it from the other pages by getting it from the session?
 
Erik Lindholm
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is just what I need to do.
I am quite new to this.
Could you please give me some small code-examples on how to add a Bean to a session, and then retrieve it ?
I guess I need JSP-code to add a Bean to a session, and java-kode in the servlet to retrieve it from the session.
I would appriciate some small examples ......
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic