jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes A simple servlet question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "A simple servlet question" Watch "A simple servlet question" New topic

A simple servlet question

Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
In y servlet class doGet method what I want to be able to do is to send a 200 ok response as soon as I get a request and then do the processing. I know to set response to 200 ok i will simply do response.setStatus (200). But how do I sent the request right after this? I think it is using response.flushBuffer() but I would appreciate if someone can confirm this for me. Plus I want to be able to run code after sending response. My java code that has nothing to do with servlet but after sending response.

some thing like this

protected void doGet ( request, response ) throws .......{
response.setStatus (200);
response.flushBuffer ();

PagingBO pagingBO = new PagingBO ();
ArrayList pages = pagingBO.getCallingQueueData();

......some more code in this same method or may be call a different

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

If you want to run some code independent of the response, you'll need to fire off a new thread.

[Asking smart questions] [About Bear] [Books by Bear]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

You could use messaging (adds a lot of overhead) to start a new set of precesses independent of the original servlet that would avoid starting a new thread explicitly.

For a J2EE system, this is a good solution since its scalable and since spawning threads is not recommended by J2EE guidelines. For smaller or simpler systems though, using messaging is probably too much work to set up.
[ December 16, 2005: Message edited by: Scott Selikoff ]

[OCA 8 Book] [Blog]
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
Hi Scott

I am a new developer and dont know what you mean by messaging. But here is the code from my servlet class. Can you or Bear please tell me if I am doing it the right way. My goal is to complete the task but before completing it send the response back and then keep doing my thing because it will going to take time.

public class SNPPServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setStatus( 200 );

processRequest ();

private void processRequest ()
SNPPRequestProcessor requestProcessor = new SNPPRequestProcessor ();

new Thread ( requestProcessor ).start() ;

Thanks for your help.
I agree. Here's the link: http://aspose.com/file-tools
subject: A simple servlet question
It's not a secret anymore!