wood burning stoves 2.0*
The moose likes Servlets and the fly likes Can a JSP Have More Than One  jsp:useBean  Tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can a JSP Have More Than One  jsp:useBean  Tag?" Watch "Can a JSP Have More Than One  jsp:useBean  Tag?" New topic
Author

Can a JSP Have More Than One jsp:useBean Tag?

Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Can a JSP have more than one <jsp:useBean> tag? In other words, can I use more than one Bean in a JSP? With one HTML Form, you don�t even need an �ACTION=� attribute since the bean in the <jsp:useBean> will be automatically used and its �setter� methods called for every <INPUT> element present in the Form.
Suppose I have two HTML Forms in the JSP. Can I associate one Form with one bean (<jsp:useBean> tag) and another Form with another bean tag? How? It may not be allowed (at least in JSP 1.0).
No one at work knows and Sun�s documentation doesn�t say. The only thing that it does say in its JSP tutorial is that �ACTION=� can point to ANOTHER JSP. That�s one (indirect) way of doing the multi-form �thing� that I ask� Maybe.
In the meantime I may write some code to try�

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Hi
The problem you will have with 2 forms on a page with each form being associated with a bean is that each page must be 'submitted' to itself for the bean's setter methods to be processed. There isn't any way to submit two forms with a single button click. So if you click on submit 1 (that is defined in form 1) then only the name/value pairs from that form are sent along the 'queryString for GET / body stream for POST' the other form will never be processed, so, the associated setter methods will never be 'fired' off.
You can also associate more than one bean in a a JSP. One could be to repreent the form (and hence use the "*" getParameter tag to get all info from the form) and the other could be a custom 'bean' you made for your database or something like that.
Hope this helps.

SOURCE CODE should be SURROUNDED by "code" tags.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I experimented today and found out that I can use as many useBean tags (with "*" for the setProperty tag), and every one of those beans that has a setter method for the NAME attributes of the INPUT tag will be called with the value in the VALUE attribute of the INPUT field of the FORM.
And if you want the page to "remember" what happened in the previous FORM submission, one has to use
scope="session" in the useBean tag.
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Hi
Okay I want to clarify.
If I have two diffrent forms that call the same servlet. You are telling me that the bean will populate (call setter methods) and set the fields to the value passed from the form?
I would have to say that this would kick a$$.
Just today I was trying to process multiple forms on a page with one ASP and only the form that is passed by the submit buttom pressed is sent.
If Servlets can handle this it would be Darn cool!
Thanks for the info.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I don�t know ASP (nor have any interest; we are a Sun �IBM Java shop) but with JSP, if you have different beans defined at the top of the page with the useBean tag, then when you submit a form, all the names that match setter methods in ANY and all beans, will get set to whatever the VALUE is.
And if one bean does NOT have one property in the FORM but has another, it will just set this last one and will not complain about the others it doesn�t know about.
 
 
subject: Can a JSP Have More Than One jsp:useBean Tag?
 
Similar Threads
JavaScript & JSP Please help
doPost() processes - doGet() displays
"target" version of c:set
Some Brainbench JSP Q's.....Opinions required
Html:form