• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

doFilter() method parameters

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey,

According to this thread,
https://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?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Well THAT's new! Comfort me, reliable tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic