We can write our code in a single servlet file or a JSP page. However this is not a very good approach while making of web applications. Using this approach, we are performing every task i.e. displaying contents (Presentation logic), connecting with database (DB connectivity logic) and results Processing (Business Logic) in a single file.
For small applications, this approach looks good. However for large applications, its very difficult to maintain or to modify the code.
The solution to this problem is use of JavaBeans.
What is a JavaBean ?
==========================
JavaBean is not at all a new concept or technology. It is also a java class (similar to other standard or other java classes we create like Student class, Employee class...). However, JavaBeans are written in a special way.
Any java class that follows certain design conventions can be a JavaBean. These conventions are:
1) A bean class must have a zero argument constructor
2) A bean class should not have any public instance variables/attributes
3) Private values should be accessed through setters/getters
4) A bean class must be serializable
A very basic javaBean is given below that satisfies all these conventions:
public class MyBean implements Serializable {
private String name;
// zero argument constructor
public MyBean( ){
name = “”;
}
// standard setter
public void setName(String n) {
name = n;
}
// standard getter
public String getName( ) {
return name;
}
// any other method
public void print( ) {
System.out.println(“Name is: ” + name);
}
} // end Bean class
Using scriplets, we can write everything (presentation, connectivity, logic..) in a single JSP file. But this makes it difficult to understand and to enhance its functionality. Here comes the use of JavaBeans.
We can move most of our java related code from JSP files to these JavaBeans.
These JavaBeans can be easily reused and composed together in an application.
JSP Action Elements
==========================
Consider that we have created 2 beans (classes):
1) connectBean: written JDBC connectivity code into it.
2) LogicImplBean: written all logic implementation code into it.
Now the only thing remaining is to access these beans into our JSP files.
There are certain JSP action elements (in the form of tags) that allow us to work with JavaBeans in our JSP pages.
1) connectBean: written JDBC connectivity code into it.
2) LogicImplBean: written all logic implementation code into it.
Now the only thing remaining is to access these beans into our JSP files.
There are certain JSP action elements (in the form of tags) that allow us to work with JavaBeans in our JSP pages.
JSP useBean Action Element
It is used to obtain a reference to an existing JavaBean object by specifying id (name of object) and scope in which bean is stored. If a reference is not found, the bean is instantiated. The format of this action element is:
<jsp:useBean id = “name”
scope = “page|request|session|application”
class=“package.Class ” />
The id attribute specifies the name of the JavaBean object that is also used for later references. The scope attribute can have one possible value out of page, request, session and application. If this attribute is omitted, the default value of scope attribute is page. The class attribute specifies the type of object is going to be created.
jsp:useBean is being equivalent to building an object in scriptlet. For example to build an object of MyBean using scriptlet is:
<%
MyBean m = new MyBean( );
%>
JSP setProperty Action Element
This is used to set or change the property value of the specified bean. String values are converted to types of properties by using the related conversion methods. The format of this action element is:
<jsp:setProperty name = “beanName or id”
property = “name”
value =“value” />
The name attribute should match the id given in jsp:useBean. The property
attribute specifies the name of the property to change and the value attribute specifies the new value.
jsp:setProperty is being equivalent to following code of scriptlet. For example to change the name property of m (instance of MyBean) using scriptlet is:
<%
m.setProperty(“abc”);
%>
JSP getProperty Action Element
Use to retrieves the value of property, converts it to String and writes it to output stream. The format of this action element is:
<jsp:getProperty name = “beanName or id”
property = “name” />
jsp:getProperty is being equivalent to following code of scriptlet. For example to retrieve the name property of m (instance of MyBean) followed by writing it to output stream, scriptlet code will look like:
<%
String name = m.getName( );
out.println(name);
%>
Sharing Beans & Object Scopes
==========================
We can store beans in four different locations, depending on the value of the optional scope attribute of jsp:useBean. The scope attribute has the following possible values: page, request, session and application.
1) Page
This is the default value of scope attribute. It indicates, in addition to being bound to local variable, the bean object should be placed in the pageContext object. The bean’s values are only available and persist on JSP in which bean is created.
Beans created with page scope are always accessed by jsp:getProperty, jsp:setProperty, scriptlets or expressions in the same page.
2) Request
This value signifies that, in addition to being bound to local variable, the bean object should be placed in ServletRequest object for the duration of the current request. In other words, until we continue to forward the request to another JSP/servlet, the beans values are available.
3) Session
This value means that, in addition to being bound to local variable, the bean object will be stored in the HttpSession object associated with the current request. We already know, object’s value stored in HttpSession persists for whole user’s session.
4) Application
This very useful value means that, in addition to being bound to local variable, the bean object will be stored in ServletContext. The bean objects stored in ServletContext is shared by all JSPs/servlets in the same web application.
Good job Sushant.. keep goin!!
ReplyDeleteThanks :)
Delete