• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

doFilter() method parameters  RSS feed

 
Ranch Hand
Posts: 352
  • 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.
 
Sheriff
Posts: 9613
37
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...
 
Ranch Hand
Posts: 437
Java Oracle
  • 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: 352
  • 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?
 
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: 352
  • 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?
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!