Question: I'm writing a web page using java servlets. When the user subscribes he will receive an email with the activation link. Currently the servlet redirects the user (using the request dispatcher) to the thank you page after it sends the email and this takes some time. I would like to redirect the user to the page before the email is created and sent and then have the email creation performed by another servlet without the user having to wait. Is this possible? How can I call a servlet from within another servlet after using the request dispatcher.forward method? Is that possible? If not, then what is the best way to do what I want? Thanks.

Answer: 
There are in fact 2 different problems in your question :

* you want to redirect to another page
* you want to continue processing after the redirection

It is simple to redirect to another URL from a servlet, just call :

```
response.sendRedirect("url?param=value");

```

For the second part, it seems that it is possible to continue processing in servlet **after** sending the response to the client. From my test on a Tomcat 7 servlet container, you can do so if ContentLength is set to 0 (as body is empty) and the output stream is closed. I could never have a confirmation from servlet specs, but as I wrote above it works on Tomcat :

```
response.sendRedirect("url?param=value");
response.setContentLength(0);
response.getOutputStream().close();
// continue after connection with client is closed
// generate and send email

```
Based on your description you would like to return message to the user and then process 'in the background' email request. It shouldn't be done via other servlet. If you have Java EE server with JMS support, that is typical task for JMS and MDB. Your servlet would just put the message email sending request to the queue and returning message to the user. MDB would pick up that message and process it, and send the email.

Method with spawning is doable as you tested and should work in most of servers, however it is discouraged. See some discussion here [Why spawning threads in Java EE container is discouraged?](https://stackoverflow.com/q/533783/3701228) 

In Java EE 7 you will have support for it via concurrency utilities - [Concurrency Utilities tutorial](http://docs.oracle.com/javaee/7/tutorial/doc/concurrency-utilities002.htm#CIHFBCFH)