José Leal José Leal -4 years ago 134
Java Question

Servlet being called twice!

sorry but I do not have the actual code with me, but I will try to explain:

I have a servlet mapped to the following:

/admin/*


So, this goes to a servlet:

public class AdminController extends MainController {
public void doPost(HttpServletRequest request, HttpServletResponse response) {
// Do stuf here
}
}


Here is MainController:

public class MainController extends HttpServlet {
@Override
public void service(ServletRequest request, ServletResponse response) {
String requesturi = ((HttpServletRequest)request).getRequestURI();
reqlist = Arrays.asList(requesturi.substring(requesturi.indexOf(Util.rootPath) + Util.rootPath.length()).split("/"));
reqlist = reqlist.subList(1, reqlist.size());

doPost((HttpServletRequest)request, (HttpServletResponse)response);
}


So, the request is passed to AdminController, no problem, but then I reallized something:

The servlet is being called twice!. And this is causing me many errors..

Does anybody have a clue on this? It is because I used some kind of heritance?
Thank you for all!

Answer Source

The HttpServlet.service method gets called for all request types and what you are seeing is a HEAD request and then a GET or POST request. Instead of implementing service just implement doGet or doPost. What is commonly done is to just implement one of doPost or doGet and then call the other from the one you don't have an implementation for.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download