Professional Documents
Culture Documents
Designed By:
www.interviewDuniya.com
Java Beans
The biggest problems with JSP is that they tend to get
messy when there is lots of Java code inside the JSP
page and that they mix Java with HTML
Often in projects a web designer writes the HTML and a
programmer writes the Java but JSP mixes them together.
JSP pages are not easily reusable because they contain both
presentation code <HTML> and calculation code Java.
Java beans provide a way to hide most of the Java
code from the JSP page leaving the HTML and minimal
Java in the JSP for presenting the results.
The Java beans can be reused and naturally lend
themselves to creating libraries of reusable components
ready to be pluggedwww.interviewDuniya.com
into the JSP
Another Java technology to Learn!
Not really, Java beans are just Java classes which follow a
few simple rules.
It must have a zero parameter constructor
It must have no public attributes
All attributes should be accessed only via get and set
methods
Get returns the value of the attribute and set takes a single
parameter which is the new value of the attribute.
Get and set methods should be named with reference to the
attribute they get or set. E.g if the attribute is called blob the getter
should be called getBlob and the setter should be called setBlob
Note the first letter of the attribute is always a capital in the get
and set methods name. www.interviewDuniya.com
A Simple Bean
Package usefulbeans
class mybean All attributes are not public
{
private int counter;
mybean() Has a constructor with no
{ parameters
counter=0;
}
public int getCounter()
Has a method to get the value
{
of the attribute
return counter;
}
public void setCounter(int c)
{ Has a method to set the value
counter=c; of the attribute
}
www.interviewDuniya.com
A Simple Bean (continued)
public void increment()
{
counter=counter+1; An extra method. It is not
} required by the bean
} specification
www.interviewDuniya.com
Accessing a bean
Beans can be created and accessed from JSP just like any
other Java class. You declare an instance, and then can use
it in expressions and Scriptlets:
This is the name of the This is the name of the class you
instance you will create want to create an instance of
www.interviewDuniya.com
Instantiating a bean from JSP
In the JSP page the following code is generated
<%! usefulbeans.mybean beaninstance=new usefulbeans.mybean(); %>
www.interviewDuniya.com
Instantiating a bean from JSP
When you instantiate a bean you can specify the scope of the bean.
scope="page" this is the default it means that bean is not shared between
JSP pages
scope="request" places the bean in the HTTPServletRequest object allowing
it to be accessed by other JSP pages included or forwarded from the page.
scope="session" places the bean in the HTTPSession object allowing the
bean to persist between requests from the client
scope="application" places the bean in the ServletContext object allowing to
be accessed by all the instances of the JSP application as global shared
data
www.interviewDuniya.com
Reading a Bean Attribute from JSP
To get the value of a bean you use jsp:getProperty
www.interviewDuniya.com
Modifying a Bean Attribute from JSP
To set the value of a bean you use jsp:setProperty The new
value for
the
attribute
www.interviewDuniya.com
scope="page"
if we execute this code in several different web browsers and hit reload
several times what happens?
www.interviewDuniya.com
scope="page"
•Each client web
browser accesses a
separate copy of the
bean. There is no
sharing between
clients.
•The value of the bean
is lost after each
request.
www.interviewDuniya.com
scope="session"
if we execute this code in several different web browsers and hit reload
several times what happens?
<%@page language="java" %>
<HTML>
<BODY>
<jsp:useBean id="mybean" class="beans.mybean" scope="session"/>
current value is <jsp:getProperty name="mybean" property="counter" />
<% mybean.increment(); %>
new value is <jsp:getProperty name="mybean" property="counter" />
</BODY>
</HTML>
www.interviewDuniya.com
scope="session"
•Each client web
browser accesses a
separate copy of
the bean. There is
no sharing between
clients. So each
client counts
independently of the
others
•The value of the
bean is not lost
after each request.
So each client
counts from 0
separately
www.interviewDuniya.com
scope="application"
•Each client web
browser accesses a
single shared bean.
There is sharing
between clients. So
each client counts
dependently on the
others
•The value of the
bean is not lost
after each request.
So each client
counts from the
value the last client
used
www.interviewDuniya.com
Sharing a Bean between JSP Pages
Pages included via include or jsp:include may need to access some of
the declarations in the main page in JSP this is quite easy to do.
The bean is declared in the main page and also declared in any
included pages that want to access it.
The included pages must declare the variable with the same id and
the same scope for the sharing to work
Setting the scope to page means that the each JSP page have a
separate copy of the variable i.e. it is not shared
A special scope of request allows you to shared a variable between
pages which is recreated after each request
session scope allows you to shared a variable per client that retains
its value between requests from the same client
application scope allows you to create a single shared variable that is
shared between clients
www.interviewDuniya.com
Main page, Page Scope
Now we will include a page from the main page and have a look at the
sharing. Both pages need to declare the bean to use it…
<%@page language="java" %>
<HTML>
<BODY>
<jsp:useBean id="mybean" class="beans.mybean" scope=“page"/>
current value is <jsp:getProperty name="mybean" property="counter" />
<% mybean.increment(); %>
new value is <jsp:getProperty name="mybean" property="counter" />
<jsp:include page="includescope.jsp" />
</BODY>
</HTML>
www.interviewDuniya.com
Included Page, Page Scope
<%@page language="java" %>
<jsp:useBean id="mybean" class="beans.mybean" scope=“page"/>
<BR>hi from included file current value is <jsp:getProperty
name="mybean" property="counter" />
<% mybean.increment(); %>
<BR>hi from included file new value is <jsp:getProperty name="mybean"
property="counter" />
www.interviewDuniya.com
Included Pages, Page Scope
•Each page accesses
a separate copy of the
bean. All the other rules
for page scope apply.
So the pages can count
past 1 and each client
also gets separate
copies of the bean.
www.interviewDuniya.com
Sharing between JSP Session Scope
Now change the scope="page" to scope="session" in both the main
page and the included page
<%@page language="java" %>
<HTML>
<BODY>
<jsp:useBean id="mybean" class="beans.mybean" scope="session"/>
current value is <jsp:getProperty name="mybean" property="counter" />
<% mybean.increment(); %>
new value is <jsp:getProperty name="mybean" property="counter" />
</BODY>
</HTML>
www.interviewDuniya.com
Sharing between JSP Session Scope
The included file…
www.interviewDuniya.com
Sharing between JSP Session Scope
•The main and included
pages accesses the
same copy of the bean.
So the main and
included pages count
together.
•All the other rules for
session scope apply. So
the value of the bean
remains between
requests. But each
client gets a separate
copy of the bean.
www.interviewDuniya.com
Sharing between JSP Application Scope
Now change the scope="session" to scope="application" in both the
main page and the included page
<%@page language="java" %>
<HTML>
<BODY>
<jsp:useBean id="my" class="beans.my" scope="application"/>
current value is <jsp:getProperty name="my" property="counter" />
<% my.increment(); %>
new value is <jsp:getProperty name="my" property="counter" />
</BODY>
</HTML> Unlike page and session scope,
with application scope if the
name of the bean and the id are
the same strange things happen!
www.interviewDuniya.com
Sharing between JSP Application Scope
The included file…
www.interviewDuniya.com
Sharing between JSP Application Scope
•The main and included
pages accesses the
same copy of the bean.
So the main and
included pages count
together.
•All the other rules for
application scope apply.
So the value of the
bean remains between
requests and each
client accesses the
same bean.
www.interviewDuniya.com
Sharing between JSP Request Scope
What happens if I want to have a bean that loses its value after each
request, is not shared but other clients but is shred with any included
pages?
Page scope is no good the included pages do not share the beans in
the main page
Session scope is no good the bean retains its value between requests
from the same client
Application scope is no good all the clients share the same bean
The answer is request scope! The bean is shared between JSPs but
otherwise acts like page scope.
www.interviewDuniya.com
Sharing between JSP request Scope
Now change the scope="application" to scope="request" in both the
main page and the included page…
<%@page language="java" %>
<HTML>
<BODY>
<jsp:useBean id="mybean" class="beans.mybean" scope="request"/>
current value is <jsp:getProperty name="mybean" property="counter" />
<% mybean.increment(); %>
new value is <jsp:getProperty name="mybean" property="counter" />
</BODY>
</HTML>
www.interviewDuniya.com
Sharing between JSP Request Scope
The included file…
www.interviewDuniya.com
Sharing between JSP request Scope
•The main and included
pages accesses the
same copy of the bean.
So the main and
included pages count
together.
•All the other rules for
page scope apply. So
the value of the bean
lost between requests
and each client
accesses a different
copy of the bean.
www.interviewDuniya.com
Accessing a bean for a Servlet
www.interviewDuniya.com