This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes jsp:useBean   tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "jsp:useBean   tag" Watch "jsp:useBean   tag" New topic
Author

jsp:useBean tag

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi everybody,
If I access the <jsp:useBean ...> tag for the same Bean object 5 times how many instances of that Bean are created?
eg. <jsp:useBean id="xx" scope="session" class="pac.Abean">
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

Hi all,
I have a problem with usebean tag in JSP. My jsp is unable to find the bean. m using tomcat and have placed the bean and jsp file in /examples/jsp.
my jsp file looks like this.....<html>
<head>
<title>
Color Demo Example
</title>
</head>
<body>
<!-- This page generates a series of rainbow colored letters -->
<!-- It uses plain method calls to the Bean -->
<%@ page language="java" %>
<%! char c = 0; %>
<jsp:useBean id="letterColor" scope="application" class="AlphabetCode" />
<%
for(int i=0;i<26;i++){
for(int j=0;j<26;j++){
c=(char)(0x41+(26-i+j)%26);
Character thisChar=new Character(c);
letterColor.setCharacter(thisChar.toString());
%>
<font color=<%=letterColor.getColor() %> >
<%=letterColor.getCharacter() %>
</font>
<%
}
%>
<br>
<%
}
%>
</body>
</html>
let me know where i have gone wrong.
nitin
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi Nitin,
I think u should place the page directive tag at the very beginning of ur file.

Can somebody please give the answer to my question?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by amit sanghai:

Hi everybody,
If I access the <jsp:useBean ...> tag for the same Bean object 5 times how many instances of that Bean are created?

jsp:useBean first attempts to locate the bean, and instantiates it only if it isn't found in the specified scope (see 2.13.1 of the JSP 1.1 spec). This is an absolute necessity for request- session- and application-scoped beans, otherwise they couldn't be referenced in multiple pages.
I'm not sure if you are allowed to use the same useBean more than once in a single page. The specification says, apparently unconditionally, that useBean will define "a scripting language variable with the given id in the current lexical scope of the scripting language". That could mean that two useBeans with the same id within the same lexical scope (read more or less: statement block) fail with a compilation error.
Did you try?
- Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Nitin Dubey:
My jsp is unable to find the bean.

"My JSP generates an error. Can someone wade through a hundred lines of unformatted code and tell me what the problem is?"
Sure Nitin, but, erm, what about quoting the error message you got? They usually tell you what the error exactly is.
From your description however I'm willing to bet it's a ClassNotFound error or similar. Your bean .class file needs to go into WEB-INF/classes, not in the same directory as the .jsp.
HTH
- Peter

[This message has been edited by Peter den Haan (edited January 22, 2001).]
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi Peter,
I tried using the same jsp:useBean tag wiht the same id and class names. It is giving a fatal error : missing resource in java.util.PropertyResourceBundle.

Thanks,
Bye.
Mohd. Yakub
Greenhorn

Joined: Nov 25, 2005
Posts: 18
Originally posted by amit sanghai:

Hi everybody,
If I access the <jsp:useBean ...> tag for the same Bean object 5 times how many instances of that Bean are created?

eg. <jsp:useBean id="xx" scope="session" class="pac.Abean">


it gives error,"duplicate local variable xx.
creating a bean is conditional,means it looks for
if(xx==null){
// create new one
xx= new pac.Abean();
}
if it is already there it won't create any other
Add to this,if we decleare the bean with the same id in other page,we can
access the values set in it.
hope ur question is answered..

Yakub
 
Consider Paul's rocket mass heater.
 
subject: jsp:useBean tag
 
Similar Threads
Session.removeAttribute doesnt work
Is there alternative method to access a bean
Strange error - Bean not found in scope
Can we have jsp:getProperty without jsp:useBean
jsp:setProperty