Note that there are some explanatory texts on larger screens.

plurals
  1. POresponse.sendRedirect() in Servlet not working correctly after file upload
    primarykey
    data
    text
    <p>I have a web application with a simple upload function. The idea is to allow user select a file and upon successfully upload, redirect to <code>index.jsp</code>.</p> <p>However, although the file got uploaded, the <code>response.redirect</code> is not working. After a successfully upload, the page doesn't get redirected. It just stays there. The weird thing is that I can see it is processing the <code>index.jsp</code> from the tomcat server log even though it doesn;t get redirected.</p> <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //processRequest(request, response); boolean status=false; if (!ServletFileUpload.isMultipartContent(request)) { throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form."); } ServletFileUpload uploadHandler = new ServletFileUpload(new DiskFileItemFactory()); PrintWriter writer = response.getWriter(); response.setContentType("text/plain"); try { List&lt;FileItem&gt; items = uploadHandler.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { File file = new File(getServletContext().getRealPath("/WEB-INF/upload"), item.getName()); item.write(file); writer.write("{\"name\":\"" + item.getName() + "\",\"type\":\"" + item.getContentType() + "\",\"size\":\"" + item.getSize() + "\"}"); } } //redirect to index.jsp if successfully redirect(request, response); } catch (FileUploadException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } finally { writer.close(); } } </code></pre> <p>The redirect method:</p> <pre><code>private void redirect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/index.jsp").forward(request, response); } </code></pre> <p>The file upload plugin is from <code>https://aquantum-demo.appspot.com/file-upload</code> I used the front-end and developed the upload event handler using java apache fileupload. Everything works fine except the redirect part.</p> <p>The application.js file which handles the JSON returns:</p> <pre><code>$(function () { // Initialize jQuery File Upload (Extended User Interface Version): $('#file_upload').fileUploadUIX(); // Load existing files: $.getJSON($('#file_upload').fileUploadUIX('option', 'url'), function (files) { var options = $('#file_upload').fileUploadUIX('option'); options.adjustMaxNumberOfFiles(-files.length); $.each(files, function (index, file) { options.buildDownloadRow(file, options) .appendTo(options.downloadTable).fadeIn(); }); }); </code></pre> <p>});</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload