This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes double about change request header in HttpServletRequestWrapper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "double about change request header in HttpServletRequestWrapper" Watch "double about change request header in HttpServletRequestWrapper" New topic
Author

double about change request header in HttpServletRequestWrapper

Pham Hoai Van
Greenhorn

Joined: May 20, 2008
Posts: 15
I got this question:
which three are true about the HttpServletRequestWrapper class:
A. The HttpServletRequestWrapper is an example of Decorator pattern
B. The HttpServletRequestWrapper can be used to extend the functionality of a servlet request
C. A subclass of HttpServletRequestWrapper can not modify the default behaviour of getReader method
D. An HttpServletRequestWrapper may be used only by a class implementing the javax.servlet.Filter interface
E. An HttpServletRequestWrapper can not be used on the request passed to the method RequestDispatcher.include method
F. An HttpServletRequestWrapper may modify header of a request within an object implementing the javax.servlet.Filter interface

The given correct answer is A B & F. But i think it should be A B & D. I digged into the API and source code of HttpServletRequestWrapper but found noway to modify header of a request. Any comment or guide, pls.
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
Brief examination suggests it is just A,B. Unsure why you would want to modify request headers in the first place. Header manipulation occurs on the response side of the loop.

Regarding restricting HttpServletRequestWrapper usage to the Filter interface, another potential usage would be in doGet/doPost before performing an include/forward, so D is not correct. The only restriction is having access to the object being wrapped.
Ankur Gargg
Ranch Hand

Joined: Sep 11, 2011
Posts: 55

Hi

F is correct.
As you can override the getHeader() method in your class to return a modified value of header.


public String getHeader(String name) {
//get the request object and cast it
HttpServletRequest request = (HttpServletRequest)getRequest();

//if we are looking for the "username" request header
if("username".equals(name)) {
//loop through the cookies
Cookie[] cookies = request.getCookies();


//if cookies are null, then return null

if(null == cookies) {
return null;

}



for(int i=0; i < cookies.length; i++) {

//if the cookie's name is "username"

if("username".equals(cookies[i].getName())) {

//get its value and return it

String val = cookies[i].getValue();

return val;

}

}

}



//otherwise fall through to wrapped request object

return request.getHeader(name);
}

Using this you can return a new header altogether.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double about change request header in HttpServletRequestWrapper
 
Similar Threads
Which three are true about the HttpServletRequestWrapper class?
Question Regarding Filters...
mock exam question on filters
Question: HttpServletRequestWrapper Class
Mock Tests doubts