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
블로그 이미지

마크제이콥스

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

,

Filter HTML 태그 제거


chain.doFilter(new HTMLTagFilterRequestWrapper((HttpServletRequest)request), response);


package egovframework.rte.ptl.mvc.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class HTMLTagFilterRequestWrapper extends HttpServletRequestWrapper
{

    public HTMLTagFilterRequestWrapper(HttpServletRequest request)
    {
        super(request);
    }

    public String[] getParameterValues(String parameter)
    {
        String values[] = super.getParameterValues(parameter);
        if(values == null)
            return null;
        for(int i = 0; i < values.length; i++)
            if(values[i] != null)
            {
                StringBuffer strBuff = new StringBuffer();
                for(int j = 0; j < values[i].length(); j++)
                {
                    char c = values[i].charAt(j);
                    switch(c)
                    {
                    case 60: // '<'
                        strBuff.append("&lt;");
                        break;

                    case 62: // '>'
                        strBuff.append("&gt;");
                        break;

                    case 38: // '&'
                        strBuff.append("&amp;");
                        break;

                    case 34: // '"'
                        strBuff.append("&quot;");
                        break;

                    case 39: // '\''
                        strBuff.append("&apos;");
                        break;

                    default:
                        strBuff.append(c);
                        break;
                    }
                }

                values[i] = strBuff.toString();
            } else
            {
                values[i] = null;
            }

        return values;
    }

    public String getParameter(String parameter)
    {
        String value = super.getParameter(parameter);
        if(value == null)
            return null;
        StringBuffer strBuff = new StringBuffer();
        for(int i = 0; i < value.length(); i++)
        {
            char c = value.charAt(i);
            switch(c)
            {
            case 60: // '<'
                strBuff.append("&lt;");
                break;

            case 62: // '>'
                strBuff.append("&gt;");
                break;

            case 38: // '&'
                strBuff.append("&amp;");
                break;

            case 34: // '"'
                strBuff.append("&quot;");
                break;

            case 39: // '\''
                strBuff.append("&apos;");
                break;

            default:
                strBuff.append(c);
                break;
            }
        }

        value = strBuff.toString();
        return value;
    }
}


'JAVA' 카테고리의 다른 글

[JAVA]filter multpart  (0) 2017.11.16
[JAVA]Filter HttpServletResponseWrapper HTML 소스 컨트롤  (0) 2017.11.16
[JAVA] extends T  (0) 2017.11.07
[JAVA] 에러정의 web.xml  (0) 2017.11.07
[JAVA] error  (0) 2017.11.06
블로그 이미지

마크제이콥스

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

,