gwooden_코린이
서블릿 필터 기능 본문
728x90
1. 서블릿 필터 기능
(귀찮음)
클라이언트 -> jsp 한글이 깨져서 encoding utf-8 매번 설정
(필터 설정으로 인해 귀찮음 해결)
클라이언트 -> 필터(encoding utf-8) -> jsp
-> jsp
-> jsp
package com.study.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class TestFilter implements Filter {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
// TODO Auto-generated method stub
}
}
package com.study.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class TestFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
}
}
singup.jsp 코드 ▼
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:setLocale value='<%=request.getParameter("lang") %>' />
<fmt:bundle basename="bundle.singupBundle">
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body>
<div class="alert alert-secondary fs-3" role="alert">
<fmt:message key="title"/>
</div>
<div class="container mt-3">
<div class="text-end">
<a class="btn btn-primary me-md-2" href="?lang=ko">한국어</a>
<a class="btn btn-primary" href="?lang=en">English</a>
</div>
<form method="post" action="test.jsp">
<div class="mb-3">
<label for="name" class="form-label"><fmt:message key="name"/></label>
<input type="text" class="form-control" id="name" name="name">
</div>
<div class="mb-3">
<label for="id" class="form-label"><fmt:message key="id"/></label>
<input type="text" class="form-control" id="id" name="id">
</div>
<div class="mb-3">
<label for="pw" class="form-label"><fmt:message key="pw"/></label>
<input type="password" class="form-control" id="pw">
</div>
<div class="mb-3">
<select class="form-select">
<option value="1"><fmt:message key="list1"/></option>
<option value="2"><fmt:message key="list2"/></option>
<option value="3"><fmt:message key="list3"/></option>
</select>
</div>
<div class="mb-3">
<input type="radio" name="gender" value="male"><fmt:message key="male"/>
<input type="radio" name="gender" value="female"><fmt:message key="female"/>
</div>
<button type="submit" class="btn btn-primary"><fmt:message key="btn"/></button>
</form>
</div>
</body>
</html>
</fmt:bundle>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<%=request.getParameter("name") %><br>
<%=request.getParameter("id") %><br>
</body>
</html>
어노테이션
web.xml
import javax.servlet.annotation.WebFilter;
@WebFilter("/*")
.클라이언트가 singup.jsp 이것좀 나한테 보내주세요(요청)
(중간에 필터가 존재) 필터 작
서버에서 수락 후 singup.jsp 클라이언트에게 응답
chain.doFilter(request, response);
위 코드를 넣으면 필터를 거쳐 정상정인 화면이 출력이 된다.
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
어떤 페이지에서든 utf-8 한글깨짐을 방지하기 위한 설정이 완료!
@WebFilter("/*") 어노테이션을 사용하는 방법 외에도 web.xml을 이용한 필터 설정도 가능하다.
<filter>
<filter-name>testFilter</filter-name>
<filter-class>com.study.filter.TestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>testFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
728x90
'JSP&Servlet' 카테고리의 다른 글
데이터베이스 커넥션 풀 (DBCP) (0) | 2023.01.30 |
---|---|
이클립스 JSP/서블릿 JDBC 사용해보기 (0) | 2023.01.27 |
jsp를 이용한 회원가입 페이지 한국어/영어 구현해보기 (0) | 2023.01.27 |
fmt 태그 사용해보기 (0) | 2023.01.26 |
JSTL 자카르타 아파치 사용해보기 (0) | 2023.01.26 |
Comments