Java Web App - Servlet - File Download

This is fun as well..

Show me the code, show me the code..
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
    <groupId>biz.tugay</groupId>
    <artifactId>download</artifactId>
    <packaging>war</packaging>
 
    <version>1.0-SNAPSHOT</version>
    <name>Download Application</name>
 
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>Download Application</finalName>
    </build>
 
</project>

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <scripting-invalid>true</scripting-invalid>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
            <default-content-type>text/html;charset=UTF-8</default-content-type>
        </jsp-property-group>
    </jsp-config>
 
</web-app>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>File Download Example!</title>
    <meta charset="utf-8">
</head>
<body>
 
<a href="<c:url value="/getKorayTxt" />">Download Koray's File!</a>
 
</body>
</html>

package biz.tugay;
/* User: koray@tugay.biz Date: 19/01/16 Time: 20:13 */
 
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
 
@WebServlet(urlPatterns = "/getKorayTxt")
public class KorayDownload extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("application/octet-stream");
        resp.setHeader("Content-Disposition", "attachment; filename=koray.txt");
        final InputStream resourceAsStream = getServletContext().getResourceAsStream("/koray.txt");
        final ServletOutputStream outputStream = resp.getOutputStream();
        int read;
        while ((read = resourceAsStream.read()) != -1) {
            outputStream.write(read);
        }
        outputStream.flush();
        outputStream.close();
    }
}

And some notes:

The getResourceAsStream() requires you to start with a forward slash (“/”) , which represents the root of your web app.

In this example koray.txt should be under WEB-INF folder.