|
INTERVIEW QUESTIONS
J2EE
JAVA
DETAILS
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: &l;% 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: &l;% application.setAttribute("foobar",foobar); %> To give the bean "request scope", you will have to place it within the request object as: &l;% 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"/
|
|
|
Category |
Java Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7284 users |
Added on |
7/15/2011 |
Views |
66836 |
Rate it! |
|
|
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: &l;% 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: &l;% application.setAttribute("foobar",foobar); %> To give the bean "request scope", you will have to place it within the request object as: &l;% 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"/ Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Searching a String
|
View Answer
|
|
Connecting to a Database and Strings Handling
|
View Answer
|
|
What is the difference between Serializalble and Externalizable interface?
|
View Answer
|
|
What is the serialization?
|
View Answer
|
|
What are synchronized methods and synchronized statements?
|
View Answer
|
|
What is synchronization and why is it important?
|
View Answer
|
|
What is the purpose of finalization?
|
View Answer
|
|
What classes of exceptions may be caught by a catch clause?
|
View Answer
|
|
What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class
|
View Answer
|
|
What happens when a thread cannot acquire a lock on an object?
|
View Answer
|
|
What restrictions are placed on method overriding?
|
View Answer
|
|
What restrictions are placed on method overloading?
|
View Answer
|
|
How does multithreading take place on a computer with a single CPU?
|
View Answer
|
|
How are this() and super() used with constructors?
|
View Answer
|
|
What class allows you to read objects directly from a stream?
|
View Answer
|
|
What is the ResourceBundle class?
|
View Answer
|
|
What interface must an object implement before it can be written to a stream as an object?
|
View Answer
|
|
What is Serialization and deserialization?
|
View Answer
|
|
What are the Object and Class classes used for?
|
View Answer
|
|
Can you write Java code for declaration of multiple inheritance in Java ?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Java Interview Questions & Answers - Exam Mode /
Learning Mode
|