• Post Reply Bookmark Topic Watch Topic
  • New Topic

using req:setAttribute  RSS feed

 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that populates a bean and sets it as a request attribute, then forwards to a frameset. I want the bean to be accessible on the JSPs that are invoked in the frames of the frameset. (Each request to populate a frame with a JSP is a new request, so the bean is not automatically available to these new request objects.)
Unfortunately, because our production environment load-balances across multiple Tomcats without session-pooling, I can't rely on setting my bean as a session attribute. I could parse the contents of the bean in the frameset jsp, then construct urls with appropriately formatted queryStrings for each frame, but this seems like a tedious and inelegant solution.
Can I use the JSTL req:setAttribute tag to set a bean as a request attribute on an outgoing request from a jsp? If not, is there any other way to set an object attribute from a jsp on an outgoing request?
Thanks!
 
Peter Bell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tricky problem!
Most app servers that do load balancing should support session affinity (i.e. each request on a session goes back to the same server). Try it. You'll probably need to use setAttribute on the session (not the request) because with a frame you'll be dealing in multiple requests, right?
Otherwise: think about popping the data in a sharable place (e.g. database) and appending a key parameter to the URLs you use.
Cheers, Peter
http://rimuhosting.com - JBoss EJB/JSP hosting specialists
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter B,
We don't have many rules at Javaranch, but our naming policy is one of them. Please read this policy and change your display name to comply with it if you wish to continue posting. Thanks.
You can change your display name here.
Also you are not eligible for the book giveaway if your display name does not comply with the policy.
 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have 8 Tomcats set up as standalone web servers; they're load-balanced, but because they're not on an AJP connector, they can't be configured for sticky sessions. (I'm trying to get our operations folks to change the configuration, but that's another story!)
My colleagues have suggested that I serialize the bean by overriding toString(), store it as the value of a hidden form field, and use form posts instead of urls to invoke my JSPs. That's what's been done elsewhere in our app.
Isn't there some better way?
 
agilan palani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John Holme,
I have the same problem of sharing the same request accross all the JSPs invoked by the frames of the frameset. I have the same constraint of not to use the session. The only point I differ from you is that u using java-beans to bundle the result, which will be displayed in the JSPs.
But iam using hashtable which is already serialized. But I dont understand clearly the <html> Hidden stuff that you used to pass the objects from one JSP to another through post..
Can you explain in detail please..
 
agilan palani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Can anyone help me with a solution?
John Holme, it would be great if you can reply
when u have time.
 
agilan palani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anyone who can help me?
 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agilan, here's the solution I'm using; not very elegant, but it works (basically, the object is serialized as a String, which is assigned as the value of a hidden form field; sub-frame documents are then invoked via javascript form.submit() using various actions):
1) serialize the data in the object to be passed via the request as a String of name:value pairs. in my case I also encrypt the String to hide proprietary data. the object has a public method that can be invoked from the JSP to write this String.
2) in the controller servlet (or one of its delegates), set my object as a request attribute.
3) in the frameset JSP, the jsp:useBean tag invokes the object in the request attribute. set up an html form consisting entirely of hidden fields: one field contains the String data from my object (using a call to myBean.toString(), or something similar); other fields contain each of the other parameter values that may be present in the request. for each sub-frame to be invoked, use javascript to set an approprite form action and target, then submit; do this repeatedly until each frame has been populated. your form actions might map to the controller servlet, or might invoke JSPs directly.
4) in whatever code is invoked via your form actions, provide a method that knows how to read the name:value pairs from the String which comprises your serialized object.
Again, not the most elegant or error-resistant system, but an adequate work-around when you're working in an environment that lacks support for Tomcat sessions. good luck!
 
agilan palani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million John,
If my understanding is correct,
You are converting a Object to a "String" and pass that "String" to a frame through a hidden element. In the receiving end, you convert back the string to the object. Am I correct?
 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agilan, that's right. You might call it a do-it-yourself serialization. Since html and javascript don't understand Java objects, this is the only way I know of to pass an object through an html page. Let me know if this works for you, or if you have any other ideas of how to do something like this. thanks! John
 
agilan palani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works. But this is not a generic solution to pass any object. However it solves the problem in hand. Would let u know if i have any other idea.
Thanks for your inputs
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!