• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP - UseBean

 
Jayashree Mohan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following staments are correct about the following jsp lines:

<jsp:useBean id=�name� class=�java.lang.String� />

<%= name %>

1) It won't 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 the output.
4) It will work if you create a javabean class with only one variable of type java.lang.String.

The answer in the mock exam was 2) .
How is this possible??
 
Bernd Hennson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayashree,
the usebean tag represents nothing else but:

String name = new String();

So <% out.println(name); %> aka <%= name %>
or some J2SEish System.out.println(name); shouldn't be too much of a problem.
 
Shameen MK
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayashree,

You are right. I find it confusing too since the java.lang.String class does now have an implementation to get the value for variable "name". I am not sure from where will it get the value of the variable 'name'.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Akshay Kiran:

so now what would be the output of
<jsp:useBean id=”name” class=”java.lang.String” />
<%= akshay? %>

output:
akshay

as simple as that, the exam does a lot of playing on variable names and strings, and the first part in passing it obviously to see through all these tricks


You are wrong, or my container is wrong. It will not output akshay, there will be an error, unless you put it inside the quotes.

Answer 2 is correct. Having this code:



There are three possibilities.

1). Attribute "name" exists in page scope.
2). Attribute "name" exists in any scope other than page scope.
3). Attribute "name" doesn't exist at all.

Then what happens:

1). <jsp:useBean> will use this attribute, expression will evaluate to the value of the attribute
2). <jsp:useBean> will create bean and we'll get blank page
3). <jsp:useBean> will create bean and we'll get blank page

This is how it work in my container. I'm also wondering why it doesn't print null in 2) and 3), just there's a blank page.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only requirement for a class to be used as a bean in a JSP page is that the class should have a public no-args constructor, so the the JSP engine can instantiate the class.

The String class has a no args constructor, so an empty string object is created.

jsp:useBean can create can object in the given scope and set it in the specified id.

Now you are printing the id variable and so the page prints "".
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm, I think that these are not the same "name"

This looks for (and possibly creates) an attribute in the pageContext instance :


=> if you wanted to access that with a JSP scriptlet, you would write something like:



now this is just a JSP expression :

it will just translate to something like


and name would need to be an instance variable in order to work.
 
suresh guru
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm,

Any way, the new bean ( String object will be created if one is not available.. but with which value it will be initialized?, empty string ("") or null?
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Vishnu Prakash said, beans are created using no-arg constructor, and according to API, new String() - initializes a newly created String object so that it represents an empty character sequence.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm extremely sorry,

I got confused....

sorry for misleading
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic