• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do RequestDispatcher Methods Invoke Servlets, As Well As JSPs?

 
Bob Nedwor
hangman
Ranch Hand
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HeadFirst book has this question on p 210.

If a servlet is invoked using the "forward" or the "include" method of RequestDispatcher, which methods of the servlet's request object can access the request attributes set by the container? (Choose all that apply.)
a) getKookies()
b) getAttributes()
c) getRequestPath()
d) getRequestAttribute()
e) getRequestDispatcher()

The answer is b)

I have no idea what they are talking about because all of the discussion to this point in the book about these "forward" or "include" methods, only discuss invoking JSPs, not servlets. I read the entire dicussion of RequestDipatchers on p.203-205 and could not find the answer.

On p. 216 where all of the answers are provided, a reference to "(Servlets v2.4 65-66)" is given, but I have no idea what this means, as p. 65-66 have nothing about this and are not even in this chapter.

Thanks for any help with this.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're actually looking at this the wrong way round to be useful! In fact, a RequestDispatcher can most generally be used to invoke a servlet. Recalling that a JSP is translated into a servlet, this also means that the RD can invoke a JSP... so once you start looking at it this way it makes sense.

RDs are in fact more useful still, as they can be used to invoke resources in /WEB-INF/ and also static resources such as HTML documents and text files.
If a servlet is invoked using the "forward" or the "include" method of RequestDispatcher, which methods of the servlet's request object can access the request attributes set by the container? (Choose all that apply.)
a) getKookies()
b) getAttributes()
c) getRequestPath()
d) getRequestAttribute()
e) getRequestDispatcher()

The answer is b)
Looking at this from a "tactical" point-of-view, we know "forward" and "include" are the only two methods in a RequestDispatcher instance, so we can ignore that fact. Now, since we're talking about request attributes (and not, for example, request parameters) we know these attributes are always bound into the ServletRequest object for the current request (and only this object). We always retrieve them via getAttribute(String) on the ServletRequest instance. The fact we're using a RequestDispatcher makes no difference to this fact.

If you were confused in the exam, a process of elimination will rapidly tell you that (a) doesn't exist in ServletRequest (or any known subclass), (e) will just get you another RD and (c) doesn't exist (the closest similar method would probably be getRequestURI(), depending on exactly which bit of the path you want). This only leaves (b) which is correct, and (d) which doesn't exist.

Does that help?
 
Bob Nedwor
hangman
Ranch Hand
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles,
Thanks so much to take the time to explain something from a different author's book.

The main reason why I am not following is probably based on
a) my general lack of experience with Servlets, RDs, and Requests and
b) my improper assumption that the String parameter in the getRequestDispatcher() method needs to be a JSP, which now I realize is NOT the case, but can be a number types of resources.

So far in the book I could have sworn that the only examples of RD I have seen are only based on JSPs, not any other type of resource. I think that is what threw me.

So then based on your response, I guess we would somehow have a reference to the Servlet (target resource) and therefore that Servlet's Request object, and therefore the attributes of that Request using "getAttribute()".

I think I am at least somewhat clearer, but I just have to study harder and practice more with different RDs.
Thanks so much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic