servlet: why the service method run twice in special time

Thread Solved
Reply

Join Date: Nov 2008
Posts: 12
Reputation: bobocqu is an unknown quantity at this point 
Solved Threads: 0
bobocqu bobocqu is offline Offline
Newbie Poster

servlet: why the service method run twice in special time

 
0
  #1
Nov 5th, 2008
the question description:
when I read a jpg file, there is no any problem. but when I try to read a mht file, the service method was run twice. why? Can anyone give me a satisfying explain? Thanks first in here.


  1. public class ShowImageServlet extends HttpServlet {
  2. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  3. getImageTask(request,response);
  4. }
  5.  
  6. public void getImageTask(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
  7. {
  8. OutputStream toClient=null;
  9. int maxFileSize=1048576;
  10. String genPath= "D:\\";
  11. String path="";
  12. String realPath="";
  13. String imageType="";
  14. String finalPath="";
  15. FileInputStream inFile=null;
  16. byte[] data ;
  17. try{
  18. path=request.getParameter("filePath");
  19. if(path==null||"".equals(path))
  20. {
  21. throw new Exception("you send a empty file");
  22. }
  23.  
  24. else{
  25. int splitType=path.indexOf(".");
  26. imageType=path.substring(splitType+1,path.length());
  27. int splitBank=path.indexOf("_");
  28. if(splitBank!=-1){
  29. realPath=path.substring(0,splitBank);
  30. finalPath=genPath+File.separatorChar+"advert"+File.separatorChar+realPath.trim()+File.separatorChar+path;
  31. }else{
  32. StringTokenizer toke = new StringTokenizer(path, "|");
  33. while (toke.hasMoreElements()) {
  34. String pathName = toke.nextToken();
  35. realPath=realPath+File.separator+pathName;
  36. }
  37. finalPath=genPath+realPath.trim();
  38. }
  39. }
  40. if(("gif").equals(imageType))
  41. {
  42. imageType="image/gif;charset=GB2312 ";
  43. }
  44.  
  45. else if(("swf").equals(imageType)){
  46. imageType="application/x-shockwave-flash;charset=GB2312 ";
  47. }
  48. else if(("mht").equals(imageType)){
  49. imageType="message/rfc822;charset=GB2312 ";
  50. }
  51. else{
  52. imageType="image/jpeg;charset=GB2312 ";
  53. }
  54. response.setContentType( imageType); //
  55. System.out.println(finalPath+"...."+imageType);
  56. File file = new File(finalPath);
  57. if(file.exists()&&file.isFile()){
  58. inFile = new FileInputStream(file); //
  59. int i = inFile.available(); //
  60. if(i>maxFileSize){
  61. throw new Exception( "file size is too large");
  62. }
  63. data= new byte[i];
  64. inFile.read(data); //
  65. inFile.close();
  66. } else{
  67.  
  68. String emptyPath=genPath+File.separatorChar+"empty.jpg";
  69.  
  70. File emFile = new File(emptyPath);
  71. inFile = new FileInputStream(emFile); //
  72. int i = inFile.available(); //
  73. data = new byte[i];
  74. inFile.read(data); //
  75. inFile.close();
  76. //inFile.reset();
  77. inFile = null;
  78. }
  79. toClient = response.getOutputStream(); //
  80. toClient.write(data); //
  81. toClient.flush();
  82. toClient.close();
  83. toClient =null;
  84. }
  85. catch (IOException ex) {
  86. System.err.println("****************Error1");
  87. ex.printStackTrace();
  88. throw ex;
  89. }catch(Exception e){
  90. System.err.println("****************Error1");
  91. }
  92. finally {
  93. if (toClient != null) {
  94. try {
  95. toClient.flush();
  96. toClient.close();
  97. }
  98. catch (IOException e2) {
  99. System.err.println("****************Error1");
  100. }
  101. }
  102. }
  103. }
  104.  
  105. }
  1. <%@ page language="java" contentType="text/html; charset=GBK" %>
  2. <html>
  3. <head>
  4. <title>My Test </title>
  5. </head>
  6. <script>
  7. function test(){
  8. var file = document.getElementById('testfile').value;
  9. var theUrl = '/mytest/ShowImage?filePath='+file;
  10. window.open(theUrl);
  11. }
  12. </script>
  13. <body>
  14. <form name="form1">
  15. <table>
  16. <tr>
  17. <td>
  18. <input type="text" id="testfile" name="filePath" value="t.mht">
  19. <input type="button" value="upfile" onclick="test();">
  20. </td>
  21. </tr>
  22. </table>
  23. </form>
  24. </body>
  25. </html>
Last edited by bobocqu; Nov 5th, 2008 at 11:55 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: bobocqu is an unknown quantity at this point 
Solved Threads: 0
bobocqu bobocqu is offline Offline
Newbie Poster

Re: servlet: why the service method run twice in special time

 
0
  #2
Nov 6th, 2008
Oh, sorry. I have submited a bad question. I will describe the question more in detail.
I have traced the action. When read a mht file. The first time, the character part was loaded, and the image part will be loaded in the second. But I will catch a Exception. IOException, the Exception's name is AbortClientException, Such as socket reset.
My Environment is Tomcat5.0.28.
And I issue the program on WebSphere, there is no Exception, but still run twice.
I have deal with the IOException, when the name is AbortClientException, I will not log the logs.
But I still want know why should the servlet run twice.
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: servlet: why the service method run twice in special time

 
0
  #3
Nov 8th, 2008
Don't override service ; just override the method which you want to support and accordingly override the doGet() or doPost() method.

The `action' attribute of the FORM tag is mandatory. You don't need to use Javascript, just use the action attribute of FORM tag with a normal submit button.

Also there is no reason for a service method to run twice unless there actually were requests made; print out the request.getRequestURL() inside your request handling method to see which URL was requested by the client. In case your page references other resources like images or Javascript files present on the server and your web.xml maps each request to your servlet, the multiple invocations might just be explained.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: bobocqu is an unknown quantity at this point 
Solved Threads: 0
bobocqu bobocqu is offline Offline
Newbie Poster

Re: servlet: why the service method run twice in special time

 
0
  #4
Nov 8th, 2008
thanks. maybe. But I have tried that before you give me that advice. I can't find anything. And I can't get any suggestion from google. You can try read mht files by yourself. If you don't get any Exception or you just request once and get the mht file loaded. you maybe should test many times. please tell me if you have tried. you are welcome here.(you can get my servlet name in my code, ShowImage)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: servlet: why the service method run twice in special time

 
0
  #5
Nov 8th, 2008
Attach a war file of your project so someone can try it out.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: bobocqu is an unknown quantity at this point 
Solved Threads: 0
bobocqu bobocqu is offline Offline
Newbie Poster

Re: servlet: why the service method run twice in special time

 
0
  #6
Nov 8th, 2008
sorry. I can't find the button to attach the war file. maybe you can create a new small project. and put the servlet and the jsp in it. It will not take your much time. ^_^ thanks for your attention.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the JSP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC