aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doFilter() method parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doFilter() method parameters" Watch "doFilter() method parameters" New topic
Author

doFilter() method parameters

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
hey,

we know doFilter() take ServletRequest and ServletResponse implement objects as parameters right? but here the cast seems to have problem to me.

HttpServletRequest httpreq= (HttpServletRequest) req ;

both interfaces are in same inheritence hierarchy so the compile time its ok but in runtime? i mean what is the real runtime object type of req, ServletRequest is just the reference type right?

Thank You.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Usually we make HTTP requests to our servlets. That's why the cast always works. ServletRequest object can represent a request from any protocol. So the cast will fail if the request is not HTTP. Basically HttpServletRequest represents HTTP requests while ServletRequest represents any generic request using any protocol...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Harshana. When a request comes to the container, the container first creates request object(by using container implemented class HttpServletRequestImpl ) and response object(by using container implemented class HttpServletResponseImpl) and FilterChain object(by using container implemented class FilterChainImpl) and pass it to the doFilter(ServletRequest req, ServletResponse resp, FilterChain fc).

After casting, the reference type of 'req' becomes HttpServletRequest and is pointing to the HttpServletRequestImpl object. Similarly the reference type of 'resp' becomes HttpServletResponse and object is pointing to the HttpServletResponseImpl object.
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
hey,

According to this thread,
http://www.coderanch.com/t/459297/Beginning-Java/java/interface-class-casting#2049291

A is ServletRequest
B is HttpServletRequest

C is HttpServletRequestImpl class

right?

So c object implements A right?

so when the cast HttpServletRequest httpreq= (HttpServletRequest) req ; happens,

which is similar to B bb=(B)c; shuold give a runtime error know?
Omer Haderi
Ranch Hand

Joined: Sep 27, 2006
Posts: 42
first NOT both interfaces are in the same hierarchy so ServletRequest is parent of the HttpServletRequest.

second the HttpServletRequestImpl implements the HttpServletRequest not the ServletRequest.

therefore you can pass to the doFilter() an object that implements the HttpServletRequest interface since the HttpServletRequest is a child of ServletRequest and since we know that the filter we created is for the http protocol (web app) the cast is safe.
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
first NOT both interfaces are in the same hierarchy


the two interfaces are in the same hierarchy right? i mean HttpServletRequest extends ServletRequest know?

second the HttpServletRequestImpl implements the HttpServletRequest not the ServletRequest.

therefore you can pass to the doFilter() an object that implements the HttpServletRequest interface since the HttpServletRequest is a child of ServletRequest and since we know that the filter we created is for the http protocol (web app) the cast is safe.


i got that..one confusion, now if some one ask what is the objetc type (i mean req) what should we tell..is it HttpServletReuestImpl or HttpServletRequest because that object implements that interface. What i want to know is if an object implemet an interface what does that object type becomes..i mean the newly implement interface type or the earlier class type?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What i want to know is if an object implemet an interface what does that object type becomes..i mean the newly implement interface type or the earlier class type?

Both. Animal pet = new Dog(); -> pet is both of type Dog and Animal, which means that both (pet instanceof Animal) and (pet instanceof Dog) return true.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doFilter() method parameters