让servlet在后台工作

我有一个上传sevlet的文件,以及我的JSP上的UI。

 

文件上传是在我的dopost方法上完成的,我的doget()方法还有其他代码,所以,当用户按下我的jsp页面上的上传按钮时,我希望在用户停留在jsp上时在后台进行上传页面并在上传完成后返回警报,我该怎么做?

 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("

File Uploaded Successfully....:-)

"); } else { String abc = item.getString(); System.out.println("

"+abc+"



"); } } } catch (FileUploadException e) { System.out.println(e); } catch (Exception e) { System.out.println(e); } } else { System.out.println("Not Multipart"); } } }

您可以使用Ajax,

通过Ajax Call上传文件,

http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax