This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Best Strategies of Passing output data to web tier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Best Strategies of Passing output data to web tier " Watch "Best Strategies of Passing output data to web tier " New topic
Author

Best Strategies of Passing output data to web tier

Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
I am new to Struts and have the following observation and question. I count no fewer than 3 ways of passing data from the business tier to a response jsp page. What is the best way of doing this?
For example: let's say my application receives 2 parameters from a "welcome.jsp" page (via the appropriate ActionForm, call it "welcomeForm"), uses those 2 params in an sql query, and returns the query results to an "answer.jsp"
It seems that,
1. one can set attributes on the HttpRequest using the query results and reference the request from "answer.jsp"
2. create a Data Transfer Object (DTO) JavaBean, populate this bean with the query results, and reference this bean in "answer.jsp"
3. Somehow set params on the "welcomeForm" with the query results and reference the updated form from "answer.jsp"

Are all of these methods valid? Are some better than others? (is, for example, #2 always best? why not?)
Bill Dudney
Author
Ranch Hand

Joined: Sep 05, 2003
Posts: 234
Hi Benjamin,
The best way to do this roughly like this;
1) In your 'welcome' action which is invoked from your form on the welcome.jsp page you would send the parameters to a Business Delegate that would return the list of 'answer' objects (i.e. value objects or data transfer objects).
2) put these answer objects as a list into the ActionForm that will sit behind the answer.jsp page.
3) In your answer.jsp page use the values in the action form to populate a table. Something like this

Hope this helps.
[ September 23, 2003: Message edited by: Bill Dudney ]

TTFN,<br /> <br />-bd-<br /> <br /><a href="http://www.amazon.com/exec/obidos/ASIN/0471449156/qid=1064343733/sr=2-1/ref=sr_2_1/002-8375300-3666449" target="_blank" rel="nofollow">Jakarta Pitfalls</a> | <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0471146153/qid=1064343733/sr=1-2/ref=sr_1_2/002-8375300-3666449?v=glance&s=books" target="_blank" rel="nofollow">J2EE AntiPatterns</a> | <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0471462071/qid=1064343733/sr=1-3/ref=sr_1_3/002-8375300-3666449?v=glance&s=books" target="_blank" rel="nofollow">Mastering JavaServer Faces</a> | <a href="http://bill.dudney.net/roller/page/bill" target="_blank" rel="nofollow">Blog</a> | <a href="http://www.jroller.com/page/BillDudney" target="_blank" rel="nofollow">Eclipse Blog</a> | <a href="http://www.sourcebeat.com/TitleAction.do?id=2" target="_blank" rel="nofollow">Eclipse 3 Live</a>
Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
Thanks much for the info.
So you wouldn't reference the DTO bean from the jsp via usebean?
Would the ActionForm referenced by answer.jsp be one and the same ActionForm which had originally received the input params from the html page--here "welcomeForm"?
Bill Dudney
Author
Ranch Hand

Joined: Sep 05, 2003
Posts: 234
Hi Benjamin,
You could put the dto's into the session then reference them via usebean but I think its better to not do that but instead put the into answerForm (i.e. answerForm.setAnswers(myListOfAnswers)) then the answer.jsp would reference answerForm in the typical way.
A sort of sequence 'diagram' follows
1) welcomAction is invoked with the welcomeForm
2) you extract the parameters required by your biz delegate
3) you send the parameters to the biz del and get the answer list
4) you find the answerForm (this can be tricky, its referenced in my Jakarta Pitfalls presentation on my blog if you'd like more info).
5) you stuff the answer list intot he answerForm
6) you return the action forward that will take you to the answer.jsp page
7) the answer.jsp page uses the answer form to get at the newly placed list of answers
I hope this makes sense
Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
Thanks, Bill,
Your method indeed makes sense. I'll try it.
Also, I found out another way it might be done: instantiate an ordinary JavaBean,
pass the bean to the delegate,
populate the bean in the delegate,
attach THIS ENTIRE BEAN (not simply the values it holds) to the HttpRequest object,
access this javaBean in the jsp via the useBean directive
 
jQuery in Action, 2nd edition
 
subject: Best Strategies of Passing output data to web tier
 
Similar Threads
Paging results back to browser
spaces between categories in cewolf barchart
Test 484 ... LONG POST
bundle, in one config file, web parameter names?
JSP based pages get confused when user clicks "Submit" more than once, before data retrieved