让servlet在后台工作 [英] Make servlet work in background
本文介绍了让servlet在后台工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个上传sevlet的文件,以及我的JSP上的UI。
I have a file uploading sevlet, and the UI on my JSP.
<form method="post" action="mygeco" enctype="multipart/form-data" style="position: absolute; left: 30%; bottom: 2%;">
<input type="file" name="dataFile" id="fileChooser" />
<input type="submit" value="Upload" multiple="multiple" />
</form>
文件上传是在我的dopost方法上完成的,我的其他代码考虑到了我的doget( )方法因此,当用户按下我的jsp页面上的上传按钮时,我希望在用户停留在jsp页面上时在后台进行上传,并在上传完成时返回警报,我该怎么做?
The file uploading is done on my dopost method, and I have other code in mind for my doget() method so, when the user presses upload button on my jsp page I want the uploading to happen in background while the user stays on the jsp page and return an alert when the uploading is done, how do I do this?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType("text/html");
if (isMultipart)
{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try
{
// Parse the request
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
System.out.println("starting saver");
while (iterator.hasNext())
{
FileItem item = (FileItem) iterator.next();
if (!item.isFormField())
{
String fileName = item.getName();
String root = getServletContext().getRealPath("/");
File path = new File("/u/poolla/workspace/FirstServlet/");
if (!path.exists())
{
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
if(fileName!="")
item.write(uploadedFile);
else
System.out.println("file not found");
System.out.println("<h1>File Uploaded Successfully....:-)</h1>");
}
else
{
String abc = item.getString();
System.out.println("<br><br><h1>"+abc+"</h1><br><br>");
}
}
}
catch (FileUploadException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
}
else
{
System.out.println("Not Multipart");
}
}
}
推荐答案
您可以使用Ajax,
通过Ajax Call上传文件,
upload file via Ajax Call,
http://abandon.ie/notebook/simple-file-uploads- using-jquery-ajax
这篇关于让servlet在后台工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文