Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Servlets Code Examples

JSP Http servlet response wrapper

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
JSP Http servlet response wrapper package com.ack.web.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; /** * JSPHttpServletResponseWrapper is used to adapt the response of a servlet. * The wrappers sole responsibility is to create a JSPServletOutputStream * off the provided HttpServletResponse. The JSPServletOutputStream is when * we can customize when is streamed back to the HTTP client. The * JSPHttpServletResponseWrapper provides the infrastructural scaffolding to * plug into our specialising output stream. */ public class JSPHttpServletResponseWrapper extends HttpServletResponseWrapper { protected ServletOutputStream stream = null; protected PrintWriter writer = null; protected HttpServletResponse origResponse = null; public JSPHttpServletResponseWrapper( HttpServletResponse response ) { super( response ); response.setContentType("text/plain"); origResponse = response; } public ServletOutputStream createOutputStream() throws IOException { return ( new JSPServletOutputStream( origResponse ) ); } public ServletOutputStream getOutputStream() throws IOException { if( writer != null ) { throw new IllegalStateException( "getWriter() has already been " + "called for this response" ); } if( stream == null ) { stream = createOutputStream(); } return stream; } public PrintWriter getWriter() throws IOException { if( writer != null ) { return writer; } if( stream != null ) { throw new IllegalStateException( "getOutputStream() has already " + "been called for this response" ); } stream = createOutputStream(); writer = new PrintWriter( stream ); return writer; } }