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.

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"); 
} 
} 
} 
} 

}
<%@ 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>

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.

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.

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)

Attach a war file of your project so someone can try it out.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.