Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q: Data type for JSP implicit objects

 
Anand Wadhwani
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q:

For JSP scopes of request and page, what type of object is used to store the attributes?

Select 1 correct option.
a HttpServletRequest and ServletContext respectively.
b ServletRequest and ServletConfig respectively.
c ServletRequest and PageContext respectively.
d HttpServletRequest and PageContext respectively.
e ServletConfig for both.

The answe to this question is c, but in my opinion it should be d.

The exaplination which is given for answer-c is as follows:
-----------------------------------------------------
Although HttpServletRequest is a subclass of ServletRequest, the feature of storing attributes is common to all servlets and jsps and not just HttpServlets and JSPs for HTTP.
-----------------------------------------------------

But as per JSP 2.0 specification data type for request and response is HttpServletRequest and HttpServletResponse respectively.

Please advise.
 
Kris Reid
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it would be HttpServletRequest

However JSP are also designed to use other prtocols then HTTP.
So that is probable why it is the more generic ServletRequest
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
ServletRequest has setAttribute() method.

the feature of storing attributes is common to all servlets and jsps and not just HttpServlets and JSPs for HTTP


This explanation is correct.

Although HttpServletRequest extends ServletRequest whenever the container implements the HttpServletRequest interface {which means makes an HttpServletRequest class} it won't give the implementation of setAttribute in it. Rather the implementation of the setAttribute will be in the ServletRequest class that the container makes.

Also it is very unlikely that some one will code a Servlet that is not HTTP but the feature is still there for others who do want to code a non Http Servlet.

Hope that helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic