Page Scope: The Bean Object Gets Disappeared As Soon

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Aim:

Description: Beans and JSP:


Java beans are reusable components. We can use simple bean in the JSP. This helps us inkeeping the business logic separate from the presentation logic. Beans are used in the jsp pages as the instance of class. We must specify the scope of the bean in the JSP page. Here scope of the bean means the range time span of the bean for its existence in JSP. When the bean is particular scope its id is also available in that scope. There are various scopes using which the bean can be used in JSP page.

1. Page scope: The bean object gets disappeared as soon


as the current page discarded. The default scope for a bean in JSP page is a page scope.

2. Request scope: The bean object remains in existence


as long as the request object is present.

3. Session scope: A session can be defined as the a


specific period of a time the user spends browsing the site. For instance if you start Mozilla browser and starts browing the net for some amount of time and then quit the browser. Then the time spend by the user from starting to quitting a site is said to be one session.

4. Application scope: During application scope the bean


will get stored to ServletContent. Hence particular bean is available to all the servlets in the same web application.

Program: a. The following is a Textdemo.java bean is used. In this file the class textdemo is written. The purpose of this bean simply to get or set some message. Intially the variable msg is initialized to some string say Technical Publication. Just follow the steps that are given below for having bean and jsp communication.

Step 1. Write a simple java program for creating a simple bean. This bean is used to set the property msg. Create a folder named textdemo in location C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\BeansJsp\WEB-INF\classes and save the file by name Textdemo.java. It can be written as follows. Textdemo.java package textdemo; public class Textdemo { public static String msg; public Textdemo() { msg="Technical Publications"; } public String getMsg()

{ return msg; } public void setMsg(String msg) { this.msg=msg; } }

Step 2. Now write a simple jsp in which the textdemo bean is invoked. This jsp file can be created in some file named beanjsp. This folder can be saved at the path C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beansjsp Save the following jsp program with file name Beanjsp.jsp in above path. Beanjsp.jsp <html> <head><title>Use of java Bean in JSP</title> </head> <body> <h1>bean and JSP Demo:</h1>

<hr></hr>

<jsp:useBean id="msgbean" class="textdemo.Textdemo" scope="session"> <p>Getting the value for msg property From bean:</p> <strong> <jsp:getProperty name="msgbean" property="msg"/> </strong> <hr></hr> <jsp:setProperty name="msgbean" property="msg" value="Hello Path"/> </jsp:useBean> <p>setting the value for msg property From JSP:</p> <strong> <jsp:getProperty name="msgbean" property="msg"/> </strong> <hr></hr> </body> </html> Step 3. Open some web browser and type the path for the jsp file http://localhost:8081/BeansJsp/beanjsp.jsp Output:

b. The Jsp program which illustrate the use of bean in session scope and another show the usage of application scope. Step 1. Create a simple bean in which count is maintained and this count can be incremented by jsp page. Create a folder named counterdemo in C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\countjsp\WEB-INF\classes and save your bean program CounterDemo.java in that directory. CounterDemo.java package counterdemo; public class CounterDemo { public int count; public CounterDemo()

{ count=0; } public int getCount() { return count; } public void setCount() { this.count=count; } }

Step 2. Now create a jsp page which make use of bean created in step1 in session scope for that matter created a folder named CountJsp at the path C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps. Save the following program with name beanjsp1.jsp in countjsp. beanjsp1.jsp <html> <head> <title>use of Bean with session scope</title> </head> <body>

<jsp:useBean id="bean_id" class="counterdemo.CounterDemo" scope="session"/> <% bean_id.setCount(bean_id.getCount()+1); %> Your count is now <%=bean_id.getCount()%> </body> </html> Step 3: Open two different web browsers Mozilla Firefox and Internet Explorer and open the same JSP page in both the browsers, repeatedly refresh the JSP page in both the browsers and observe it.

Note that the count gets incremented independently because Mozilla browser is treated as one complete session and Internet Explorer is treated as another session for the bean object. Step 4: Now open the jsp file beanjsp2.jsp and edit the scope. Change it to application. The modified jsp is saved with a file name beanjsp2.jsp in the same location. beanjsp2.jsp <html> <head> <title>use of Bean with application scope</title> </head> <body> <jsp:useBean id="bean_id" class="counterdemo.CounterDemo" scope="application"/> <% bean_id.setCount(bean_id.getCount()+1);

%> Your count is now <%=bean_id.getCount()%> </body> </html> Step 5: Open two different browsers Mozilla Firefox and Internet Explorer and open the same JSP page in both the browsers, repeatedly refresh the JSP page in both the browsers and observe it.

Now note that in two different browsers, both the counts are getting incremented synchronously. That means for our bean there are two different sessions but the web application is same. Just switch between these two browsers and each time refresh the web page.

You might also like