File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes can we make response from a child thread of a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "can we make response from a child thread of a servlet?" Watch "can we make response from a child thread of a servlet?" New topic
Author

can we make response from a child thread of a servlet?

Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

can we make response from a child thread of a servlet ?


Correct Me if i am wrong
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Neeraj,
Welcome to CodeRanch! You can write to the HttpSession from a child thread, but you can't send a response back to the user.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Maybe asynchronous servlets fit the bill: http://weblogs.java.net/blog/mode/archive/2008/12/asynchronous_su.html
Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

Thanks Tim and Jeanne,
My application supports servlet 2.5 API so asynchronous servlets .... :-(
is there any another method to do this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

You haven't really explained what "this" is yet.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

i am trying to make a thread in servlet that read a file and write on ServletOutputStream.
when i am writing on ServletOutputStream and it give an exception like this

java.lang.NullPointerException
[12/21/11 10:30:00:531 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.SRTServletResponse.setHeader(SRTServletResponse.java:817)
[12/21/11 10:30:00:531 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.SRTServletResponse.addLocaleHeader(SRTServletResponse.java:301)
[12/21/11 10:30:00:547 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.SRTServletResponse.commit(SRTServletResponse.java:262)
[12/21/11 10:30:00:547 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.SRTServletResponse.alertFirstFlush(SRTServletResponse.java:211)
[12/21/11 10:30:00:547 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.BufferedServletOutputStream.flushBytes(BufferedServletOutputStream.java:350)
[12/21/11 10:30:00:547 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.BufferedServletOutputStream.write(BufferedServletOutputStream.java:307)
[12/21/11 10:30:00:547 IST] a16e830 SystemErr R at ggs.smiles.servlet.DownloadThread.run(DownloadThread.java:123)
[12/21/11 10:30:00:547 IST] a16e830 SystemErr R at java.lang.Thread.run(Thread.java:568)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R java.lang.IllegalStateException: no Content-Type was specified
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.http.HttpResponse.writeHeaders(HttpResponse.java:285)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.http.ResponseStream.writeHeaders(ResponseStream.java:238)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.http.ResponseStream.flushBuffer(ResponseStream.java:207)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.http.ResponseStream.flush(ResponseStream.java:301)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.io.WriteStream.flush(WriteStream.java:139)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.http.HttpConnection.flush(HttpConnection.java:379)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srp.SRPConnection.flush(SRPConnection.java:242)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.SRTOutputStream.flush(SRTOutputStream.java:46)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.BufferedServletOutputStream.flushBytes(BufferedServletOutputStream.java:361)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.BufferedServletOutputStream.flush(BufferedServletOutputStream.java:334)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.BufferedServletOutputStream.finish(BufferedServletOutputStream.java:167)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at com.ibm.ws.webcontainer.srt.BufferedServletOutputStream.close(BufferedServletOutputStream.java:420)
[12/21/11 10:30:00:562 IST] a16e830 SystemErr R at ggs.smiles.servlet.DownloadThread.run(DownloadThread.java:181)

All header and content type are set on servlet and i am passing response object to thread.

I know,its a bad approch to do, like this. but how to make response from servlet's child thread. is there any consistance approch ?


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

But why?
Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

Because i have a problem with downloading large files. so i want to create a thread which run in the background write on the stream and make a response.
is it possible or not?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
"a problem" is what, exactly? There's no principal problem with streaming large files in a servlet.
Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

ok. let me explain the exact problem.
when i try to download large image sometime the image get corrupt (95 % code works).
my downloading code is:

// setting header and content type depends upon type of files

ServletOutputStream out = response.getOutputStream();
FileInputStream in = null;
stream = new FileInputStream(f);
int i=0;
response.addHeader("Cache-Control", "no-transform, max-age=0");
while ((in != null) && (i = in.read()) != -1){
out.write(iRead);
i++;
}
the code will run on mozila and chrome without any exception. but in IE an exception throws i.e.


java.net.SocketException: Connection reset by peer: socket write error
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at java.net.SocketOutputStream.socketWrite0(Native Method)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:112)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at java.net.SocketOutputStream.write(SocketOutputStream.java:156)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at com.ibm.ws.io.Stream.write(Stream.java:26)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at com.ibm.ws.io.WriteStream.flushMyBuf(WriteStream.java:145)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at com.ibm.ws.io.WriteStream.flush(WriteStream.java:137)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at com.ibm.ws.http.ResponseStream.flush(ResponseStream.java:303)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at com.ibm.ws.io.WriteStream.flush(WriteStream.java:139)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at com.ibm.ws.webcontainer.http.HttpConnection.flush(HttpConnection.java:379)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at com.ibm.ws.webcontainer.srp.SRPConnection.flush(SRPConnection.java:242)
[12/21/11 17:14:53:219 IST] 70ac65bd SystemErr R at com.ibm.ws.webcontainer.srt.SRTOutputStream.flush(SRTOutputStream.java:46)

even this exeption the code runs well and image is downloaded.

The problem is corupted image.am i missing something or doing somethng wrong? Please help
i was thinking thead may solve this problem.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Neeraj Dhiman wrote:i was thinking thead may solve this problem.

Unlikely. What makes you think that?
Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

file is large. it takes time when there is load on server, and during the process connection is resseted. any how the exception is thrown after writing certain bytes on output stream.
so it may be the case connection may not be resseted, resulting corrupt image problem, May be i am wrong. so i want to test it by running as a background process.

Hey friends, if you have any idea about this problem please help its urgent. thank you in Advance.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can we make response from a child thread of a servlet?
 
Similar Threads
How to use Threads in Servlets?
display child threads !
How can I determine when my Threads are complete?
long running service call and tracking completion
invoking a thread from a servlet