Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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: 67650
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: 1512
  • 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 ......
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic