• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem while accessing page var using EL

 
Maya Dolas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Tomcat 5.5, JSP ver 2.0

I have a JSP with foll code

<%! String name = "Maya"; %>


<head>
</head>

<body>
The name is ${name}
</body>

When I execute i don't get the desired output, just get "The name is "

I tried doing this

${pageContext.page.name} however it throws an exception

How do i access var declared in JSP using EL?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't. The EL and scriptlets should never be used in the same JSP. Either use old-fashioned scriptlets, or use the JSTL/EL. Not both.
 
Sebastian Tincu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With EL's . operator you may access an attribute set in one of the four scopes or a bean's property. The String name variable, at translation time, just gets to be an instance variable in your servlet class. You may access it using a simple expression:


The error you get when using pageContext is because you don't have a JavaBean property named name defined into your servlet class. You need to define a getter for it.



You may set an attribute in one of the four scopes but just like below and then access it with the:


But this three are poor examples showing you how the things are working.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat: mixing scriptlets and JSTL/EL in the same page is an extremely poor practice. The JSTL and EL are meant to replace scriptlets on modern JSP pages. Mixing them just results in problems that you have encountered, and in adding a lot of garbage code into your page in order to bend over backwards to make the work together.

Pick one or the other and use it. Not both.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!