javax.servlet Code Samples
- How do I get servlet request URL information?
- How do I get web application context path?
- How do I read text file in Servlet?
- How do I create a HelloWorld Servlet?
- How do I get Spring's Bean from a servlet?
- How do I create a hit counter Servlet?
- How do I read servlet context initilization parameters?
- How do I get client IP and hostname in Servlet?
- How do I count number of online users?
- How do I get parameter names from servlet request?
- How do I invalidate user's session?
- How do I get servlet request headers information?
- How do I read request parameter from servlet?
- How do I send a response status in servlet?
Page of 2 |
Prev
|
Next
How do I create zip file in Servlet for download?
The example below is a servlet that shows you how to create a zip file and send the generated zip file for user to download. The compressing process is done by the zipFiles method of this class.
For a servlet to work you need to configure it in the web.xml file of your web application which can be found after the code snippet below.
The web.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>ZipDownloadServlet</servlet-name>
<servlet-class>org.kodejava.example.servlet.ZipDownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ZipDownloadServlet</servlet-name>
<url-pattern>/zipservlet</url-pattern>
</servlet-mapping>
</web-app>
