• Post Reply Bookmark Topic Watch Topic
  • New Topic

EL and Variables in JSP

 
saps rao
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

For the code shown below,

<html>
<body
<% int one = 1;%>

${one}

</body>
</html>

the output is a blank page. Can anyone please tell me why can't EL access the variable that is declared in the scriptlet.

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EL is intended to replace scriplets in modern JSP pages. As such, they really have little to do with each other. Scriplet variables become local variables in the service method of the servlet created on behalf of your JSP. The EL has no access to these variables.

Rather, the EL operates upon scoped variables -- that is, objects places in one of the context scopes: page, request, session or application.

Typically scoped variables are created by the page controller for a JSP via the setAttribute() method of the appropriate scoped context.
 
saps rao
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for explanation. However, I thought the local variables declared using a scriptlet in a JSP will have a page scope. And hence EL can access them.

Isn't local variable a page scoped variable? If not can you please explain the differnce between a local variable and page scoped variable.

thanks, appreciate your help
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this question gets asked fairly frequently, it has been added to the JSP FAQ.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saps rao:
I thought the local variables declared using a scriptlet in a JSP will have a page scope. And hence EL can access them.
That is not correct.

Isn't local variable a page scoped variable?
No.

If not can you please explain the differnce between a local variable and page scoped variable.
Already explained. Scriplet code, including variables, become the method body of the servlet's service() method.

Page-scoped variables are objects stored in the page context.
[ May 24, 2007: Message edited by: Bear Bibeault ]
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EL variables have nothing to do with java variable: EL variables are scoped attributes. If you don't know what I'm speaking, please read jsp specs JSP.2.5

In your case EL variable "one" does not exists, which means an empty string. As a try (and ONLY as a try), you may add the following scriptlet code in order to get your page working:

pageContext.setAttribute("one", new Integer(one))

Thanks.
 
saps rao
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!