I had some files in my webapp in a simple folder in the WAR file. The requests were very fast: as soon as the browser cached a file, it did not request the file contents again until the file changed.
Now I put the files in a different location, and implemented a servlet to deliver the files. The code is simple, but the performance drops. This is the minimal example:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>File</servlet-name>
<servlet-class>test.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>File</servlet-name>
<url-pattern>/file/*</url-pattern>
</servlet-mapping>
</web-app>
FileServlet
public class FileServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
FileSystem fs = FileSystems.getDefault();
Path path = fs.getPath("/some/folder/", req.getPathInfo());
Files.copy(path, resp.getOutputStream());
}
}
How can I improve it, so that I get the same performance as, for instance, tomcat's DefaultServlet
?