+ All Categories
Home > Documents > JAVA Beans

JAVA Beans

Date post: 13-Nov-2014
Category:
Upload: jajupreetam
View: 958 times
Download: 0 times
Share this document with a friend
35
www.interviewDuniya.com JAVA Beans JAVA Beans Designed By:
Transcript
Page 1: JAVA Beans

www.interviewDuniya.com

JAVA BeansJAVA BeansDesigned By:

Page 2: JAVA Beans

www.interviewDuniya.com

Java BeansJava BeansThe biggest problems with JSP is that they tend to get The biggest problems with JSP is that they tend to get messy when there is lots of Java code inside the JSP messy when there is lots of Java code inside the JSP page and that they mix Java with HTMLpage and that they mix Java with HTML

Often in projects a web designer writes the HTML and a Often in projects a web designer writes the HTML and a programmer writes the Java but JSP mixes them together.programmer writes the Java but JSP mixes them together.JSP pages are not easily reusable because they contain both JSP pages are not easily reusable because they contain both presentation code <HTML> and calculation code Java.presentation code <HTML> and calculation code Java.

Java beans provide a way to hide Java beans provide a way to hide mostmost of the Java of the Java code from the JSP page leaving the HTML and minimal code from the JSP page leaving the HTML and minimal Java in the JSP for presenting the results.Java in the JSP for presenting the results.The Java beans can be reused and naturally lend The Java beans can be reused and naturally lend themselves to creating libraries of reusable components themselves to creating libraries of reusable components ready to be plugged into the JSPready to be plugged into the JSP

Page 3: JAVA Beans

www.interviewDuniya.com

Another Java technology to Learn!Another Java technology to Learn!Not really, Not really, Java beans are just Java classesJava beans are just Java classes which follow a which follow a few few simplesimple rules.rules.It must have a zero parameter constructorIt must have a zero parameter constructorIt must have no public attributesIt must have no public attributesAll attributes should be accessed only via get and set All attributes should be accessed only via get and set methodsmethods

Get returns the value of the attribute and set takes a single Get returns the value of the attribute and set takes a single parameter which is the new value of the attribute.parameter which is the new value of the attribute.Get and set methods should be named with reference to the Get and set methods should be named with reference to the attribute they get or set. attribute they get or set. E.gE.g if the attribute is called if the attribute is called blobblob the getter the getter should be called should be called getgetBBloblob and the setter should be called and the setter should be called setsetBBloblobNote the first letter of the attribute is always a capital in thNote the first letter of the attribute is always a capital in the get e get and set methods name.and set methods name.

Page 4: JAVA Beans

www.interviewDuniya.com

A Simple BeanA Simple BeanAll attributes are not public

Has a constructor with no parameters

Has a method to get the value of the attribute

Has a method to set the value of the attribute

class class mybeanmybean{{privateprivate intint countercounter;;mybeanmybean()()

{{counter=0;counter=0;}}

publicpublic intint getgetCCounterounter()(){{return counter;return counter;}}

publicpublic void void setsetCCounterounter(int(int c)c){{counter=c;counter=c;}}

Package Package usefulbeansusefulbeans

Page 5: JAVA Beans

www.interviewDuniya.com

A Simple Bean (continued)A Simple Bean (continued)

An extra method. It is not required by the bean

specification

publicpublic void void incrementincrement()(){{countercounter=counter+1=counter+1;;}}

}}

Page 6: JAVA Beans

www.interviewDuniya.com

Accessing a beanAccessing a beanBeans can be created and accessed from JSP just like any Beans can be created and accessed from JSP just like any other Java class. You declare an instance, and then can use other Java class. You declare an instance, and then can use it in expressions and it in expressions and ScriptletsScriptlets::

<%! u<%! usefulbeans.mybeansefulbeans.mybean beaninstancebeaninstance=new =new usefulbeans.mybeanusefulbeans.mybean();(); %>%><%= b<%= beaninstanceeaninstance..getCountergetCounter(); %>(); %><% b<% beaninstanceeaninstance.set.setCounterCounter(0); %>(0); %>

