Page Scope: The Bean Object Gets Disappeared As Soon
Page Scope: The Bean Object Gets Disappeared As Soon
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()
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.