wood burning stoves 2.0*
The moose likes Servlets and the fly likes How can I solve this problem? servlet with strong process and Time out. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can I solve this problem? servlet with strong process and Time out." Watch "How can I solve this problem? servlet with strong process and Time out." New topic
Author

How can I solve this problem? servlet with strong process and Time out.

Fabio Batalha
Greenhorn

Joined: Aug 11, 2004
Posts: 6
I made this class that do some stuffs and this stuffs take several minutes to be done so almost every time I run this servlet I got a time out.

What can I do to leavy the strong process running at the server side and at the same time send a message to the client?



import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import de.infinityloop.upcast.UpcastEngine;
import java.io.*;
import java.util.*;
//import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerConfigurationException;
//import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class UploadFileServlet extends HttpServlet{

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
boolean isMultipart = FileUpload.isMultipartContent(request);
TransformerFactory tFactory = TransformerFactory.newInstance();

//registering UPCAST LIB
UpcastEngine.setLicense( new FileInputStream(outputFileName("/upcast/upcast.license")) );
//boolean isRegistered = UpcastEngine.setLicense(outputFileName("/upcast/upcast.license"));

try
{
if (isMultipart == true)
{
//Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
Transformer transformer = tFactory.newTransformer(new StreamSource(outputFileName("/upcast/xsl/legis.xsl")));

//Set upload parameters
upload.setSizeThreshold(0);//yourMaxMemorySize
upload.setSizeMax(5000000);//yourMaxRequestSize
//upload.setRepositoryPath("C:\\TEMP\\UploadFiles");

//Parse the request
List items = upload.parseRequest(request);

//Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
out.println(processFormField(item));
} else
{
out.println(processUploadedFile(item));
File uploadedFile = new File(outputFileName("/upcast/rtf/file.rtf"));
item.write(uploadedFile);
}
}

// Gerando XML do UPCAST
UpcastEngine uc = new UpcastEngine("instanceOne");
uc.setGlobalParameter( "outputDir", outputFileName("/upcast/output") );
String filterID = uc.setImportFilterType( UpcastEngine.kRTFImportType );
uc.setImportFilterParameter( filterID, "IncludeImages", new Boolean( true ) );
uc.importFile( outputFileName("/upcast/rtf/file.rtf") );
filterID = uc.setExportFilterType( UpcastEngine.kXMLExportType );
uc.setExportFilterParameter( filterID, "AllowEmptyCells", new Boolean( false ) );
uc.exportFile( outputFileName("/upcast/input/file_upcast.xml") );

//transformando XML UPCAST
transformer.transform(new StreamSource(outputFileName("/upcast/input/file_upcast.xml")), new StreamResult(new FileOutputStream(outputFileName("/upcast/output/file.xml"))));
out.println("<textarea rows=20 cols=80>temp</textarea>");
}
}
catch(FileUploadException ex)
{
out.println(ex.getMessage());
}
catch(Exception ex)
{
out.println(ex.getMessage());
}
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
doPost( request , response );
}

public String processFormField(FileItem item){
String name = item.getFieldName();
String value = item.getString();
return "Campo "+ name +" Valor "+ value +"<br/>";
}

public String processUploadedFile(FileItem item)
{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
return "O arquivo "+ fileName +" foi enviado com sucesso.<br/>";
}

public String outputFileName(String path)
{
ServletContext context = getServletContext();
String realPath = context.getRealPath(path);
return realPath;
}
}
Surya Suravarapu
Greenhorn

Joined: Oct 20, 2004
Posts: 10
Can this time consuming process be run as a seperate thread? This will give the control back to the servlet immediately and you can show an appropriate message to the user.

- SPS
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How can I solve this problem? servlet with strong process and Time out.
 
Similar Threads
upload file question
How to display all headers of multipart request?
Displaying image which is outside my web application
Store file in access
Unable to upload file using Swing and Servlets to Server