• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scope of variable in scriptlet

 
Kanishka Ajwani
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I write <% String str = "hello"; %> in a JSP, a variable with the name "str" is created in method _jspService of the translated servlet. My question is that what will be the scope of this variable? According to me, since this variable will be visible to the entire page, its scope should be page. Am I thinking in the right direction?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kanishka,

My question is that what will be the scope of this variable?

You should see this as a local variable in the Servlet that is created from your jsp. It is available in the entire jsp-file (as it will be associated with one Servlet) but only accessible through an expression or another scriptlet:

It is however not accessible via EL:


Regards,
Frits
 
Kanishka Ajwani
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frits. Just to confirm that I got my concepts right,

<= String str = "hello"; %>

declares a local variable "str" in the jsp servlet. And

<jsp:useBean id = str class = "java.lang.String"/>

declares a page scoped variable with the name "str".

I initially thought these two should collide, because of similar names. But if the scope is different for both the variable, this code should work correctly. Am I right?
 
Gaurav Sagar
Ranch Hand
Posts: 97
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kanishka Ajwani wrote:Thanks Frits. Just to confirm that I got my concepts right,

<= String str = "hello"; %>

declares a local variable "str" in the jsp servlet. And

<jsp:useBean id = str class = "java.lang.String"/>

declares a page scoped variable with the name "str".

I initially thought these two should collide, because of similar names. But if the scope is different for both the variable, this code should work correctly. Am I right?


<%String str = "Hello"; %> creates a local variable str in the service method of the servlet formed after the translation of the JSP.
However, using the tag <jsp:useBean id....String"/> you create a bean that has the default scope of a page.

Regards,
Gaurav
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I initially thought these two should collide, because of similar names. But if the scope is different for both the variable, this code should work correctly. Am I right?

Yes, you are right.

It is always nice to find out what the servlet container makes out of a jsp: if you have a index.jsp tomcat will translate it into index_jsp.java
The file will be in a location like:
\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\WebAppRoot\org\apache\jsp\jsp\index_jsp.java

Look for the service method of the Servlet (created from your jsp)

for this piece of code:

The translation will be:


You can have a problem though if you define in your scriptlet a request attribute like this:

Now you have created a scoped variable that can be accessed by a <jsp:useBean> standard action. Just try it and see what happens.

Regards,
Frits
 
Kanishka Ajwani
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frits, the explanation was far beyond perfect . But this raises another question in my mind, can we access scoped attributes in scriplets?
I mean, can i write :


P.S: My computer is facing some issues, and is in the process of reformatting. Therefore, I am not able to try it out immediately, I am hoping to do it soon though. Meanwhile, it will be great if you can build my concepts.

Thanks.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But is this also valid?
<%= str%>

Well, you can, but not exactly like this.

Let me give another example where you can access it.
We have a object Person with one property name:

Then in your jsp file you will have the following:


You will get the output:
Frits Frits Frits



By the way: you can also get to a variable that is defined in the pageScope:

This will give you the same output!

Regards,
Frits
 
Kanishka Ajwani
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Frits.
To conclude:
1) A variable declared in scriptlet will not collide with a bean with the same name created using <jsp:useBean>
2) The bean can be accessed via EL and in scriptlets( using the getAttribute() method), but the bean cannot be accessed in expressions directly.

Thanks for your help..!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic