Filter HttpServletResponseWrapper HTML 소스 컨트롤


package keis.user.comm;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;


public class ResponseFilter implements Filter{

public void destroy() {
  
 }

       public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
  
            System.out.println("Filter.doFilter()");
        

HelloWorldResponse myResponse = new HelloWorldResponse((HttpServletResponse)res); 
chain.doFilter(req, myResponse);

// ----------- FilterChain의 호출이 끝나고 Servlet의 Service의 호출까지 끝난시점 --

// 이후에는 Response의 데이터를 캡쳐해 처리가 가능합니다
        
        res.setContentType("text/html");
        res.setCharacterEncoding("ksc5601");
        
        PrintWriter out = res.getWriter();
        String str = myResponse.toString();
        out.println("<root>");
        out.println("<value>");
        out.println(str.replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
        out.println("</value>");
        out.println("</root>");  
 }

 public void init(FilterConfig arg0) throws ServletException {
  
 }

}
==============================================


package keis.user.comm;

import java.io.CharArrayWriter;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class HelloWorldResponse extends HttpServletResponseWrapper{
    private CharArrayWriter buffer;

    public HelloWorldResponse(HttpServletResponse response) {
        super(response);
        buffer = new CharArrayWriter();
    }
    
    public PrintWriter getWriter() {
        return new PrintWriter(buffer);
    }
    
    public String toString() {
        return buffer.toString();
    }
}


'JAVA' 카테고리의 다른 글

[JAVA]Filter responseWrapper  (0) 2017.11.16
[JAVA]filter multpart  (0) 2017.11.16
[JAVA]Filter HTML 태그 제거  (0) 2017.11.08
[JAVA] extends T  (0) 2017.11.07
[JAVA] 에러정의 web.xml  (0) 2017.11.07
블로그 이미지

마크제이콥스

초보 개발자의 이슈및 공부 내용 정리 블로그 입니다.

,