However, the designers of JSP wanted to avoid HTML However, the designers of JSP wanted to avoid HTML scriptersscripters having to write or see any Java code in the JSP so having to write or see any Java code in the JSP so there is an alternative way to access beans...there is an alternative way to access beans...

Page 7: JAVA Beans

www.interviewDuniya.com

Instantiating a bean Instantiating a bean jsp:useBeanjsp:useBean

This is the name of the instance you will create

This is the name of the class you want to create an instance of

Its not quite as easy as you would hope to access a bean in JSP Its not quite as easy as you would hope to access a bean in JSP but but the syntax is independent of the language the bean is written inthe syntax is independent of the language the bean is written in....Lets use Lets use mybeanmybean as an exampleas an exampleFirst you need to create an instance of the bean using First you need to create an instance of the bean using jsp:useBeanjsp:useBean

<<jsp:useBeanjsp:useBean id="id="beabeanninstanceinstance"" cclass="lass="usefulbeans.mybeanusefulbeans.mybean"" />/>

Page 8: JAVA Beans

www.interviewDuniya.com

Instantiating a bean from JSPInstantiating a bean from JSPIn the JSP page the following code is generatedIn the JSP page the following code is generated

<%! u<%! usefulbeans.mybeansefulbeans.mybean beaninstancebeaninstance=new =new usefulbeans.mybeanusefulbeans.mybean();(); %>%>

In the In the ServletServlet the following code is generatedthe following code is generateduusefulbeans.mybeansefulbeans.mybean beaninstancebeaninstance=new =new usefulbeans.mybeanusefulbeans.mybean();();

Page 9: JAVA Beans

www.interviewDuniya.com

Instantiating a bean from JSPInstantiating a bean from JSP

When you instantiate a bean you can specify the scope of the beaWhen you instantiate a bean you can specify the scope of the bean.n.

scope="page"scope="page" this is the default it means that bean is not shared between this is the default it means that bean is not shared between JSP pagesJSP pages

scope="request"scope="request" places the bean in the places the bean in the HTTPServletRequestHTTPServletRequest object allowing object allowing it to be accessed by other JSP pages included or forwarded from it to be accessed by other JSP pages included or forwarded from the page.the page.

scope="session"scope="session" places the bean in the places the bean in the HTTPSessionHTTPSession object allowing the object allowing the bean to persist between requests from the clientbean to persist between requests from the client

scope="application"scope="application" places the bean in the places the bean in the ServletContextServletContext object allowing to object allowing to be accessed by all the instances of the JSP application as globabe accessed by all the instances of the JSP application as global shared l shared datadata

More about this in a few slides timeMore about this in a few slides time……

Page 10: JAVA Beans

www.interviewDuniya.com

Reading a Bean Attribute from JSPReading a Bean Attribute from JSP

This is the name of the instance you created

This is the name of the attribute in the bean you want to get

To get the value of a bean To get the value of a bean you use you use jjsp:getPropertysp:getProperty

<<jsp:getPropertyjsp:getProperty name="name="beaninstancebeaninstance"" property=property=""countercounter"/>"/>

In the JSP the following code is generatedIn the JSP the following code is generated<%= b<%= beaninstanceeaninstance..getCountergetCounter(); %>(); %>

In the In the ServletServlet the following code is generatedthe following code is generatedoout.println(beanInstance.getCounterut.println(beanInstance.getCounter());());

Page 11: JAVA Beans

www.interviewDuniya.com

Modifying a Bean Attribute from JSPModifying a Bean Attribute from JSP

This is the name of the instance you created

This is the name of the attribute in the bean you want to modify

The new value for

the attribute

To set the value of a bean To set the value of a bean you use you use jjspsp::setsetPropertyProperty

<<jsp:getPropertyjsp:getProperty name="name="beaninstancebeaninstance" property=" property=""countercounter" value="" value="00""/>/>

In the JSP the following code is generatedIn the JSP the following code is generated<% b<% beaninstanceeaninstance.set.setCounterCounter(0); %>(0); %>

