본문 바로가기

웹 프로그래밍/Back-End

Servlet Parameter 와 한글처리

반응형
  • Servlet Parameter

- Form 태그의 submit 버튼을 클릭하여 데이터를 서버로 전송하면, 해당파일(Servlet)에서는 HttpServletRequest객체를 이용하여 Parameter값을 얻을 수 있다.

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>

	<form action="FormEx" method = "post">
		이름 : <input type="text" name="name" size = "10"><br>
		아이디 : <input type="text" name="id" size = "10"><br>
		비밀번호 : <input type="password" name="pw" size = "10"><br>
		
		취미 : <input type="checkbox" name = "hobby" value = "read"> 독서
		<input type="checkbox" name = "hobby" value = "cook"> 요리
		<input type="checkbox" name = "hobby" value = "run"> 조깅
		<input type="checkbox" name = "hobby" value = "swim"> 수영
		<input type="checkbox" name = "hobby" value = "sleep"> 취침 <br>
		
		<input type="radio" name = "major" value = "kor"> 국어
		<input type="radio" name = "major" value = "eng" checked="checked"> 영어
		<input type="radio" name = "major" value = "mat"> 수학
		<input type="radio" name = "major" value = "des"> 디자인 <br>
		 
		 <select name = "protocol">
		 	<option value="http">http</option>
		 	<option value="ftp">ftp</option>
		 	<option value="smtp">smtp</option>
		 	<option value="pop">pop</option>
		 </select><br>
		 
		 <input type="submit" value="전송"><input type="reset" value="초기화">
	</form>
</body>
</html>
package com.javalec.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 FormEx
 */

@WebServlet("/FormEx")
public class FormEx extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FormEx() {
        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
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("doPost");
		
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		
		String[] hobbys = request.getParameterValues("hobby");
		String major = request.getParameter("mojor");
		String protocol = request.getParameter("protocol");
		
		response.setContentType("text/html; charset = EUC-KR");
		PrintWriter writer = response.getWriter();
		
		writer.println("<html><head></head><body>");
		writer.println("아이디 : " + id + "<br/>");
		writer.println("비밀번호 : " + pw + "<br/>");
		writer.println("취미 : " + Arrays.toString(hobbys) + "<br/>");
		writer.println("전공 : " + major + "<br/>");
		writer.println("프로토콜 : " + protocol);
		writer.println("</body></html>");
		
		writer.close();
	}

}

 

 


  • 한글처리

1. Get방식 요청 -> server.xml 수정

<Connector connectionTimeout="20000" port="8181" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector URIEncoding = "EUC-KR" connectionTimeout="20000" port="8181" protocol="HTTP/1.1" redirectPort="8443"/>

 

2. Post 방식 요청 -> request.setCharacterEncoding() 메소드 이용

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("doPost");
		
		request.setCharacterEncoding("EUC-KR");
		
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		
		String[] hobbys = request.getParameterValues("hobby");
		String major = request.getParameter("mojor");
		String protocol = request.getParameter("protocol");
		
		response.setContentType("text/html; charset = EUC-KR");
		PrintWriter writer = response.getWriter();
		
		writer.println("<html><head></head><body>");
		writer.println("이름 : " + name + "<br/>");
		writer.println("아이디 : " + id + "<br/>");
		writer.println("비밀번호 : " + pw + "<br/>");
		writer.println("취미 : " + Arrays.toString(hobbys) + "<br/>");
		writer.println("전공 : " + major + "<br/>");
		writer.println("프로토콜 : " + protocol);
		writer.println("</body></html>");
		
		writer.close();
	}

 

반응형

'웹 프로그래밍 > Back-End' 카테고리의 다른 글

Django - SQL  (0) 2020.09.10
Django - CRUD  (0) 2020.09.03
Django 기초  (0) 2020.08.28
Servlet 라이프사이클(생명주기), 선처리와 후처리  (0) 2020.03.11
web.xml 에 서블릿 맵핑  (0) 2019.12.08