Javascript의 encodeURI() 함수를 이용하여 URL인코딩을 수행하여 GET방식으로 서버측에 전송하는 예
encode.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript">
function test(){
var address = "서울시 종로구 효제동";
/* url encode 2가지 방법 */
var encoded1 = window.encodeURI( address );
var encoded2 = window.encodeURIComponent( address );
/* 유니코드로 변환하는 예 */
var encoded3 = window.escape( address );
document.getElementById("span1").innerHTML = encoded1;
document.getElementById("span2").innerHTML = encoded2;
document.getElementById("span3").innerHTML = encoded3;
location.href = "decode.jsp?title="+encoded1;
}
</script>
</head>
<body><center>
<input type="button" value="Request" onClick="test();"/>
<p/>
encodeURI('한글제목')==> <span id="span1"></span><p/>
encodeURIComponent('한글제목')==> <span id="span2"></span><p/>
escape('한글제목')==> <span id="span3"></span><p/>
</center>
</body>
</html>
decode.jsp (URL인코딩된 문자열을 서버측에서 수신하여 URL 디코딩한 후에 사용하는 예)
pageEncoding="EUC-KR"%>
<%
String tmp = request.getParameter("address");
String address = new String(tmp.getBytes("8859_1"), "utf-8");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>decode 결과</title>
</head>
<body>
<center>
서버측에서 java.net.URLDecoder를 이용하여 디코드한 결과<p/>
<%=address%><p/>
</center>
</body>
</html>