After I make a GET request to the following servlet, the text ‘In service method’ is written and can not explain it. why is that?
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class MyServlet extends HttpServlet {
public void service(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
Writer out = resp.getWriter();
out.write("In Service method");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Writer out = resp.getWriter();
out.write("In get method");
}
}
The default implementation of
HttpServlet#service()does exactly that job. Here’s an extract of its javadoc:It delegates to the appropriate servlet method depending on the HTTP method. E.g. when
request.getMethod()equals to"GET", then it delegates todoGet(). By the way, have you read the last sentence as well?In any way, you should basically be performing the very same job yourself.
Or just call the
supermethod so that it can do its job.Or just don’t override it at all.