Solved

<jsp:usebean> question

Posted on 2003-10-30
7
870 Views
Last Modified: 2006-11-17
we have <%@useBean id = "myBean" class = "com.junk.MyBeanClass" scope = "page">  
in a jsp called "MyFirstJSP.jsp"

This is equilaent to
<%
MyClassBean myBean = new MyClassBean();
%>

My question is in the scope attribute.

page "the bean is there as long as the page is active"
---> Does this mean the scope of the object is throughout the page and when the user moves from the page the object is not avaioable

request "the bean is avaialbale for the target of the page ..."
--->is the scope avaibale for the request , which means if the page is forward to nect page , i need not create a new object but use the existing object

session "the bean is accesibke from any jsp page participate in the session"
---> I can use the objecy anywhere i want in all the pages for that particular session. ie I can load user info such as name , login , passowrd in a bean and call it anywhere without making a new object in each page.

ie in MySecondJSP.jsp I can use  "myBean.getInfo() " without ceating the obejct in the second page

appication "the bean ia avaibale throuhg the appication"
---> I can call the object of the bean anywhere i want an example is to store some name values pais similar to servelt context.

Thx for ur help :)

0
Comment
Question by:applekanna
7 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9656162
Well,
You have pretty much answered your own question in the question inself....!! whats there for us to add??
0
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9656713
page - yes
request - yes
session - yes
application - yes (application isn't *similar* to servlet context, it *is* servlet context)

;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9657032
-- This is equilaent to
-- <%
--   MyClassBean myBean = new MyClassBean();
-- %>

Not quite...  It's equivalent to:

<%
   MyClassBean myBean = new MyClassBean();
   pageContext.setAttribute( "myBean", myBean );
%>
 
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 15

Expert Comment

by:jimmack
ID: 9657439
Good catch Tim ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9657447
;-)
0
 
LVL 3

Author Comment

by:applekanna
ID: 9658894
Hmm..

In my app, I have user info , contact info and other info as session beans and get them in each page. SO I can now replace them with javs use bean .

How is it advantageous - session attributes and/or  use bean

Thanks.
0
 
LVL 27

Expert Comment

by:rrz
ID: 9661647
>How is it advantageous - session attributes and/or  use bean
They are the same.
If in JSP you have the following  

      <jsp:useBean id="greeting" class="java.lang.String"/>
      <% greeting = "hello"; %>
      <%=greeting%>    

then Tomcat will translate it  to the following. (look in Tomcat's work folder)


   java.lang.String greeting = null;
synchronized (pageContext) {
    greeting = (java.lang.String) pageContext.getAttribute("greeting", PageContext.PAGE_SCOPE);
    if (greeting == null){
    try {
         greeting = (java.lang.String) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "java.lang.String");
        } catch (ClassNotFoundException exc) {
            throw new InstantiationException(exc.getMessage());
        } catch (Exception exc) {
            throw new ServletException("Cannot create bean of class " + "java.lang.String", exc);
        }
          pageContext.setAttribute("greeting", greeting, PageContext.PAGE_SCOPE);
        }
}
out.write("\r\n");
greeting = "hello";       out.write(" \r\n");
out.print(greeting);
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
constuctor calling other constructor 2 78
Multiple Images Upload [JSP Web-application] 3 41
instanceof  operator in java 26 95
jsp login check 12 42
The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
This article explains the steps required to use the default Photos screensaver to display branding/corporate images

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question