위로 아래

한글 깨질 시

이클립스 -> Servers -> Tomcat -> server.xml 안에서 이거 맨 뒤에 URIEncoding="UTF-8"이거 붙이면 한글 설정

    <Connector connectionTimeout="20000" maxParameterCount="1000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

doget에

response.setContentType("text/html; charset=UTF-8");

dopost에

request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("UTF-8");

 

 


필터 설정

src/main/java 폴더에 필터 붙여넣기

더보기
package filter;

import java.io.IOException;

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

public class CommonEncodingFilter implements Filter {

    /**
     * 인코딩을 수행할 인코딩 캐릭터 셋 지정
     */
    private String encoding = null;

    /**
     * 필터 설정 관리자
     */
    protected FilterConfig filterConfig = null;

	
    /**
     * @return
     */
    public FilterConfig getFilterConfig() {
        return filterConfig;
    }

    /**
     * @param cfg
     */
    public void setFilterConfig(FilterConfig cfg) {
        filterConfig = cfg;
    }

	@Override
	public void destroy() {
		this.encoding = null;
	    this.filterConfig = null;

	}

	@Override
	public void doFilter(ServletRequest request, 
			   ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		  
		if (request.getCharacterEncoding() == null) {
	            if (encoding != null) {
	              request.setCharacterEncoding(encoding);
	            }
	        }

	        chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	    this.filterConfig = filterConfig;
	    this.encoding = filterConfig.getInitParameter("encoding");

	}

}

web.xml 파일에 필터 넣어주기

더보기
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>jspEx</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
   <filter>
        <filter-name>Encoding Filter</filter-name>
        <filter-class>filter.CommonEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>Encoding Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

 


STS에서

이클립스 jsp에서

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>

<meta charset="UTF-8">

가져와서 붙여주기