package forms; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.ecs.*; import org.apache.ecs.html.*; import java.util.zip.*; /** * A servlet to demonstrate how to compressed data sent to the client * It also uses ECS to format the results. * A compressed version of Parameters3 * * @version 1.0 2001/08/15 * @since 1.0 * @author Mike Gehard */ public class CompressedReg extends HttpServlet{ // used so we can tell client how long the response is private ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024); private String compressionType; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // determine what type of encoding we are going to use OutputStream compressStream = getCompressionStream(request, response); response.setContentType("text/html"); // you will write to the compression stream, not the response stream PrintWriter out = new PrintWriter(compressStream); out.println(createPage(request)); out.close(); // tell the client how much data they are getting response.setContentLength(byteStream.size()); // actually write the data to the client byteStream.writeTo(response.getOutputStream()); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // forward to doPost() doPost(request, response); } private String createPage(HttpServletRequest request){ Table table = new Table(0); // border = 0 // create the HTML tree Html html = new Html(); Head head = new Head(); Title title = new Title("Registration"); Body body = new Body(); head.addElement(title); body.addElement(table); html.addElement(head); html.addElement(body); body.addElement(new H1("Thank you! Your registration is confirmed.")); body.addElement(request.getParameter("salutation") + " " + request.getParameter("lastName") + " " + request.getParameter("firstName")); body.addElement(new BR()); body.addElement(request.getParameter("company")); body.addElement(new BR()); body.addElement("Email: " + request.getParameter("email")); body.addElement(new BR()); body.addElement("Primary area of business: " + request.getParameter("business")); body.addElement(new BR()); body.addElement("Interest areas"); String values[] = request.getParameterValues("interests"); //System.out.println("values[] length = " + values.length); if (values != null) { UL list = new UL(); for (int i = 0; i < values.length; i++) { list.addElement(new LI(values[i])); } body.addElement(list); } body.addElement("Comments: " + request.getParameter("comments")); body.addElement(new HR()); body.addElement(compressionType); return html.toString(); } private OutputStream getCompressionStream(HttpServletRequest request, HttpServletResponse response) throws IOException{ // the default value for the return OutputStream retVal = byteStream; compressionType = "No Compression"; String encodings = request.getHeader("Accept-Encoding"); if (encodings != null && encodings.indexOf("gzip") >= 0){ // tell client they are getting gzip response.setHeader("Content-Encoding", "gzip"); retVal = new GZIPOutputStream(byteStream); compressionType = "GZip Compressed"; } else if (encodings != null && encodings.indexOf("compress") >= 0){ // tell client they are getting compress response.setHeader("Content-Encoding", "compress"); retVal = new ZipOutputStream(byteStream); compressionType = "Zip Compressed"; } return retVal; } }