• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A simple servlet question

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
method
}

Thanks
Imad
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to run some code independent of the response, you'll need to fire off a new thread.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 );
response.flushBuffer();

processRequest ();
}

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

new Thread ( requestProcessor ).start() ;
}
}

Thanks for your help.
Imad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic