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
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.
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
Joined: Jan 04, 2004
Posts: 16
posted
0
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.