In the In the ServletServlet the following code is generatedthe following code is generatedbbeaninstanceeaninstance.set.setCounterCounter(0);(0);

Page 12: JAVA Beans

www.interviewDuniya.com

JSP: Advantages and DisadvantagesJSP: Advantages and DisadvantagesUsing beans is a good idea even if you don't like or use the Using beans is a good idea even if you don't like or use the jsp:useBeansjsp:useBeans, , jsp:getPropertyjsp:getProperty and and jsp:setPropertyjsp:setProperty meachanismmeachanism

They allow you hide most of your code from the JSP page and They allow you hide most of your code from the JSP page and HTML scripterHTML scripterThey allow you to write reusable modules you can plug into you They allow you to write reusable modules you can plug into you JSPJSP

The The jsp:useBeansjsp:useBeans, , jsp:getPropertyjsp:getProperty and and jsp:setPropertyjsp:setProperty meachanismmeachanism is is supposed to be simpler for non programmers to understand althougsupposed to be simpler for non programmers to understand although h they pay for this by having to write more!they pay for this by having to write more!Beans can be accessed via standard JSP declarations, expressionsBeans can be accessed via standard JSP declarations, expressionsand and ScriptletsScriptlets ( <%!, <%= and <% ) but this looks much more like ( <%!, <%= and <% ) but this looks much more like programming!programming!It is probably not a good idea to mix both access methods togethIt is probably not a good idea to mix both access methods together. er. Choose the most appropriate one for a JSP page and stick to it.Choose the most appropriate one for a JSP page and stick to it.

Page 13: JAVA Beans

www.interviewDuniya.com

CompileCompile Error MessagesError MessagesSince Since JSPsJSPs are only compiled when loaded by the server the error messages are only compiled when loaded by the server the error messages you get back you get back when there is a compile problem are not very helpful. In this cawhen there is a compile problem are not very helpful. In this case I miss spelt the name of a se I miss spelt the name of a variable:variable:

500 Internal Server Error500 Internal Server Error<PRE> <B<PRE> <B>/kingj1/sessionscope.jsp:</B> >/kingj1/sessionscope.jsp:</B> javax.servlet.ServletExceptionjavax.servlet.ServletException:: <<jsp:getPropertyjsp:getProperty> on line > on line

'5' failed. '5' failed. java.lang.IllegalArgumentExceptionjava.lang.IllegalArgumentException: Bean was null: Bean was null at at allaire.jrun.jsp.JRunJSPStaticHelpers.getBeanProperty(JRunJSPStaallaire.jrun.jsp.JRunJSPStaticHelpers.getBeanProperty(JRunJSPStaticHelpers.java:292) ticHelpers.java:292) atatjrun__sessionscope2ejsp11._jspService(jrun__sessionscope2ejsp11.jrun__sessionscope2ejsp11._jspService(jrun__sessionscope2ejsp11.java:50) java:50) atatallaire.jrun.jsp.HttpJSPServlet.service(HttpJSPServlet.java:40) allaire.jrun.jsp.HttpJSPServlet.service(HttpJSPServlet.java:40) at at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1024) at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1024) at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:936) at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:936) at allaire.jrun.servlet.JRunNamedDispatcher.forward(JRunNamedDispatallaire.jrun.servlet.JRunNamedDispatcher.forward(JRunNamedDispatcher.java:34) cher.java:34) atatallaire.jrun.jsp.JSPServlet.service(JSPServlet.java:177) at allaire.jrun.jsp.JSPServlet.service(JSPServlet.java:177) at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1024) at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1024) at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:936) at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:936) at allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDiallaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:88) spatcher.java:88) atatallaire.jrun.servlet.JRunSE.service(JRunSE.java:1163) at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1163) at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1153) at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1153) at allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:330) atallaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:330) atallaire.jrun.http.WebEndpoint.run(WebEndpoint.java:107) at allaire.jrun.http.WebEndpoint.run(WebEndpoint.java:107) at allaire.jrun.ThreadPool.run(ThreadPool.java:272) at allaire.jrun.ThreadPool.run(ThreadPool.java:272) at allaire.jrun.WorkerThread.run(WorkerThread.java:75) </PRE>allaire.jrun.WorkerThread.run(WorkerThread.java:75) </PRE>

