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

마크제이콥스

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

,