<jsp:useBean id="name" class="java.lang.String"/> <%= name %> 1) It wont compile 2) It is a valid jsp line and it will print the variable called name 3) it will compile but it will always produce null as output 4) it will work if u create a javabean class with only one variable of type java.lang.String The given answer is 2 what about 3?
<%! 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 [ November 14, 2002: Message edited by: trupti nigam ]
Trupthi, That was interesting. It is obvious from what you say is that once a variable is used in the use bean action it can't be used for printing. How about this... <%! String name="trupti";%> <jsp:useBean id="<=name>" class= "java.lang.String"/> and then trying to print it Does this print now?<%= name%> . I think it will. Also, please look at my other posting about request time attributes. Thanks, vasu
What a wonderful world!
Joined: Jun 21, 2001
vasu, I tried your code and it's giving me compilation error like.. org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: 2 in the jsp file: /jsp/testD.jsp Generated servlet error: C:\tomcat\work\Standalone\localhost\myDevelopment\jsp\testD$jsp.java:63: Missing term. java.lang.String <%= name%> = null; Did you try this code?? ^ thanks, Trupti
As Vedhas put it, what is the output when your print new String() Also, starring at this line should answer your question: <jsp:useBean id="name" class="java.lang.String"/> usebean tries to get the object from the defined scope, and if it does not find it, creates new of specified class. So <%=name%> is printing an empty string but not null ('cos name is not null). It is not visually obvious, and hence the confusion,i'd presume.. Rama
Rama, good point. You hit the nail on the head. Try adding the following code to the page:
It tells you exactly what's happening on the page. MS
Joined: Oct 10, 2002
Trupti, basically the line <%! String name="trupti";%> is at class level and it is not specifically set into any of the valid scopes(i mean session, application..) but the following line <jsp:useBean id="name" class="java.lang.String"/> is basically searching for the object name "name" belonging to the class "java.lang.String" in "page" scope. As the page scope did not have an attribute called "name" it instantiates a new string object. Thats the reason your "trupti" wasn't showing up. I guess the important point here is scope and yes now I understand why 3 is not right.