• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what exactly is happening?

 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%! String name="trupti";%>
<jsp:useBean id="name" class="java.lang.String/>
<%= name%>
I tries the above code on tomcat4 but it doesn't print anything..
Then I commented the <jsp:useBean...%>line.
and the name variable was printed on the screen..
what is exactly happening here??
If I try to print the name variable before the use of <jsp:useBean..> the name "trupti" is printed on the screen but if I try to print it after the <jsp:useBean ..> Then nothing is printed on the screen..
like this..
<%! String name="trupti";%>
<%= name%> (name printed on the screen)
<jsp:useBean id="name" class="java.lang.String"/>
<%= "second time:" +name%>(name is not printed on the screen)
what exactly is happening..
thanks,
Trupti
 
See El
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to look at the TOMCAT_HOME/work/localhost/<your-web-app>/<your-jsp-name>$jsp.java
and everything should be clear.
It's is a container generated java source-code of your jsp file.
It should looks something like this
...
public class xxx extends HttpJspBase {
...
String name = "trupti"; //this is your first, instance declaration
....
public void _jspService(...) {
java.lang.String name = ....instantiate()...; //this is equivalent of your jsp:useBean, local declaration, because your use "class" attribute of useBean, container will create a String with no-args constructor
...
out.println(... + name + ... ); //as you are in local scope, local name will be printed (which is "")
}
}
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks see,
got it!!
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See,

In which case, if we declare the variable in a scriptlet whereby it becomes a local variable of the _jspService method, it should print the vale of the variable, right?
Vasu
Well, I tried doing this and I am getting an error that the variable is already declared in service method. From this I understand that once you use a variable as an id for the bean, you can't use the same variable to print it out. Am I right, people?
vasu
[ November 14, 2002: Message edited by: vasu maj ]
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vasu,
if you try the below code it will print the "temp" in the output.
<jsp:useBean id= "temp" class= "java.lang.String"/>
<% temp= "temp";
out.println(temp);%>
so what you are saying is correct..
Thanks,
Trupti
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trupthi,
I tried it and it is working. Thank you. Only it gives an error if I declare the variable first and then try to use the same in the usebean attribute.

vasu
 
kavitha yalamanchili
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is because it goes in the service method and u need to declare the variable before assigning a value.
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kavitha,
I am getting an error only if I am declaring it before using it in the usebean id. It says the variable is already declared. If I declare it after using it as use bean id and then try to print it, no probs.
Thanks,
vasu
 
kavitha yalamanchili
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here it goes...
if u have code like this..
<html><body>
<jsp:useBean id= "temp" class= "java.lang.String"/>
<% String temp = ""; %>
<% temp= "temp";
out.println(temp);%>
</body></html>
the jsp class has the following lines..
temp= (java.lang.String)
pageContext.getAttribute("temp",PageContext.PAGE_SCOPE);
if ( temp == null ) {
_jspx_specialtemp = true;
try {
temp = (java.lang.String) Beans.instantiate(getClassLoader(), "java.lang.String");
} catch (Exception exc) {
throw new ServletException (" Cannot create bean of class "+"java.lang.String");
}

pageContext.setAttribute("temp", temp, PageContext.PAGE_SCOPE);
}
}
.
.
.
.
String temp = "";

and if the jsp code is
<html><body>
<% String temp = ""; %>
<jsp:useBean id= "temp" class="java.lang.String"/>
out.println(temp);%>
</body></html>
the class has following lines..
String temp = "";
.
.
.
java.lang.String temp = null;
so you see.. u are trying to declare the same variable twice...and hence the server error..
:-)
hope that helps
 
kavitha yalamanchili
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Addition to the above reply..
from what I said it should throw server error whether u declare the variable below the useBean or after useBean..
I tried and I got error in both the cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic