|
|
NEW INTERVIEW QUESTIONS.COM
J2EE INTERVIEW QUESTIONS
JAVA INTERVIEW QUESTIONS DETAILS
NewInterviewQuestions.com - Home for World's Largest Interview Questions Website.
A D V E R T I S E M E N T
Question |
How do I instantiate a bean whose constructor accepts parameters using the useBean tag? |
Answer
|
Consider the following bean: package bar; public class FooBean { public FooBean(SomeObj arg) { ... } //getters and setters here } The only way you can instantiate this bean within your JSP page is to use a scriptlet. For example, the following snippet creates the bean with session scope: < % SomeObj x = new SomeObj(...); bar.FooBean foobar = new FooBean(x); session.putValue("foobar",foobar); %> You can now access this bean within any other page that is part of the same session as: &l;% bar.FooBean foobar = session.getValue("foobar"); %> To give the bean "application scope", you will have to place it within the ServletContext as: < % application.setAttribute("foobar",foobar); %> To give the bean "request scope", you will have to place it within the request object as: < % request.setAttribute("foobar",foobar); %> If you do not place the bean within the request, session or application scope, the bean can be accessed only within the current JSP page (page scope). Once the bean is instantiated, it can be accessed in the usual way: jsp:getProperty name="foobar" property="someProperty"/ jsp:setProperty name="foobar" property="someProperty" value="someValue"/
© NewInterviewQuestions.com
|
If you have the better answer, than send it to us. We will display your answer after the approval.
Rate the above answer. Help us to know about the answer.
Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.
A D V E R T I S E M E N T
|
|
|