Reputation: 3407
I'm trying to understand how the assignment of response.getWriter() happens to PrintWriter.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("");
}
I know that the parent interface (super interface) of HTTPServletResponse contains a getWriter method which returns an instance of a PrintWriter class. However, what kind of object gets passed in the 'response' argument when this code executes. There has to be some kind of connection between that object that is passed as the response and the PrintWriter object...since PrintWriter class doesn't have anything to do with the HTTPServletResponse nor its parent inerfaces...otherwise I don't see how you can assign the PrintWriter variable to the response.
I tried to implement my own example of this functionality and created an interface called iDriveable which contains a method that returns a class called Vehicle. But I keep getting a nullPointerException
public interface Drivable {
public Vehicle accelerate();
}
public class Vehicle {
public String VehFunction(){
System.out.println("in vehFunction");
return "vehicle class";
}
public void testFunction(Driveable driveable){
Vehicle veh = driveable.accelerate();
veh.VehFunction();
}
public static void main(String[] args) {
Driveable d = null;
Vehicle v = new Vehicle();
v.testFunction(d);
}
}
Thanks
Upvotes: 0
Views: 954
Reputation: 692151
I'm not sure I understand the question. The object passed as argument is an instance of a class, provided by the servlet container, which looks like this:
public class HttpServletResponseImpl implements HttpServletResponse {
private PrintWriter out = new PrintWriter(...);
@Override
public PrintWriter getWriter() {
return this.out;
}
// other fields and methods
}
It's a simple case of an object containing another object.
EDIT:
to explain why your example doesn't work: you're passing d
, which is null, to v.testFunction()
. This method calls accelerate()
on this null reference. So obviously, you get a NullPointerException. You can't call any method on null
.
Let's rewrite your example, which is too complex to be understandable, and even more complex that the simple HttpResponse case.
Let's say we have an interface Vehicle:
public interface Vehicle { // an interface, just like HttpServletResponse
Person getDriver(); // returns a Person, just like getWriter returns a PrintWriter
}
And a simple class Person:
public class Person { // a class, just like PrintWriter
private String name;
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
No instance of the Vehicle interface can exist without defining a clas implementing it:
public class Car implements Vehicle { // an implementation of Vehicle, just like Tomcat provides an implementation of HttpServletResponse
private Person driver; // contains a person, just like the HttpServletResponse iplementation contains a PrintWriter
public Car(Person driver) {
this.driver = driver;
}
@Override
public Person getDriver() {
return this.driver;
}
}
Now let's write a program:
public static void main(String[] args) {
Person john = new Person("John");
Vehicle vehicle = new Car(john);
doGet(vehicle);
}
public static void doGet(Vehicle vehicle) {
Person p = vehicle.getDriver();
System.out.println(p);
}
In this example:
Vehicle
is analogous to HttpServletResponse
. Vehicle.getDriver()
is analogous to HttpServletResponse.getWriter()
. Person
is analogous to PrintWriter
. Car
is analogous to HttpServletResponseImpl
, which is the concrete implementation, provided by Tomcat, of the HttpServletResponse interface.main
is analogous to the internal code of Tomcat, which invokes doGet()
, which is analogous to your servlet's doGet()
method.Upvotes: 2
Reputation: 4713
However, what kind of object gets passed in the 'response' argument when this code executes. There has to be some kind of connection between that object that is passed as the response and the PrintWriter object...since PrintWriter class doesn't have anything to do with the HTTPServletResponse nor its parent inerfaces...otherwise I don't see how you can assign the PrintWriter variable to the response.
I think what you need to understand is,
HttpServeltResponse inherits from ServletResponse
ServletResponse has getWriter()
Upvotes: 0