본 내용은 아래 링크에 나온 Request Object Example1의 한글 인코딩과 관련된 포스팅입니다. 

This post is related to the Korean encoding of Request Object Sample1 shown in the link below.

https://agilemeadow.tistory.com/manage/newpost/?type=post&returnURL=%2Fmanage%2Fposts%2F# 

 

TISTORY

나를 표현하는 블로그를 만들어보세요.

www.tistory.com

<HTML>

<html>
<head>
	<meta charset="UTF-8">
	<title>request Example1</title>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script>
	$(document).ready(function(){
		$("form").submit(function(){
			if($("#name").val()==""){
				alert("Please insert your name.");
				$("#name").focus();
				return false;
			}
			if($("#studentNum").val()==""){
				alert("Please insert your student number.");
				$("#studentNum").focus();
				return false;
			}
			if($("#male").is(":checked")==false &&
			   $("#female").is(":checked")==false){
				alert("Please select your sex.");
				return false;
			}
			if($("#major").val()=="#"){
				alert("Please select your major.");
				$("#major").focus();
				return false;
			}
		});	
	});	
	</script>
</head>
<body>
<h1>Request Example1</h1>

<form name=myform method=post action="requestExample1.jsp">
 Name : <input type="text" name="name" id="name" autofocus="autofocus"><br>
 Student No. : <input type="text" name="studentNum" id="studentNum"><br>
 Sex : Male <input  type="radio" name="sex"  value="male" id="male">
      Female <input type="radio" name="sex" value="female" id="female"><br>
      Prefer not to say <input type="radio" name="sex" value="prefernottosay" id="prefernottosay"><br>
 Major : <select name="major" id="major">
			<option value="#">Select your major</option>
			<option value="CS">Computer Science</option>
			<option value="EL">English Literature</option>
			<option value="M">Mathmetics</option>
			<option value="PS">Political Science</option>
			<option value="BM">Business Management</option>
		</select><br>
<input type="submit" value="submit">
<input type="reset" value="cancel">
</form>

</body>
</html>

<JSP>

인코딩은 항상 위에서 먼저 해야 합니다. 그렇지 않으면 한글 값이 깨집니다.

Post 방식으로 값을 전달 할 때는 Tomcat이 자동으로 인코딩하지 않으므로 인코딩을 먼저 시킨다음 값을 받아야 합니다. 

대부분의 전송방식이 Post 방식이기 때문에 한글 인코딩은 항상 빠지지 않는 중요한 문제입니다. 

Encoding should always be done first above. Otherwise, the Korean value is broken.
Tomcat does not automatically encode the value when forwarding it in the Post method, so you must encode it first and then receive it. 
Since most transmission methods are Post, encoding Korean is always an important issue.

<%@ page contentType="text/html;charset=utf-8"%>

	
<%	
// 한글 인코딩
// 1. 폼 파일에서 한글값이 get방식으로 전송될 때는 tomcat이 자동으로 utf-8로 인코딩을 시켜준다. 
// 2. 폼 파일에서 한글값이 post 방식으로 전송될 때는 tomcat이 자동으로 인코딩을 시켜주지 않기 때문에 
//    아래의 코드로 직접 인코딩해야 한다. 
// 폼파일에서 한글값이 post방식으로 전송될때 utf-8로 인코딩을 시켜주는 역할

// Korean encoding
// 1. When the Korean value is transmitted in the form file by the get method, tomcat automatically encodes it to utf-8. 
// 2. Tomcat does not automatically encode the Korean value in the form file when it is sent in the post method 
//    It shall be encoded directly into the code below. 
// The role of encoding the Korean values in the form file to utf-8 when they are transmitted by post.

	request.setCharacterEncoding("utf-8");
%>

<html>
<h1>Request Example1</h1>

<%
	String name = request.getParameter("name");
	String studentNum = request.getParameter("studentNum");
	String sex = request.getParameter("sex");
	String major = request.getParameter("major");
/* 
	if(sex.equals("m")){
		sex = "male";
	}else{
		sex = "female";
	} */
%>

<body>
Name: <%=name%><p>
Student No.: <%=studentNum%><p>
Sex : <%=sex%><p>
Major : <%=major%>
</body>
</html>

get방식과 post 방식의 차이에 대해 궁금하시다면 이 상단의 링크를 참조해주세요. 

If you are curious about the difference between get method and post method, please refer to the link above/ or refer to my last post, <JSP) Implicit Objects 1>.

 

Please comment below if you have any issues or questions about this post. 

'Java > JSP' 카테고리의 다른 글

JSP) Session  (0) 2022.08.29
JSP) Cookies  (0) 2022.08.28
JSP) Implicit Objects 3 - Out Object  (0) 2022.08.27
HTML / JSP / Javascript) Implicit Objects 2 - Response Object and Page Link  (0) 2022.08.26
JSP) Implicit Objects 1 - Request Object  (0) 2022.08.26

+ Recent posts