Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can we access the methods in the HttpServletRespone interface

 
Suresh Khanna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
How can we access the methods in the HttpServletRespone interface
object without implementing this interface.
ie we are using
public void doGet(HttpServletRequest req, HttpservletRespone res)
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
---------
---------
}

where the setrContentType() and getWriter() method implementations are done. Pls Clarify.
Thank in advance
Suresh K
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh,
doGet() is passed a class that implements the HttpServletResponse interface. Each servlet container provides its own implementation. You just call the methods as shown in your example.
 
Dinesh Kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh,
To give a more elaborate reply, the HttpServletResponse res object that is being passed as a parameter actually is an object of a class that implements the HttpServletResponse interface.
You'll find that most of the time it's the interface that you're using during your Servlet programming. This is because the Servlet specs. doesn't manadate any class names for most of the interfaces. That's left upto the Servlet containers (e.g. tomcat) to design.
So if you try something like, System.out.println(res.getClass()) on your response object, you'll find the class name that the container is actually using.
Since this class implements the interface HttpServletResponse, it is perfectly legal to declare your res object to be of the type HttpServletResponse.
Same is the case with many other interfaces in the Servlet API.
Hope that helps.
 
Suresh Khanna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you Jeanne and Dinesh for your answers.
Dinesh, got the class that is implementing the HttpServletResponse interface
by calling getClass() method. I appreciate your elaborate explanation.

Regards
Suresh K
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic