Solved

<jsp:usebean> question

Posted on 2003-10-30
7
861 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
Comment Utility
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
Comment Utility
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
Comment Utility
-- This is equilaent to
-- <%
--   MyClassBean myBean = new MyClassBean();
-- %>

Not quite...  It's equivalent to:

<%
   MyClassBean myBean = new MyClassBean();
   pageContext.setAttribute( "myBean", myBean );
%>
 
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Good catch Tim ;-)
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
;-)
0
 
LVL 3

Author Comment

by:applekanna
Comment Utility
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
Comment Utility
>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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
constructor overloading 2 73
Set up Wildfly 10 8 318
love6 challenge java 31 128
equalIsNot  challenge 43 115
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
A procedure for exporting installed hotfix details of remote computers using powershell
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

15 Experts available now in Live!

Get 1:1 Help Now