• Post Reply Bookmark Topic Watch Topic
  • New Topic

A JSP Page is a subclass of ?

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking at an application that was written by an unknown party.
In a JSP, I see the code:
<%= this.getServletContext() %>
(why the unknown party didn't use the application variable, I do not know).
My development environment suggests that the method getServletContext() is not valid within a JSP page. It seems to suggest that a JSP is solely an implementation of the javax.servlet.jsp.HttpJspPage, which doesn't have such a method.
While the JSP Specification seems to make it clear that a translated JSP page implements the javax.servlet.jsp.HttpJspPage interface, it doesn't make it clear if it implements any other interfaces, such as javax.servlet.http.HttpServlet (and thus explaining the code written by the unknown party).
I am requesting suggestions and possible explanations regarding the code written by the unknown party, since my development environment and myself (unless I have overlooked something) both agree that this code is not written according to the specification.
All speculative or concrete answers are most appreciated.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony
I agree with the fact that we should not use 'this' in JSP and use application etc variables to access the servlet context etc as otherwise our code becomes dependent upon some JSP engines where it works fine and doesn't work on little different environment. I wonder if the third party in the question have made sure that the code provided works within their environment because as you suggested getServletContext() shouldn't be found if it really follows what JSP specification says.
Thanks
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,
I have observed that different JSP engines generates different Java code for the JSPs. In iPlanet and Tomcat or even different iPlanet versions there are differences b/w the way java files for JSPs are generated.
Once I get to office tomorrow I would check with my iPlanet to see what does it do for JSPs to see if they extend HttpServlet somehow or what...
But in anycase a JSP code should not be dependent upon specific JSP engines..
Regards
Maulin
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ultimately, it is important to remember that a JSP is translated into a Servlet. Therefore, it has all of the behavior available to Servlets. Of course it is not a good idea to explicitly use that behavior as the code in your post does.
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I am moving this thread to the JSP forum where it is a better fit. Please continue the discussion there.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excerpt from JSP 2.0 specification, section JSP1.1

A JSP page defines a JSP page implementation class that implements the semantics of the JSP page. This class implements the javax.servlet.Servlet interface.

Since getServletContext is defined by GenericServlet (and not the Servlet interface), it's not spec-compliant to rely upon it (even though HttpServlet is always derived from GenericServlet).
That said, I agree that using getServletContext rather than the more conventional 'application' variable is a bad idea (even if it works); if for no other reason than that it's confusing.
[Edited for clarity]
[ February 23, 2004: Message edited by: Bear Bibeault ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That said, I agree that using getServletContext rather than the more conventional 'application' variable is a bad idea; if for no other reason than that it's confusing.

Even so, that doesn't justify the use of getServletContext() since this interface does not define that method.
Unless I am advised otherwise, I am going to conclude that the code is broken, and dependant upon the container in which it was developed (Disclaimer: I didn't write it !)
Thanks for the suggestions.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. My post wasn't very clear. Didn't mean to link the Servlet interface with the correctness of the JSP code. I've edited my response to be more clear.
[ February 23, 2004: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!