Page 14: JAVA Beans

www.interviewDuniya.com

Controlling the life time of a declarationControlling the life time of a declaration

With With ServletsServlets if you want a variable to last for more if you want a variable to last for more than a single request you have to create a session than a single request you have to create a session and add it to it or add the variable to the and add it to it or add the variable to the applications applications ServletServlet Context.Context.JSP allows you to specify the life time of a bean JSP allows you to specify the life time of a bean when you instantiate itwhen you instantiate it

Page 15: JAVA Beans

www.interviewDuniya.com

scope="page"scope="page"if we execute this code in several different web browsers and hiif we execute this code in several different web browsers and hit reload t reload several times what happens?several times what happens?

<%@page language="java" %><%@page language="java" %><HTML><HTML><BODY><BODY><<jsp:useBeanjsp:useBean id="id="mybeanmybean" class="" class="beans.mybeanbeans.mybean" scope="" scope="pagepage"/>"/>current value is <current value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /><% <% mybean.incrementmybean.increment(); %>(); %>new value is <new value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /></BODY></BODY></HTML></HTML>

Page 16: JAVA Beans

www.interviewDuniya.com

scope="page"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.

Page 17: JAVA Beans

www.interviewDuniya.com

scope="session"scope="session"if we execute this code in several different web browsers and hiif we execute this code in several different web browsers and hit reload t reload several times what happens?several times what happens?

<%@page language="java" %><%@page language="java" %><HTML><HTML><BODY><BODY><<jsp:useBeanjsp:useBean id="id="mybeanmybean" class="" class="beans.mybeanbeans.mybean" scope="" scope="sessionsession"/>"/>current value is <current value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /><% <% mybean.incrementmybean.increment(); %>(); %>new value is <new value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /></BODY></BODY></HTML></HTML>

Page 18: JAVA Beans

www.interviewDuniya.com

scope="session"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

Page 19: JAVA Beans

www.interviewDuniya.com

scope="application"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

Page 20: JAVA Beans

www.interviewDuniya.com

Sharing a Bean between JSP PagesSharing a Bean between JSP PagesPages included via Pages included via include include or or jspjsp:include :include may need to access some of may need to access some of the declarations in the main page in JSP this is quite easy to dthe declarations in the main page in JSP this is quite easy to do.o.

The bean is declared in the main page and also declared in any The bean is declared in the main page and also declared in any included pages that want to access it.included pages that want to access it.The included pages must declare the variable with the same id anThe included pages must declare the variable with the same id and d the same scope for the sharing to workthe same scope for the sharing to work

Setting the scope to Setting the scope to pagepage means that the each JSP page have a means that the each JSP page have a separate copy of the variable i.e. it is separate copy of the variable i.e. it is not sharednot sharedA special scope of A special scope of requestrequest allows you to allows you to shared a variable between shared a variable between pagespages which is which is recreated after each requestrecreated after each requestsessionsession scope allows you to scope allows you to shared a variable per clientshared a variable per client that that retains retains its value between requests from the same clientits value between requests from the same clientapplicationapplication scope allows you to create a single shared variable that is scope allows you to create a single shared variable that is shared between clientsshared between clients

Page 21: JAVA Beans

www.interviewDuniya.com

Main page, Page ScopeMain page, Page ScopeNow we will include a page from the main page and have a look atNow we will include a page from the main page and have a look at the the sharing. Both pages need to declare the bean to use itsharing. Both pages need to declare the bean to use it……

<%@page language="java" %><%@page language="java" %><HTML><HTML><BODY><BODY><<jsp:useBeanjsp:useBean id="id="mybeanmybean" class="" class="beans.mybeanbeans.mybean" scope=" scope=““pagepage"/>"/>current value is <current value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /><% <% mybean.incrementmybean.increment(); %>(); %>new value is <new value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /><<jsp:includejsp:include page="page="includescope.jspincludescope.jsp" />" /></BODY></BODY></HTML></HTML>

