Solved

<jsp:usebean> question

Posted on 2003-10-30
7
873 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apache-Tomcat Single Sign On with NTLM/Kerberos on Windows 2008R2 3 259
squirrelPlay java challenge 40 159
Java Script nested call 3 89
tomcat startup error 5 136
The world seems to conceive of a curious bubble separating IT from “the business.”  More so than just about any other pursuit in the commercial world, people think of IT as some kind of an island.
While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …

726 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