You are on page 1of 35

JAVA Beans

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:

<%! usefulbeans.mybean beaninstance=new usefulbeans.mybean(); %>


<%= beaninstance.getCounter(); %>
<% beaninstance.setCounter(0); %>

„ However, the designers of JSP wanted to avoid HTML


scripters having to write or see any Java code in the JSP so
there is an alternative way to access beans...
www.interviewDuniya.com
Instantiating a bean jsp:useBean
„ Its not quite as easy as you would hope to access a bean in JSP but
the syntax is independent of the language the bean is written in..
„ Lets use mybean as an example
„ First you need to create an instance of the bean using jsp:useBean

<jsp:useBean id="beaninstance" class="usefulbeans.mybean" />

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(); %>

„ In the Servlet 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

More about this in a few slides time…

www.interviewDuniya.com
Reading a Bean Attribute from JSP
„ To get the value of a bean you use jsp:getProperty

<jsp:getProperty name="beaninstance" property="counter"/>

This is the name of the This is the name of the attribute in


instance you created the bean you want to get

„ In the JSP the following code is generated


<%= beaninstance.getCounter(); %>
„ In the Servlet the following code is generated
out.println(beanInstance.getCounter());

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

<jsp:getProperty name="beaninstance" property="counter" value="0"/>

This is the name of the This is the name of the attribute in


instance you created the bean you want to modify
„ In the JSP the following code is generated
<% beaninstance.setCounter(0); %>
„ In the Servlet the following code is generated
beaninstance.setCounter(0); www.interviewDuniya.com
JSP: Advantages and Disadvantages
„ Using beans is a good idea even if you don't like or use the
jsp:useBeans, jsp:getProperty and jsp:setProperty meachanism
„ They allow you hide most of your code from the JSP page and
HTML scripter
„ They allow you to write reusable modules you can plug into you
JSP
„ The jsp:useBeans, jsp:getProperty and jsp:setProperty meachanism is
supposed to be simpler for non programmers to understand although
they pay for this by having to write more!
„ Beans can be accessed via standard JSP declarations, expressions
and Scriptlets ( <%!, <%= and <% ) but this looks much more like
programming!
„ It is probably not a good idea to mix both access methods together.
Choose the most appropriate one for a JSP page and stick to it.
www.interviewDuniya.com
Compile Error Messages
„ Since JSPs are only compiled when loaded by the server the error messages you get back
when there is a compile problem are not very helpful. In this case I miss spelt the name of a
variable:
500 Internal Server Error
<PRE> <B>/kingj1/sessionscope.jsp:</B> javax.servlet.ServletException: <jsp:getProperty> on line
'5' failed. java.lang.IllegalArgumentException: Bean was null at
allaire.jrun.jsp.JRunJSPStaticHelpers.getBeanProperty(JRunJSPStaticHelpers.java:292) at
jrun__sessionscope2ejsp11._jspService(jrun__sessionscope2ejsp11.java:50) at
allaire.jrun.jsp.HttpJSPServlet.service(HttpJSPServlet.java:40) at
allaire.jrun.servlet.JRunSE.service(JRunSE.java:1024) at
allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:936) at
allaire.jrun.servlet.JRunNamedDispatcher.forward(JRunNamedDispatcher.java:34) at
allaire.jrun.jsp.JSPServlet.service(JSPServlet.java:177) at
allaire.jrun.servlet.JRunSE.service(JRunSE.java:1024) at
allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:936) at
allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:88) at
allaire.jrun.servlet.JRunSE.service(JRunSE.java:1163) at
allaire.jrun.servlet.JRunSE.service(JRunSE.java:1153) at
allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:330) at
allaire.jrun.http.WebEndpoint.run(WebEndpoint.java:107) at
allaire.jrun.ThreadPool.run(ThreadPool.java:272) at
allaire.jrun.WorkerThread.run(WorkerThread.java:75) </PRE>
www.interviewDuniya.com
Controlling the life time of a declaration
„ With Servlets if you want a variable to last for more
than a single request you have to create a session
and add it to it or add the variable to the
applications Servlet Context.
„ JSP allows you to specify the life time of a bean
when you instantiate it

www.interviewDuniya.com
scope="page"
„ 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="page"/>
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="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…

<%@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
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…

<%@page language="java" %>


<jsp:useBean id="my" class="beans.mybean" scope=“page"/>
<BR>hi from included file current value is <jsp:getProperty name="my"
property="counter" />
<% my.increment(); %>
<BR>hi from included file new value is <jsp:getProperty name="my"
property="counter" />

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…

<%@page language="java" %>


<jsp:useBean id="mybean" class="beans.mybean" scope=“request"/>
<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
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

„ A java bean is simply a java class so you can


access it just the same as any other java class
„ However, you don't get the advantages of JSPs
scoping. Instead the bean behaves like any
other variable:
„ If you make the bean an attribute of your Servlet it is
available while the Servlet is loaded.
„ If you make the bean a local variable it is only
available within the method it is created in.
www.interviewDuniya.com
What you should have learnt
„ How to write a bean
„ Required methods: constructor, get and set methods
„ How to instantiate a bean using
„ <%!
„ useBean
„ How page, session and application scope work
„ How to access a bean from JSP
„ Using <% and <%=
„ setProperty getProperty
„ How to include a bean
„ How page, request, session and application scope work

www.interviewDuniya.com

You might also like