Page 22: JAVA Beans

www.interviewDuniya.com

Included Page, Page ScopeIncluded Page, Page Scope<%@page language="java" %><%@page language="java" %><<jsp:useBeanjsp:useBean id="id="mybeanmybean" class="" class="beans.mybeanbeans.mybean" scope=" scope=““pagepage"/>"/><BR><BR>hi from included file current value is <hi from included file current value is <jsp:getPropertyjsp:getProperty

name="name="mybeanmybean"" property="counter" />property="counter" /><% <% mybean.incrementmybean.increment(); %>(); %><BR><BR>hi from included file new value is <hi from included file new value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean""

property="counter" />property="counter" />

Page 23: JAVA Beans

www.interviewDuniya.com

Included Pages, Page ScopeIncluded Pages, Page Scope•Each page accessesa 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.

Page 24: JAVA Beans

www.interviewDuniya.com

Sharing between JSP Session ScopeSharing between JSP Session Scope

Now change the scope="page" to scope="session" in both the main Now change the scope="page" to scope="session" in both the main page and the included pagepage and the included page

<%@page language="java" %><%@page language="java" %><HTML><HTML><BODY><BODY><<jsp:useBeanjsp:useBean id="id="mybeanmybean" class="" class="beans.mybeanbeans.mybean" scope="" scope="sessionsession"/>"/>current value is <current value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /><% <% mybean.incrementmybean.increment(); %>(); %>new value is <new value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /></BODY></BODY></HTML></HTML>

Page 25: JAVA Beans

www.interviewDuniya.com

The included fileThe included file……

<%@page language="java" %><%@page language="java" %><<jsp:useBeanjsp:useBean id="id="mybeanmybean" class="" class="beans.mybeanbeans.mybean" scope=" scope=““pagepage"/>"/><BR><BR>hi from included file current value is <hi from included file current value is <jsp:getPropertyjsp:getProperty

name="name="mybeanmybean"" property="counter" />property="counter" /><% <% mybean.incrementmybean.increment(); %>(); %><BR><BR>hi from included file new value is <hi from included file new value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean""

property="counter" />property="counter" />

Sharing between JSP Session ScopeSharing between JSP Session Scope

Page 26: JAVA Beans

www.interviewDuniya.com

Sharing between JSP Session ScopeSharing 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.

Page 27: JAVA Beans

www.interviewDuniya.com

Sharing between JSP Application ScopeSharing between JSP Application Scope

Now change the scope="session" to scope="application" in both thNow change the scope="session" to scope="application" in both the e main page and the included pagemain page and the included page

<%@page language="java" %><%@page language="java" %><HTML><HTML><BODY><BODY><<jsp:useBeanjsp:useBean id="id="mymy" class="" class="beans.mybeans.my" scope="" scope="applicationapplication"/>"/>current value is <current value is <jsp:getPropertyjsp:getProperty name="name="mymy" property="counter" />" property="counter" /><% <% mymy.increment.increment(); %>(); %>new value is <new value is <jsp:getPropertyjsp:getProperty name="name="mymy" property="counter" />" property="counter" /></BODY></BODY></HTML></HTML> Unlike page and session scope,

with application scope if the name of the bean and the id are the same strange things happen!

Page 28: JAVA Beans

www.interviewDuniya.com

The included fileThe included file……

<%@page language="java" %><%@page language="java" %><<jsp:useBeanjsp:useBean id="id="mymy" class="" class="beans.mybeans.mybeanbean" scope=" scope=““pagepage"/>"/><BR><BR>hi from included file current value is <hi from included file current value is <jsp:getPropertyjsp:getProperty name="name="mymy""

property="counter" />property="counter" /><% <% mymy.increment.increment(); %>(); %><BR><BR>hi from included file new value is <hi from included file new value is <jsp:getPropertyjsp:getProperty name="name="mymy""

