Hi all,
I am self submitting(reloading) the jsp page using javascript but after self submitting the value in <input type="file" name=" "> disappears . I tried to retain it using <input type="file" name="exe" value="<%=value[0][1]%>" > but it is not working.
please suggest me how to retain this value.


Thaks
Ritesh

Dani AI

Generated

A file input selection cannot be preserved across a full page reload — browsers deliberately prevent pages from programmatically pre-filling <input type="file"> for security and privacy. That makes 's short answer correct: echoing a server-side value back into the file control will not reselect the local file.

Practical alternatives that keep the file available after a reload or navigation:

  • Upload the file immediately (AJAX/FormData) and keep a server-side reference (filename, token, session id). After the upload completes the page can reload or navigate and display the saved file info instead of expecting the original input to remain selected.
  • Avoid a full reload: submit non-file parts via AJAX and leave the file input alone, or use a hidden iframe target for legacy browsers to submit the file without tearing down page state.
  • For multi-step forms, upload at step 1, store a server-side temp ID, and use that ID on later steps.

Example client-side upload (minimal):

const input = document.querySelector('input[type=file]');
const fd = new FormData();
fd.append('file', input.files[0]);
fetch('/upload', { method: 'POST', body: fd })
  .then(r => r.json())
  .then(data => {
    // store returned token/filename and then reload or redirect
    window.location.reload();
  });

Example server-side (Servlet 3.0):

@MultipartConfig
public class UploadServlet extends HttpServlet {
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    Part p = req.getPart("file");
    String name = Paths.get(p.getSubmittedFileName()).getFileName().toString();
    try (InputStream in = p.getInputStream()) {
      Files.copy(in, Paths.get("/tmp/uploads", name), StandardCopyOption.REPLACE_EXISTING);
    }
    // return JSON token or set session attribute
  }
}

Operational notes: always validate type/size server-side, sanitize filenames, expire temporary uploads, and show a clear "file uploaded / replace file" UI so the user knows the server has the file. For legacy containers use Apache Commons FileUpload instead of Servlet 3.0 APIs. In short: do not try to pre-fill the input — upload or preserve state on the server or avoid reloading the page.

Recommended Answers

All 3 Replies

It is not possible.

Can you please tell me why it is not possible ? as I am able to retain values in all other fields like texbox , drop down list

I think that the following information helps you to understand the problem with input file tag.

There are two MIME types for form information: application/x-www-form-urlencoded and multipart/form-data. The first MIME type, application/x-www-form-urlencoded, is the MIME type most everyone is familiar with and results in the Servlet API automatically parsing out name and value pairs.

The second MIME type, multipart/form-data, is the one that is usually considered difficult. The reason why is because the Servlet API does nothing to help you with it. Instead the information is left as is and you are responsible for parsing the request body via either HttpServletRequest getInputStream() or getReader().

So, when you upload a file, browser send the copy of your selected file along with file metadata - filename, size of file to the webserver and webserver forward this request to the application server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.