It's not a secret anymore!
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JSP - UseBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JSP - UseBean " Watch "JSP - UseBean " New topic

JSP - UseBean

Jayashree Mohan
Ranch Hand

Joined: Nov 23, 2005
Posts: 37
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

Joined: Jan 30, 2006
Posts: 16
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.

--<br />zoink,<br />Hennson<br /> <br />Certified-Java-Oracle-Moped
Shameen MK

Joined: Sep 28, 2000
Posts: 22

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

Joined: Nov 11, 2005
Posts: 127
Originally posted by Akshay Kiran:

so now what would be the output of
<jsp:useBean id=”name” class=”java.lang.String” />
<%= 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

Joined: Nov 15, 2004
Posts: 1026
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 "".

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
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.

It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
suresh guru
Ranch Hand

Joined: Sep 07, 2002
Posts: 38

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

Joined: Nov 11, 2005
Posts: 127
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

Joined: Aug 18, 2005
Posts: 220
I'm extremely sorry,

I got confused....

sorry for misleading

"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
I agree. Here's the link:
subject: JSP - UseBean
It's not a secret anymore!