Table of contents
1.
Introduction
2.
Understanding with an Example
2.1.
Writing Backend Script
2.1.1.
Explanation
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

JSP - File Downloading

Author Naman Kukreja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Sometimes, the users need to download the file from the website for its use, but how can they do this?

This is possible because of the file downloading feature provided by JSP files. We will discuss this more while moving further in the blog.

Understanding with an Example

In this example, we are going to study the download feature.

Here we are going to click on a button for downloading a file:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="download.jsp">click here to download the jsp file</a>
</body>
</html>

 

In the above code, we download the file in the index.html file using the download.jsp file. We are creating a FileInputStream for adding paths and files.

Writing Backend Script

Here we are writing the code for defining the processes and their corresponding location that will trigger when the user clicks on the link for downloading: 

package demotest;

 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 
public class ninja_download extends HttpServlet {
    private static final long serialVersionUID = 1L;

 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String ninjafile = "test.txt";
        String ninjapath = "c:/ninja/upload/";
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=\""
                + ninjafile + "\"");
 
        FileInputStream fileInputStream = new FileInputStream(ninjapath
                + ninjafile);
 
        int i;
        while ((i = fileInputStream.read()) != -1) {
            out.write(i);
        }
        fileInputStream.close();
        out.close();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 } 
}

Explanation

  1. First, we import IO Exception, Print Writer, and FileInputStream from java.io.package.
  2. Then we are defining ninja_download server, which extendsHttpServlet.
  3. Here GET method will be processed as we have defined a href in the enclosed URL.
  4. Then, define a variable named ninjafile and set the content type using a response object.
  5. We are adding ninjapath + ninjafile add in FileInputStream.
  6. Here we use the while loop and use the condition as !=1. 

 

You can also read about the JSP Architecture and Lifecycle.

FAQs

  1. Name some methods to include the results of another page.
    We can use the include directive and include action method.
     
  2. Name some liberals used in JSP.
    Some literals used in JSP are boolean, null, string, float, integer, etc.
     
  3. Mention some advantages of servlets?
    Some advantages are efficiency, integration, scalability, etc.
     
  4. How to download the file in JSP?
    You can download the file by clicking on the hyperlink provided in the particular website.

Key Takeaways

In this article, we have learned how to download the file in JSP and see an example of how to write background code.

Along with downloading, most people want to learn about uploading, so if you want to learn more about uploading files in JSP, you must refer to this blog. You will get a complete idea of how to upload files with an example.

Live masterclass