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  { 

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 ; 
throw new Exception("you send  a empty  file"); 

int splitType=path.indexOf("."); 
int splitBank=path.indexOf("_"); 
StringTokenizer toke = new StringTokenizer(path, "|"); 
while (toke.hasMoreElements()) { 
String pathName = toke.nextToken(); 
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 "; 
imageType="image/jpeg;charset=GB2312 "; 
response.setContentType( imageType);  // 
File  file  =  new  File(finalPath); 
inFile  =  new  FileInputStream(file);  //   
int  i  =  inFile.available();  //    
throw new Exception( "file size is too large"); 
data=  new  byte[i];;  // 
}  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 = null; 
        toClient  =  response.getOutputStream();  //   
        toClient.write(data);  //  
toClient =null; 
catch (IOException ex) { 
throw ex; 
}catch(Exception e){ 
finally { 
if (toClient != null) { 
try { 
catch (IOException e2) { 

<%@ page language="java" contentType="text/html; charset=GBK" %> 
<title>My Test </title> 
function test(){ 
var file = document.getElementById('testfile').value; 
var theUrl = '/mytest/ShowImage?filePath='+file;; 
<form name="form1"> 
<input type="text" id="testfile" name="filePath" value="t.mht"> 
<input type="button" value="upfile" onclick="test();"> 

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.

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)

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.

This question has already been answered. Start a new discussion instead.