The servlet is called twice!

Sorry, I donโ€™t have the code with me, but I will try to explain:

I have a servlet mapped to the following:

/admin/* 

So this applies to the servlet:

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

Here is the 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 the AdminController, no problem, but then I redid something:

The servlet is called twice! . And that makes me a lot of mistakes.

Does anyone have a clue to this? Is it because I used some of my legacy? Thank you all!

+3
source share
4 answers

The HttpServlet.service method is called for all types of requests, and what you see is a HEAD request and then a GET or POST request. Instead of implementing the service, simply implement doGet or doPost. What is usually done is simply to implement one of doPost or doGet, and then call another of the one for which you do not have an implementation.

+8
source

Although this is an old thread, but my answer might help someone. Today I faced the same problem. My servlet worked fine earlier, and suddenly he started calling the doGet method twice. During the investigation, I found that in my Chrome browser there is an html validator extension that again calls the servlet with the same html check request. After disabling the extension, the problem was resolved.

+9
source

I solved the same problem in a simple way.

If you are developing local access and gaining access to your application with the address http://127.0.0.1 , which is a loop network, change the address to http: // localhost , which is direct.

This problem will not work if you actually run it on a web hosting or server and access it from an external network.

+2
source

Had the same problem and I tried everything that was mentioned above and on other posts, but the problem was only on local.

If nothing works for you, try deploying :)

0
source

All Articles