| | |
servlet: why the service method run twice in special time
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
JSP Syntax (Toggle Plain Text)
public class ShowImageServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getImageTask(request,response); } public void getImageTask(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { OutputStream toClient=null; int maxFileSize=1048576; String genPath= "D:\\"; String path=""; String realPath=""; String imageType=""; String finalPath=""; FileInputStream inFile=null; byte[] data ; try{ path=request.getParameter("filePath"); if(path==null||"".equals(path)) { throw new Exception("you send a empty file"); } else{ int splitType=path.indexOf("."); imageType=path.substring(splitType+1,path.length()); int splitBank=path.indexOf("_"); if(splitBank!=-1){ realPath=path.substring(0,splitBank); finalPath=genPath+File.separatorChar+"advert"+File.separatorChar+realPath.trim()+File.separatorChar+path; }else{ StringTokenizer toke = new StringTokenizer(path, "|"); while (toke.hasMoreElements()) { String pathName = toke.nextToken(); realPath=realPath+File.separator+pathName; } finalPath=genPath+realPath.trim(); } } if(("gif").equals(imageType)) { imageType="image/gif;charset=GB2312 "; } else if(("swf").equals(imageType)){ imageType="application/x-shockwave-flash;charset=GB2312 "; } else if(("mht").equals(imageType)){ imageType="message/rfc822;charset=GB2312 "; } else{ imageType="image/jpeg;charset=GB2312 "; } response.setContentType( imageType); // System.out.println(finalPath+"...."+imageType); File file = new File(finalPath); if(file.exists()&&file.isFile()){ inFile = new FileInputStream(file); // int i = inFile.available(); // if(i>maxFileSize){ throw new Exception( "file size is too large"); } data= new byte[i]; inFile.read(data); // inFile.close(); } else{ String emptyPath=genPath+File.separatorChar+"empty.jpg"; File emFile = new File(emptyPath); inFile = new FileInputStream(emFile); // int i = inFile.available(); // data = new byte[i]; inFile.read(data); // inFile.close(); //inFile.reset(); inFile = null; } toClient = response.getOutputStream(); // toClient.write(data); // toClient.flush(); toClient.close(); toClient =null; } catch (IOException ex) { System.err.println("****************Error1"); ex.printStackTrace(); throw ex; }catch(Exception e){ System.err.println("****************Error1"); } finally { if (toClient != null) { try { toClient.flush(); toClient.close(); } catch (IOException e2) { System.err.println("****************Error1"); } } } } }
JSP Syntax (Toggle Plain Text)
<%@ page language="java" contentType="text/html; charset=GBK" %> <html> <head> <title>My Test </title> </head> <script> function test(){ var file = document.getElementById('testfile').value; var theUrl = '/mytest/ShowImage?filePath='+file; window.open(theUrl); } </script> <body> <form name="form1"> <table> <tr> <td> <input type="text" id="testfile" name="filePath" value="t.mht"> <input type="button" value="upfile" onclick="test();"> </td> </tr> </table> </form> </body> </html>
Last edited by bobocqu; Nov 5th, 2008 at 11:55 am.
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
Don't override
The `action' attribute of the
Also there is no reason for a service method to run twice unless there actually were requests made; print out the
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.
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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)
![]() |
Other Threads in the JSP Forum
- Previous Thread: Updating a MySQL database using Java
- Next Thread: What is the best JSP/Servlet container?
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






