Is there a way to incorporate a progress bar into the Java file uploading example?

I'm currently developing a JSP/Servlet web application and I am looking to implement file upload functionality with a progress bar within a Servlet context. Below is an example of uploading a file using JSP and Servlet but without the progress bar feature.

The FileUploadHandler.java

 import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet that manages File upload requests from Clients
 * @author Javin Paul
 */
public class FileUploadHandler extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final String UPLOAD_DIRECTORY = "uploads";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uploadPath = getServletContext().getRealPath("")
                + File.separator + UPLOAD_DIRECTORY;

        String name = null;
        //process only if its multipart content
        if(ServletFileUpload.isMultipartContent(request)){
            try {
                List<FileItem> multiparts = new ServletFileUpload(
                                         new DiskFileItemFactory()).parseRequest(request);

                for(FileItem item : multiparts){
                    if(!item.isFormField()){
                         name = new File(item.getName()).getName();
                        item.write( new File(uploadPath+name));

                    }
                }

               //File uploaded successfully
               request.setAttribute("message", "File Uploaded Successfully "+uploadPath+ name);
            } catch (Exception ex) {
               request.setAttribute("message", "File Upload Failed due to " + ex);
            }          

        }else{
            request.setAttribute("message",
                                 "Sorry this Servlet only handles file upload request");
        }

        request.getRequestDispatcher("/result.jsp").forward(request, response);

    }

}

The upload.jsp

The result.jsp

The web.xml

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering ElementNotInteractableException while using Selenium in Java to send Arrow Up to a number input

When trying to input a salary into a tax calculator website, I encountered an issue where an arrow_up or arrow_down key needed to be pressed in order for the tax amounts to be calculated instead of using the enter key. Despite researching online and findin ...

Generating a JSON file dynamically containing hierarchical data

I am looking to create a JSON file structure similar to the example provided in this post, but I need it to be dynamic. The current solution is specific for "entry1" and "entry2", however, I want the flexibility to use any entity name such as customer, add ...

Optimizing the performance of J2EE web applications

I am currently working on enhancing the performance of my web application. The application is java-based and is hosted on an Amazon cloud server with JBoss and Apache. One particular page in the application is experiencing a slow loading time of 13-14 sec ...

Using PHP to encode JSON and submit it via POST request

Issue: I am encountering difficulty in sending a JSON-encoded variable to a PHP page using jQuery/AJAX. Here is my current approach. The file is uploaded through It undergoes processing using jQuery based on the following JS code. JS code: <scrip ...

Problem occurred while processing base64 encoded image via AJAX request due to failure in opening the stream

Managing a blog that showcases various images can be challenging, especially when it comes to optimizing server requests. One method I employ is encoding each image to base64 using a simple PHP function. Incorporating an infinite scroll feature on my blog ...

Effect of Ajax calls on pagination in Laravel Views

I have a Laravel project in userlist.blade.php where I display the list of users. Everything is working fine, but I have used a select field to limit the pagination by different values. With the help of Nitish Kumar on Stack Overflow, I was able to retriev ...

Selenium Test Case Execution Encountered a Null Pointer Exception

I am new to Selenium Java programming. I encountered an error when trying to run the script below using TestNG. Error Message "java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)&quo ...

Using Accordions in Jquery to dynamically adjust page height during ajax calls

I am currently using AJAX to call in a page and animate its height successfully. However, I have encountered an issue with an accordion-like function that is supposed to toggle the visibility of an element and adjust the height of the containing element ac ...

What order should jquery files be included in?

Today I ran into an issue while trying to add datepicker() to my page. After downloading jqueryui, I added the scripts in the following order: <script type="text/javascript" src="js/jquery.js"></script> <script src="js/superfish.js">< ...

Volley JSON Exception: Unexpected end of data at position 0

I have encountered a problem while trying to utilize Volley for REST calls. Specifically, when attempting to make a Put call with a JSON Object as a parameter, I am receiving an error message stating: error with: org.json.JSONException: End of input at cha ...

Error authorizing AJAX call to Gmail API

I'm just getting started with the GMail API and I'm attempting to use AJAX to fetch emails. This is my code: $.ajax({ beforeSend: function (request) { request.setRequestHeader("authorization", "Bearer xxxxxxxxxxxxxxxxx.a ...

HTML5 Audio using AudioJS may not function reliably when loaded remotely

I have been encountering frustrating issues with loading audio tags for a while now. Despite spending nearly two weeks trying to solve the problems, every fix seems to create another one. My webpage includes an < audio > tag that utilizes audio.js to m ...

Ajax is updating the information stored in the Data variable

Recently, I reached out to tech support for help with an issue related to Ajax not executing properly due to Access-Control-Allow-Origin problems. Fortunately, the technician was able to resolve the issue by adding a file named .htaccess with the code Head ...

Sending string variable from Perl CGI script to HTML frontend

Having just begun experimenting with AJAX, I'm encountering difficulties in passing variables back to my HTML script. In this setup, I'm using a combination of a XAMPP webserver, JavaScript, and jQuery for the HTML script, along with a Perl cgi ...

Information inputted through Selenium webdriver's sendKeys() function fails to be recognized

My current project involves automating a user journey on an insurance web application. The process includes interacting with two fields that are populated based on a hidden list of matching selections. As the user begins typing, a list of options appears f ...

Ways to showcase INPUT TYPE when making a Selection?

I've been struggling with a simple issue and despite trying multiple solutions, I can't seem to get it right. I have a form where I'm using the <select> tag with two options: coo and uh. What I want is for an additional input type fiel ...

Unneeded return is necessary when utilizing recursion and restarting the application

Recently, I successfully recreated the 2048 game in Java, which was a pleasant surprise to me as I didn't expect to create something this "advanced." During the game, when tiles are moved, a new square/tile/number needs to be generated. To find an av ...

What is the best way to retrieve all variable names from a JSON file?

Is there a way to extract an array of variable names from JSON data? For instance, if I'm given: { "Java": 20526, "Shell": 292, "Groovy": 213 } I aim to transform it into an array like this: String[] {"Java", "Shell", "Groovy"} Any suggestio ...

Executing multiple test classes with TestNG while utilizing page factory

I have developed two classes for locating elements on two web pages within the same package, named LoginPage.java and AddEmployee.java. Additionally, I have created two test classes in a separate package to correspond with the aforementioned classes, named ...

Unresponsive Ajax Button

Trying to create a follow button in WordPress, but encountering an issue with the Ajax functionality not working correctly. Here is the sequence of events that should take place: User clicks on #followbtn Ajax sends request to $_POST action which is set ...