테스트 JSP페이지
- /view/encodingTest.jsp,
- /view/to.jsp
encodingTest.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/view/to.jsp" method="post">
<input type="text" name="id">
<input type="submit" value="전송">
</form>
</body>
</html>
to.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<%
String id = request.getParameter("id");
%>
출력 : <%= id %>
</body>
</html>
필터 설정 전 테스트를 하면 한글이 깨진다.
필터 클래스 : CharacterEncodingFilter
package filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;
@WebFilter(urlPatterns = "/*",
initParams = {@WebInitParam(name = "encoding", value = "utf-8")}
)
public class CharacterEncodingFilter implements Filter {
private String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("encoding");
if(encoding==null){
encoding = "UTF-8";
}
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding(encoding);
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
jsp 회원제 게시판(2) : 한글 필터, 핸들러 인터페이스, 컨트롤러 (0) | 2020.07.06 |
---|---|
jsp 회원제 게시판(1) : DB, 의존성, 커넥션 풀 (0) | 2020.07.06 |
Filter interface, filter setting, 필터 설정, 필터 인터페이스 (0) | 2020.07.06 |
서블릿 컨트롤러, 커맨드 패턴, URI 사용 (0) | 2020.07.06 |
서블릿 리스너 : ServletContextListener 인터페이스 @WebListener (0) | 2020.07.06 |
댓글 영역