Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we make response from a child thread of a servlet ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Moores
Bartender
Posts: 2891
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe asynchronous servlets fit the bill: http://weblogs.java.net/blog/mode/archive/2008/12/asynchronous_su.html
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't really explained what "this" is yet.
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why?
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2891
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"a problem" is what, exactly? There's no principal problem with streaming large files in a servlet.
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj Dhiman wrote:i was thinking thead may solve this problem.

Unlikely. What makes you think that?
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic