본문 바로가기

기존카테고리/JSP

서블릿 한글처리

package com.test.ex;


import java.io.IOException;

import java.io.PrintWriter;

import java.util.Arrays;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class FormDemo_01

 */

@WebServlet("/FormD_01")

public class FormDemo_01 extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public FormDemo_01() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("doPost");

request.setCharacterEncoding("UTF-8");

//request.setCharacterEncoding("EUC-KR");


// 한글처리

// 톰캣서버의 default 문자 처리 방식은 ISO-8859-1 방식이다.

// 개발자는 별도의 한글 인코딩을 하지 않으면 한글이 깨질 수 있다.

// get 방식과 post 방식의 한글처리가 다르다.

// get 방식의 한글처리는 Servers/톰캣서버 폴더내에 있는 server.xml에서 <connector >에

// URIEncoding="EUC-KR"

// post 방식의 한글처리는 doPost메소드 내에서 설정한다.

// request.setCharacterEncoding("EUC-KR");

String name = request.getParameter("name");

String id = request.getParameter("id");

String pass = request.getParameter("pw");

String[] hobbys = request.getParameterValues("hobby");

String sex = request.getParameter("gender");

String local = request.getParameter("local");

response.setContentType("text/html; charset=euc-kr");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head></head>");

out.println("<body>");

out.println("이름 : "+name+"<br/>");

out.println("아이디 :"+id+"<br/>");

out.println("비밀번호 :"+pass+"<br />");

out.println("취미 :"+Arrays.toString(hobbys)+"<br/>");

out.println("성별 :"+sex+"<br/>");

out.println("지역 :"+local+"<br/>");

out.println("</body></html>");

out.close();

}


}



server.xml 수정 : utf-8로 수정예시



<!-- A "Connector" represents an endpoint by which requests are received

         and responses are returned. Documentation at :

         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)

         Java AJP  Connector: /docs/config/ajp.html

         APR (HTTP/AJP) Connector: /docs/apr.html

         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080

    -->

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

    <!-- A "Connector" using the shared thread pool-->



'기존카테고리 > JSP' 카테고리의 다른 글

ServletContext를 이용한 데이터 공유  (0) 2017.06.06
context path, web.xml을 이용한 서블릿 초기화  (0) 2017.06.06
서블릿 form 전송과 결과값 출력  (0) 2017.06.06
서블릿3  (0) 2017.06.06
서블릿2  (0) 2017.06.06