Created
September 11, 2012 09:54
-
-
Save vivizhyy/3697317 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.CharArrayWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpServletResponseWrapper; | |
public class CharArrayWriterResponse extends HttpServletResponseWrapper { | |
private final CharArrayWriter charArray = new CharArrayWriter(); | |
public CharArrayWriterResponse(HttpServletResponse response) { | |
super(response); | |
} | |
@Override | |
public PrintWriter getWriter() throws IOException { | |
return new PrintWriter(charArray); | |
} | |
public String getOutput() { | |
return charArray.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
@SuppressWarnings("serial") | |
public class ServletUsingCustomResponse extends HttpServlet { | |
@Override | |
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
String template = req.getParameter("tmpl"); | |
CharArrayWriterResponse customResponse = new CharArrayWriterResponse(resp); | |
req.getRequestDispatcher(template).forward(req, customResponse); | |
System.out.println(String.format("The output of %s is %s", template, customResponse.getOutput())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment