0

I am new to using Java Servlets. From what I've seen so far, there are a number of ways to do the initial routing of a url, such as using @WebServlet url patterns as well as using web.xml.

From the way I see it, web.xml is really the router. Alternatively, I could wildcard all routes to single servlet, use that as a front router to some extent and do pattern matching in Java with something like request.getPathInfo();, then call other servlets from that. The implication would be that each servlet that gets called is a new thread, right?

My question is, what are the implications of doing this, such as the fact that if this is done, doesn't this mean that the servlet is restarted and reinitialized every single time? Is this the way servlets are designed to be used? Is it an okay idea to route all requests to one servlet and then use the servlet as a router?

Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
  • A lot of your side questions are rather unclear but there isn't anything stopping you from using a single servlet for routing and there are many frameworks that do precisely that. – pvg Dec 17 '15 at 18:50
  • I voted to close because this question is primarily opinion based. – bhspencer Dec 17 '15 at 18:56
  • http://stackoverflow.com/q/3106452, http://stackoverflow.com/q/3541077 – BalusC Jan 06 '16 at 14:02

2 Answers2

3

You can use a servlet to send requests to other objects to handle them, see the Front Controller pattern (as Kayaman describes), or Spring's DispatcherServlet, which doesn't route requests to other servlets but to controllers. The idea is that servlets are clunky, hard to test, and need a servlet container to instantiate them, so it's better to limit the servlets to a single dispatcher and let the controllers do most of the work. With Spring the controllers can be managed by the container, so they can be injected with other components more easily than servlets can.

A servlet is initialized once on startup, the same instance handles all requests (or technically, the servlet container might be allowed to instantiate multiple instances but you shouldn't count on it), you should assume there is only one instance of the servlet. See this question about the servlet lifecycle.

Servlets should not contain any mutable state, because multiple threads will be calling methods on the same instance concurrently. There was previously an option to make single-threaded servlets (SingleThreadModel) but it was so bad for throughput it was deprecated. Each HTTP request gets assigned its own thread from the servlet container's thread pool and that same thread stays with the request until the response is sent, threads are not specific to a particular servlet.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
2

There is a design pattern called Front Controller which does exactly this. All the requests are routed to a single servlet (for example the DispatcherServlet in Spring), which then directs the requests to non-servlet classes that handle the actual business logic.

The front controller usually performs additional things that help with the writing of the non-servlet classes.

Kayaman
  • 72,141
  • 5
  • 83
  • 121