0

could someone please explain this java code for me. i find it really hard to understand.

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    String firstName = request.getParameter("firstName").toString();
    processRequest(request, response);
}

so request is an object of the interface class HttpServletRequest which got some empty methods.

  1. but how can it return any data if the method getParameter() is empty.

  2. and also, i cant find getParameter() in the HttpServletRequest interface. But shouldn´t it be one, cause request is declared as a HttpServletRequest and thus own all its methods and fields?

  3. and which class is toString() coming from.

2
  • and you should really try googling the names of the methods and objects you're wondering about, e.g.: google.com/search?q=getParameter%20HttpServletRequest Commented Jan 10, 2010 at 18:33
  • but there were more questions about the whole picture...knowing how it works. not that i cant find specific information. Commented Jan 10, 2010 at 18:51

4 Answers 4

2

So HttpServletRequest is an interface. Interfaces declare methods for actual classes to implement. In this case, your servlet container will provide the implementation for you (depends what you are using: Tomcat, Jetty, etc). Remember that interfaces can also extend each other. In this case HttpServletRequest extends ServletRequest. You should look at ServletRequest for additional methods.

Just to be clear, the methods of HttpSerlvetRequest aren't "empty". Have been implemented by the container, so when the container calls your method above, the objects it is passing to you are full-fledged objects that happen to implement the HttpServletRequest interface. this is why you can cast them as such.

Edit: toString() method comes from the java.lang.Object class. This is the base class for all java objects.

Sign up to request clarification or add additional context in comments.

6 Comments

but how do i know that i can use getParameter as method? and toString? which class is it that has them as methods? i have to know this information to be able to code and know how to think.
Well remember that any class you use has to extend java.lang.Object, so right off the bat you can use any method declared there. As for HttpServletRequest, the hierarchy looks like HttpServletRequest -> ServletRequest. It sounds like you might be struggling with inheritance and subclassing a bit. A class that implements an interface or extends some other class has access to the methods of those other constructs.
but if a class is implementing an interface...isnt it up to me to implement those methods declared and not by others?
not necessarily. In the case above HttpServletRequest is an interface. Think of it like a contract, you know that the object coming into your method can AT LEAST do everything an HttpServletRequest can because it has been typed that way. Who knows what kind of real object lives underneath it. We need not know how Tomcat or Jetty implement that actual class that provide the implementation for HttpServletRequest. All we care about is that it supports everything that an HttpServletRequest should.
but then why aren´t all classes in SE API interfaces and i don´t have to know about anything except what methods and fields i can use?
|
2

@noname, @darren did a good job answering your question, but your question to him suggests that you need even more fundamental matters explained. So...

request is an object of a class that implements HttpServletRequest. And when we look to the declaration of HttpServletRequest we see:

public interface HttpServletRequest extends ServletRequest {

Saying that a class implements an interface, or that one interface extends another, can usefully be expressed in more human terms as IS A. So request is an object of a class that IS A HttpServletRequest, which IS A ServletRequest. Boiled all the way down:

request IS A ServletRequest.

This means that any methods ServletRequest declares can be invoked for your request object, and the declaration of ServletRequest includes:

public String getParameter(String name);

And that's why we can call request.getParameter(someString).

You say:

which class is it that has them as methods? i have to know this information to be able to code and know how to think.

But that's not right. You don't need to know which specific class holds the implementation of the method, and you will come to understand this as a strength. It is an abstraction, and abstraction is at the heart of programming; it is where all the power lies. The doPost() method you posted can accept ANY HttpServletRequest as a parameter without knowing specifically which implementation has been passed in - all it knows is that whatever is passed in will honor the HttpServletRequest interface. One time it may be called with a request that is of a class from a library; the very next time it may be called with a different request, from a class you have written yourself. That is why doPost() can be used in different contexts; that's what makes it powerful.

2 Comments

thx for the thorough explanation. just one question. if we got 2 classes (from vendors) implementing the interface, and im using the interface´s methods, which class's method will it use?
who knows? That's the beauty of it. We don't need to know this information, so long as the interface is honored. Realistically in the case of the HttpServletRequest the implementation comes from the container you are using. e.g. if you are hosting your webapp with Tomcat, then it will provide the class that implements HttpServletRequest.
1

1) HttpServletRequest inherits from the interface ServletRequest, which contains the getParameter() method.

2) When a Java method lists an interface as a parameter type, an object that implements that interface must be passed in as a parameter. That object will have code that implements getParameter(), rather than the 'empty method' you see in the interface. (For example, one such object is HttpServletRequestWrapper.)

All this information can be found in the JavaDocs, e.g.:

Comments

1

The method doesn't take an object of the Interface type "HttpServletRequest". Rather, it accepts any object which implements that Interface. The way a Class implements an Interface is to provide an implementation (ie, code) for the methods that the Interface says are available.

For example, assume we had an Interface for a Dog

interface Dog {
    public String bark();
}

For any given class to implement that Interface, it would have to provide it's own version of bark(). Each class implements that method in the way that makes the most sense to it.

class GoldenRetriever implements Dog {
    public String bark() {
        return "Woof!";
    }
}
class YippyDog implements Dog {
    public String bark() {
        return "yip";
    }
}

Edit for comments:

When actually using the bark() command, you would write a method that takes, as input, any class that implements the Dog interface:

public dodog(Dog mydog) {
    System.out.println(mydog.bark());
}

Then, you could use that method by creating an Object of any of the types that implement the Dog Interface

public void usedog() {
    GoldenRetriever golden = new GoldenRetriever();
    dodog(golden);

    YippyDog yippy = new YippyDog();
    dodog(yippy);
}

The dodog method accepts both of those because they're both instances of a Class that implements the Dog Interface. It can then call the bark() method because it knows they implement it... because they implement the Dog Interface.

I hope that's clearer.

2 Comments

i know how it works from the coding perspective but not in practical use. so would i call the interface's method or each class's method? eg. like the example i posted, should I type: Dog myDog; then use myDog.bark(). which one between GoldenRetriever and YippyDog would it use?
That depends on whether inbetween you say myDog = new GoldenRetriever(); or myDog = new YippyDog(); - it decides at runtime based on the actual object's class which method gets called. This is called "dynamic method dispatch".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.