• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getServletContext method

 
Rasika Chitnis
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found that in the book SCWCD exam study kit, it is mentioned at several places that getServletContext method belongs to Servlet interface. I don't think it is correct. this method belongs to ServletConfig interface and GenericServlet implements it since it implements ServletConfig interface.
Any servlet extending from GenericServlet or HttpServlet will inherit it, but not a jsp page whose lineage is Servlet -> JspPage -> HttpJspPage.
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is an error and listed in this errta link
http://www.manning.com/getpage.html?project=deshmukh&filename=errata.html
Page 190, Third paragraph:
The text in bold is missing. The pragraph should read as follows:
Because the JSP page is converted into a servlet, we can call all the methods in a JSP page that we can call on a servlet. Hence we can get the ServletContext object via getServletConfig().getServletContext(). However, in tomcat and in many other containers, the base class of the page's generated class also implements the ServletConfig interface. Thus, in both methods, jspInit() and jspDestroy(), we get the ServletContext object by using the method getServletContext(), which is actually defined in the javax.servlet.ServletConfig interface.

This means, just writing getServletContext() inside a jsp file is not a portable way!. Just beacase Tomcat's jsp implementation base class so happened to be a HttpServlet (which implemnets HttpJspPage), this code works. Othewise it will break.
regards,
Maha Anna
 
Rasika Chitnis
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply, especially the errata for the book. I was not aware that it exists. thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic