This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Interface method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Interface method question" Watch "Interface method question" New topic
Author

Interface method question

Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
Hi,

I have written a small snippet to try out some methods in javax.servlet.ServletRequest interface.

Here is the partial view of the working program.

package com.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


public class displayParam extends HttpServlet {

public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

int counter = 1;
response.setContentType("text/html");
PrintWriter out = response.getWriter();

/* Server Information */
int lport = request.getLocalPort();
int rport = request.getRemotePort();
int sport = request.getServerPort();
==================================================================
I know that HttpServletRequest extends javax.servlet.ServletRequest interface and also that the methods in the ServletRequest interface are implemented by the class ServletRequestWrapper class. The piece which I am not able to figure out is how is the connection made by the parameter 'request' and the implementing class ServletRequestWrapper. Please advise.

Regards,
Sen
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41078
    
  43
The doPost method is passed an object that implements the HttpServletRequest interface as the "request" parameter. Which class is used for this is up to the servlet container. It may be a ServletRequestWrapper object, but that is not a requirement of the servlet spec - this class merely helps servlet container implementors, and they are free to come up with their own object that implements HttpServletRequest.

It's a similar situation for the web app developer with HttpServlet - that's a convenience class that can be used, but doesn't have to be used, as long as the servlet class implements all the required interfaces.

Does this answer your question?


Ping & DNS - my free Android networking tools app
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
Why is that there is no explicit reference to the implementing class-in my understanding, it should have been 'ServletRequestWrapper'-within the code. In other words, there is no statement like 'ServletRequestWrapper' implements ServletRequest interface' within the code to make it work.

Or as you stated, is the web container designed to resolve the reference between the implementing class (ServletRequestWrapper) and the corresponding interface (HttpServletRequest) at runtime by working backwards from the parameter name (request) which is of type HttpServletRequest? Please advise.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41078
    
  43
Looking at the javadocs of HttpServletRequestWrapper, I see "implements HttpServletRequest", and for ServletRequestWrapper I see "implements ServletRequest", so I'm not sure what you think is missing?

Those notices in the javadocs are present because they are present in the source code of ServletRequestWrapper and HttpServletRequestWrapper.

The container has nothing to resolve. There is only one object, and inside of doGet all that matters is that it does indeed implement HttpServletRequest. In other places (internally in the container) it will treat it as the object it actually is, but that is off-limits to the servlet code.
Sen George
Ranch Hand

Joined: Sep 21, 2005
Posts: 76
Thanks Ulf.
 
 
subject: Interface method question
 
Similar Threads
HttpServletRequest doubt
doFilter - what to do in this case?
java.lang.NoSuchMethodError
Tomcat 4.0 problem with Filter class
Request,Response object creation?