• Post Reply Bookmark Topic Watch Topic
  • New Topic

pageContext cannot be resolved  RSS feed

 
Robert Pruett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pageContext, to my understanding, should be an implicit object capable of reading attributes from the page scope. My goal is to read the contents of a <c:set> tag from a scriptlet context at runtime. When I attempt to do so, I receive an error as documented below.

Code snippet from within JSP --

<c:set var="layer" scope="page" value="${layer +1}"/>
<%! Integer layer1 = Integer.parseint(pageContext.getAttribute("layer")); %>

Error output while rendering --

pageContext cannot be resolved

I have consulted Google and have read through these forums to the best of my ability and have found nothing specific enough to solve my problem. Any help or explanation that can be offered on this is very much appreciated!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robert Pruett:
My goal is to read the contents of a <c:set> tag from a scriptlet context at runtime.


May I ask why? Mixing and matching scriptlets with the JSTL and EL is an odd thing to do. Usually you are either writing JSP 1.x style pages with scriptlets, or JSP 2.0 style pageswith JSTL/EL and without scriptlets.

<%! Integer layer1 = Integer.parseint(pageContext.getAttribute("layer")); %>


The implicit variables are available only within the service() method of the generated servlet. You are trying to use it in a declaration scriplet. No can do.

You do realize that by using declaration scriplets in this manner that your page is no longer thread safe?
 
Robert Pruett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


The implicit variables are available only within the service() method of the generated servlet. You are trying to use it in a declaration scriplet. No can do.

You do realize that by using declaration scriplets in this manner that your page is no longer thread safe?


I do now! Thanks for the information. I managed to solve this by viewing the .java output to see that the declarations were coming through as static. I changed the <%! tags to <% and it came out perfectly. I had read somewhere that <%! was a declaration, so I just presumed to use it with each of my variable assignments without knowing exactly what it does. Lesson learned!

Also, an interesting tidbit I found while working through my next problem is that <c:set> is evaluated at runtime. I had expected the value I placed within to be recognized as an int, but it actually came out as a long, so I had to do this:

<% Integer layer2 = new Integer(((Long)pageContext.getAttribute("layer")).intValue()); %>

Thanks again for the help!
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, congratulations. You have now converted your EL variable into a local variable to use in scriptlet code.

I'm still asking my self WHY?
Almost any scriptlet code that should be in a JSP can be written in JSTL/EL.
If you can't write it in JSTL/EL it most probably shouldn't be on a JSP - rather in a servlet.
Alternatively you may need to rewrite your bean interfaces to make them more friendly to JSTL/EL

Ok, I am biased against scriptlet code, and aim for 0% scriptlet in every JSP page. Still it is quite achievable. Adding this conversion between EL and scriptlet code just adds complexity, making it even more unreadable.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robert Pruett:

I managed to solve this by viewing the .java output


That is always an excellent debugging step to take when something isn't acting as you expect, or just to learn the mechanics of JSPs.

P.S. I concur with Stefan. Try weaning yourself away from scriptlets. You'll be a happier person in the long run.
 
Peter Hu
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your issue is related to the scope of the objects. The page scope is just within the doGet or doPost method. Therefore pageContext can be accessed only from the page, i.e. the doGet or doPost method. When you declare a var using <@! > tag, it creates it in object scope that is outer doGet or doPost. An object scope var can not reference a local var inside a method.



[ May 26, 2006: Message edited by: Paul HG ]
[ May 26, 2006: Message edited by: Paul HG ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Paul HG",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!