<main.jsp> (꼭 jsp로 작업하지 않아도 된다.)


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script type="text/javascript">


var xhr = null;

function getXMLHttpRequest() {

if (window.ActiveXObject) {

try {

return new ActiveXObject("Msxml2.XMLHTTP");

} catch (e1) {

try {

return new ActiveXObject("Microsoft.XMLHTTP");

} catch (e2) {

return null;

}

}

} else if (window.XMLHttpRequest) {

return new XMLHttpRequest();

} else {

return null;

}

}


function requestHello(URL) {

param = f.name.value;

URL = URL + "?name=" + encodeURIComponent(param);

xhr = getXMLHttpRequest();

xhr.onreadystatechange = responseHello;

xhr.open("GET", URL, true);

xhr.send(null);

}

function responseHello(){

if(xhr.readyState == 4){

var str = xhr.responseText;

document.getElementById("message").innerHTML = str;

}else{

alert(!"Fail : "+httpRequest.status);

}

}

</script>

</head>

<body>

<form name="f">

<input type="text" name="name">

<input type="button" value="입력 " onclick="requestHello('hello.jsp')">

</form>

<div id="message"></div>


</body>

</html>



------------------------------------------------------------------


<hello.jsp>


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

request.setCharacterEncoding("UTF-8");

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

%>

안녕하세요, <%=name %> 회원님!

</body>

</html>




================================================================================================




저렇게만 한다면 분명히 한글이 깨질 것이다.


 이 부분을 -> main.jsp : escape(encodeURIComponent(param))

hello.jsp :  java.net.URLDecoder.decode(request.getParameter("name"),"utf-8")


이렇게 다시 바꾸고 실행해보자.

그럼 한글이 제대로 나온다.


그래도 한글이 깨져서 나온다면 잘 저장하고 tomcat 을 restart 하면 된다.


'Client Standard > Ajax' 카테고리의 다른 글

[Jsonp] POST method 데이터전송  (0) 2017.03.30
[Ajax] Http/Https 크로스도메인 문제  (0) 2017.03.27
Js, Ajax Example  (0) 2013.02.23

+ Recent posts