This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doFilter() method parameters

 
Harshana Dias
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic