Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6339027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:33:58+00:00 2026-05-24T19:33:58+00:00

I am currently using Uploadify (Flash + Ajax) to the Servlet (Commons Upload with

  • 0

I am currently using Uploadify (Flash + Ajax) to the Servlet (Commons Upload with OWASP ESAPI overlay) with success, but I was wondering how I would go about building in HTML5 support, or rather HTML5 with Flash support.

I know how to get the HTML5 drag and drop working, but I can’t quite figure out the mechanics of a Java Servlet connection and/or backend.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:33:59+00:00Added an answer on May 24, 2026 at 7:33 pm

    I know how to get the HTML5 DnD working, but I can’t quite figure out the mechanics of a Java Servlet connection and/or backend.

    It’s not different from when using a regular <form enctype="multipart/form-data">. All you need to do is to get that HTML5/JS code to send a multipart/form-data request with the dropped file, exactly the same kind of request as it would have been sent with a regular <input type="file"> field. I’ll assume that you just can’t figure out how to achieve exactly that with HTML5/JS.

    You can utilize the new HTML5 File API, XHR2 FormData and XMLHttpRequestUpload APIs for this.

    Here’s a kickoff example of how your drop event handler should look like:

    function dropUpload(event) {
        event.stopPropagation();
        event.preventDefault();
    
        var formData = new FormData();
        formData.append("file", event.dataTransfer.files[0]);
    
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "uploadServlet");
        xhr.send(formData);
    }
    

    That’s it. This example assumes that the servlet is mapped on a URL pattern of /uploadServlet. In this example, the file is then available in Apache Commons FileUpload the usual way as a FileItem instance with a field name of file.


    For more advanced stuff like attaching event handlers for monitoring the progress and like, checkout the following blogs:

    • HTML5 Drag and Drop Upload and File API Tutorial
    • HTML5 File Upload With Progress

    I’ve played somewhat around it with the following SSCCE:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>HTML5 drag'n'drop file upload with Servlet</title>
            <script>
                window.onload = function() {
                    var dropbox = document.getElementById("dropbox");
                    dropbox.addEventListener("dragenter", noop, false);
                    dropbox.addEventListener("dragexit", noop, false);
                    dropbox.addEventListener("dragover", noop, false);
                    dropbox.addEventListener("drop", dropUpload, false);
                }
    
                function noop(event) {
                    event.stopPropagation();
                    event.preventDefault();
                }
    
                function dropUpload(event) {
                    noop(event);
                    var files = event.dataTransfer.files;
    
                    for (var i = 0; i < files.length; i++) {
                        upload(files[i]);
                    }
                }
    
                function upload(file) {
                    document.getElementById("status").innerHTML = "Uploading " + file.name;
    
                    var formData = new FormData();
                    formData.append("file", file);
    
                    var xhr = new XMLHttpRequest();
                    xhr.upload.addEventListener("progress", uploadProgress, false);
                    xhr.addEventListener("load", uploadComplete, false);
                    xhr.open("POST", "uploadServlet", true); // If async=false, then you'll miss progress bar support.
                    xhr.send(formData);
                }
    
                function uploadProgress(event) {
                    // Note: doesn't work with async=false.
                    var progress = Math.round(event.loaded / event.total * 100);
                    document.getElementById("status").innerHTML = "Progress " + progress + "%";
                }
    
                function uploadComplete(event) {
                    document.getElementById("status").innerHTML = event.target.responseText;
                }
            </script>
            <style>
                #dropbox {
                    width: 300px;
                    height: 200px;
                    border: 1px solid gray;
                    border-radius: 5px;
                    padding: 5px;
                    color: gray;
                }
            </style>
        </head>
        <body>
            <div id="dropbox">Drag and drop a file here...</div>
            <div id="status"></div>
        </body>
    </html>
    

    and this UploadServlet utilizing the new Servlet 3.0 HttpServletRequest#getPart() API:

    @MultipartConfig
    @WebServlet("/uploadServlet")
    public class UploadServlet extends HttpServlet {
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Part file = request.getPart("file");
            String filename = getFilename(file);
            InputStream filecontent = file.getInputStream();
            // ... Do your file saving job here.
    
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("File " + filename + " successfully uploaded");
        }
    
        private static String getFilename(Part part) {
            for (String cd : part.getHeader("content-disposition").split(";")) {
                if (cd.trim().startsWith("filename")) {
                    String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
                    return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
                }
            }
            return null;
        }
    }
    

    See also:

    • How to upload files to server using JSP/Servlet?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently using Google Analytics as a supplement to our paid tracking software, but neither
I am currently using the following command to upload my site content: scp -r
I'm currently using the awesome attachment-fu plugin for a Rails app, but as a
I am currently uploading files in ActionScript 3 using the upload() method of the
i am using the jQuery Plugin Uploadify to upload files, and i store the
I am currently using the Zend Framework and have an upload file form. An
I am using uploadify to allow images upload in a form. The issue i'm
Regarding Uploadify Hello, Currently I have a PHP only upload script and I'm planning
I'm using Flash uploader(uploadify, swfupload) with CodeIgniter, want to get the session data. I
I'm using Uploadify to upload some images with ASP.NET. I use Response.WriteFile() in ASP.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.