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.
-
but how can it return any data if the method getParameter() is empty.
-
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?
-
and which class is toString() coming from.
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.