property="counter" />property="counter" />

Sharing between JSP Application ScopeSharing between JSP Application Scope

Page 29: JAVA Beans

www.interviewDuniya.com

Sharing between JSP Application ScopeSharing 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.

Page 30: JAVA Beans

www.interviewDuniya.com

Sharing between JSP Request ScopeSharing between JSP Request Scope

What happens if I want to have a bean that loses its value afterWhat happens if I want to have a bean that loses its value after each each request, is not shared but other clients but is shred with any irequest, is not shared but other clients but is shred with any included ncluded pages?pages?Page scope is no good the included pages do not share the beans Page scope is no good the included pages do not share the beans in in the main pagethe main pageSession scope is no good the bean retains its value between requSession scope is no good the bean retains its value between requests ests from the same clientfrom the same clientApplication scope is no good all the clients share the same beanApplication scope is no good all the clients share the same beanThe answer is The answer is requestrequest scope! The bean scope! The bean isis shared between shared between JSPsJSPs but but otherwise acts like page scope.otherwise acts like page scope.

Page 31: JAVA Beans

www.interviewDuniya.com

Sharing between JSP request ScopeSharing between JSP request Scope

Now change the scope="application" to scope="request" in both thNow change the scope="application" to scope="request" in both the e main page and the included pagemain page and the included page……

<%@page language="java" %><%@page language="java" %><HTML><HTML><BODY><BODY><<jsp:useBeanjsp:useBean id="id="mybeanmybean" class="" class="beans.mybeanbeans.mybean" scope="" scope="requestrequest"/>"/>current value is <current value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /><% <% mybean.incrementmybean.increment(); %>(); %>new value is <new value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean" property="counter" />" property="counter" /></BODY></BODY></HTML></HTML>

Page 32: JAVA Beans

www.interviewDuniya.com

The included fileThe included file……

<%@page language="java" %><%@page language="java" %><<jsp:useBeanjsp:useBean id="id="mybeanmybean" class="" class="beans.mybeanbeans.mybean" scope=" scope=““requestrequest"/>"/><BR><BR>hi from included file current value is <hi from included file current value is <jsp:getPropertyjsp:getProperty

name="name="mybeanmybean"" property="counter" />property="counter" /><% <% mybean.incrementmybean.increment(); %>(); %><BR><BR>hi from included file new value is <hi from included file new value is <jsp:getPropertyjsp:getProperty name="name="mybeanmybean""

property="counter" />property="counter" />

Sharing between JSP Request ScopeSharing between JSP Request Scope

Page 33: JAVA Beans

www.interviewDuniya.com

Sharing between JSP request ScopeSharing 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.

Page 34: JAVA Beans

www.interviewDuniya.com

Accessing a bean for a Accessing a bean for a ServletServlet

A java bean is simply a java class so you can A java bean is simply a java class so you can access it just the same as any other java classaccess it just the same as any other java classHowever, you don't get the advantages of However, you don't get the advantages of JSPsJSPsscoping. Instead the bean behaves like any scoping. Instead the bean behaves like any other variable:other variable:

If you make the bean an attribute of your If you make the bean an attribute of your ServletServlet it is it is available while the available while the ServletServlet is loaded.is loaded.If you make the bean a local variable it is only If you make the bean a local variable it is only available within the method it is created in.available within the method it is created in.

Page 35: JAVA Beans

www.interviewDuniya.com

What you should have learntWhat you should have learnt

How to How to writewrite a beana beanRequired methods: constructor, get and set methodsRequired methods: constructor, get and set methods

How to How to instantiateinstantiate a bean using a bean using <%!<%!useBeanuseBean

How page, session and application scope workHow page, session and application scope workHow to How to accessaccess a bean from JSPa bean from JSP

Using <% and <%=Using <% and <%=setPropertysetProperty getPropertygetProperty

How to How to includeinclude a beana beanHow page, request, session and application scope workHow page, request, session and application scope work


Recommended