• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

pagecontext in JSP

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I know that we MUST avoid scriptlets in JSP (but I forgot the reason why we should avoid them). can anybody remind me?
2. When do we (absolutely) need to use pagecontext in JSP? as far as I know, there's not a single object can't be accessed using the other scopes.
Thanks
 
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Spades wrote:1. I know that we MUST avoid scriptlets in JSP (but I forgot the reason why we should avoid them). can anybody remind me?
Thanks



Maintenance nightmare!
 
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

there's not a single object can't be accessed using the other scopes.


Um, no. If a scoped variable is in page context, it cannot be referenced via other scopes.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why maintenance nightmare? care to elaborate more? and what about question number 2? thanks
 
Bear Bibeault
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Spades wrote:why maintenance nightmare? care to elaborate more? and what about question number 2? thanks


Please do not ask multiple questions in future posts. It just makes the replies confusing and overlapping.

Scriptlets were a horrible kludge that filled a gap before a better system was devised. The are antiquated and obsolete and should not be used in any page written after 2001. That's 13 years ago.

I'm going to direct this topic towards the pageContext question at this point; please see any of the other dozens of posts about the abomination that is scriptlets for more discussion on why they have been obsoleted.

The remainder of this topic should focus on the pageContext question.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you please give me an example of such object? I think I haven't encountered a case where I must create an object with page scope. thanks
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maintenance nightmare? Okay.

1. Java is supposed to have static types, i.e. the compiler is supposed to check the types declared for variables before the class can be used. The JSP compiler doesn't do this until run time.

2. Exceptions thrown by Java code in a JSP include a stack trace referring to code which the programmer can't see, so the line numbers are incomprehensible.

3. JSPs containing scriptlets require you to match the opening and closing HTML tags and also the opening and closing braces in Java blocks. This is easy to get wrong, and it's hard to see when you have got it wrong.

4... I'm sure there's more if anybody really cares to go on. But I've noticed that there's always somebody who demands to use the old technology even though the new stuff was put there because of obvious faults in the old stuff. It's been 15 years and finally we don't see people demanding explanations of why they should use Swing now and not AWT. There was a whole decade in which people made up bogus reasons why we should still use Vector rather than ArrayList, but fortunately we don't see that any more. It should be the same with Java scriptlets -- it's been over a decade since they were made obsolete so it gets pretty tiresome pointing that out.
 
Bear Bibeault
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Spades wrote:could you please give me an example of such object? I think I haven't encountered a case where I must create an object with page scope. thanks



One example: scoped variables in page scope can be created explicitly though the use of <c:set>.

Another: scoped variables are created in page scope implicitly via the use of JSTL tags such as <c:forEach>.

In either case, these variables are not available through any scope but page.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul : yes, I know it's been quite a while, and that's why I've forgotten why it's not being used anymore. I'm a team leader now and I need to guide my juniors in the way of programming and when I say to them "don;t use scriptlets", there's bound for one or two newbie that would innocently ask "why?" and since it's been a while, I've forgotten why exactly scriptlets should be avoided. I just know that I've never even used that stuff, just saw them briefly and changed them to EL/JSTL ASAP, but never really understood why they were so evil. thanks for the reply anyway.

@bear: those are explicit objects. what about implicit objects?
thanks
 
Bear Bibeault
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already mentioned: the scoped variables created by tags like <c:forEach> exist in page scope.
 
Bear Bibeault
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed the specific wording of your question:

David Spades wrote:When do we (absolutely) need to use pagecontext in JSP?


You never need to explicit place scoped variables that you create on the page in page context, though it is a good practice. In reality, because a JSP (at least a modern one) is the "end of the line" so to speak in the request processing cycle, it could be considered moot whether explicitly-created scoped variables are placed in page or request context. Placing them in the session or application context is, of course, a different story.

Also, you practically never really need to explicitly use the pageContext reference when accessing a page-scoped variable as page context is the first searched. So ${fred} could be used rather than ${pageContext.fred}. I only use the latter when I want it to be explicitly clear that fred is coming from page context when there are multiple freds; but it's not technically necessary.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!