Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

USEBEAN-tag and servlet context - problems  RSS feed

 
Erik Lindholm
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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 ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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 ?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • 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
  • 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 ......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!