Solved

<jsp:usebean> question

Posted on 2003-10-30
7
863 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Images won't display in JSP pages 30 179
library class in java 1 90
session bean client machine 9 57
How to set default webapp for host 6 45
In the hope of saving someone else's sanity... About a year ago we bought a Cisco 1921 router with two ADSL/VDSL EHWIC cards to load balance local network traffic over the two broadband lines we have, but we couldn't get the routing to work consi…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now