1.xmlPostClient.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function sendXML(){
createXMLHttpRequest();
var xml="<VCOUPON><TEL_NO>0101234567</TEL_NO><AMOUNT>1000</AMOUNT><RETURNCODE>00</RETURNCODE></VCOUPON>";
var url="http://localhost:8080/test/httpPostServer.jsp";
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.setRequestHeader("aaa","aaa");
xmlHttp.setRequestHeader("bbb","bbb");
xmlHttp.send(xml);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
parseResults();
}
}
}
function parseResults(){
var responseDiv=document.getElementById("response");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodex[0]);
}
var responseText = document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}
</script>
</head>
<body>
<input type="button" value="보내기" onClick="sendXML();"><br>
<div id="response">
</div>
</body>
</html>
2.xmlPostServer.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import = "java.io.*" %>
<%@ page import = "javax.xml.parsers.DocumentBuilderFactory" %>
<%@ page import = "javax.xml.parsers.ParserConfigurationException" %>
<%@ page import = "org.w3c.dom.Document" %>
<%@ page import = "org.w3c.dom.NodeList" %>
<%@ page import = "org.xml.sax.SAXException" %>
<%!
public String readXMLFromRequestBody(HttpServletRequest request){
StringBuffer xml = new StringBuffer();
String line = null;
try{
BufferedReader reader = request.getReader();
while((line=reader.readLine())!=null){
xml.append(line);
}
}catch(Exception e){
System.out.println("Error reading XML:"+e.toString());
}
return xml.toString();
}
%>
<%
String xml=readXMLFromRequestBody(request);
Document xmlDoc = null;
try{
xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
}catch(ParserConfigurationException e){
System.out.println("ParserConfigurationException : "+e);
}catch(SAXException e){
System.out.println("SAXException : "+e);
}
System.out.println("header aaa : "+request.getHeader("aaa"));
System.out.println("header bbb : "+request.getHeader("bbb"));
NodeList TEL_NO = xmlDoc.getElementsByTagName("TEL_NO");
NodeList AMOUNT = xmlDoc.getElementsByTagName("AMOUNT");
NodeList RETURNCODE = xmlDoc.getElementsByTagName("RETURNCODE");
String telno = null;
String amount = null;
String returncode = null;
String responseText = "request text : ";
for(int i = 0; i < TEL_NO.getLength();i++){
telno = TEL_NO.item(i).getFirstChild().getNodeValue();
responseText = responseText+"TEL_NO : "+telno;
}
for(int i = 0; i < AMOUNT.getLength();i++){
amount = AMOUNT.item(i).getFirstChild().getNodeValue();
responseText = responseText+"AMOUNT : "+amount;
}
for(int i = 0; i < RETURNCODE.getLength();i++){
returncode = RETURNCODE.item(i).getFirstChild().getNodeValue();
responseText = responseText+"RETURNCODE : "+returncode;
}
response.setContentType("text/xml");
out.print(responseText);
%>
댓글 없음:
댓글 쓰기