a. Bean is an object of bean class defined by the user in JSP application.
b. The bean objects are used in the JSP application to store the data of the client temporarily at the server with more clarity.
c. All the bean objects are created by the JSP engine according to the JSP action tags given within the JSP files of the application.
d. Data of the client is stored into the bean object & retrieve from the bean object by the JSP engine according to JSP action tags given within the JSP files of the application.
We should follow the below steps to use bean object in the JSP application.
1. Define a bean class with a set of set & get methods according to functional requirements, all the bean classes must be stored in the package of classes folder.
2. We use the following action tag to create a bean object of some bean class
i.e. <jsp:useBean ...
3. We use the following JSP action tag to store the data of the client into the bean object
i.e. <jsp:setProperty ...
4. We use the following JSP action tag to get the data from the bean object.
i.e. <jsp:getProperty ...
5. All the bean classes which are defined by the user must implement java.io.Serializable interface, which is marker interface.
6. The scope of the bean object can be defined as application scope or session scope.
7. If the scope is application scope then the JSP engine will create only one bean object that can accessed by all clients.
8. If the scope is session scope then the JSP engine will create a separate bean object for every requested clients.
All the bean objects are automatically destroyed when we shut down the server.
The following example demonstrate how to use the bean object in the JSP application.
index.html
==========
<form action="first.jsp" method="get" >
Name : <input type="text" name="sname" ><br /><br />
Address : <input type="text" name="saddress" ><br /><br />
Age : <input type="text" name="sage" ><br /><br />
<input type="submit" name="submit" value="SUBMIT">
</form>
first.jsp
=======
This will receive the student form request with 4 parameters sname,saddress,sage and submit
welcome to this application.
<br /><br />
<jsp:useBean id="student" scope="session" class="beans.StudentEx" />
This JSP action tag will create a bean object of StudentEx class which is available in sub package of beans , the id name of the bean object is given as student and the scope of this bean object is session scope.
<jsp:setProperty name="student" property="name" param="sname" />
This JSP action tag will read sname parameter value from the request object and store that value into student bean object by executing the setName method of student bean object.
<jsp:setProperty name="student" property="address" param="saddress" />
This JSP action tag will read saddress parameter value from the request object and store that value into student bean object by executing the setAddress method of student bean object.
<jsp:setProperty name="student" property="age" param="sage" />
This JSP action tag will read sage parameter value from the request object and store that value into student bean object by executing the setAge method of student bean object.
<a href="second.jsp" >Show my details</a>
<br /><br />
Have a nice day.
second.jsp
=========
This will receive the request without any parameters when user clicks the anchoring message.
<center><b>
Your name : <jsp:getProperty name="student" property="name" />
This JSP action tag will get a value from student bean object by executing the getName method of student bean object.
<br /><br />
Your Address : <jsp:getProperty name="student" property="address" />
This JSP action tag will get a value from student bean object by executing the getAddress method of student bean object.
<br /><br />
Your Age : <jsp:getProperty name="student" property="age" />
This JSP action tag will get a value from student bean object by executing the getAge method of student bean object.
</b></center>
How do you feel your life at the Age of :
<jsp:getProperty name="student" property="age" />
<br /><br />
BYE BYE...
x
No comments:
Post a Comment