1:示例代码login_xml.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Ext.Ajax.request向服务器提交xml数据</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="ext3.2/resources/css/ext-all.css"></link>
<script type="text/javascript" src="ext3.2/adapter/ext/ext-base-debug.js"></script>
<script type="text/javascript" src="ext3.2/ext-all-debug.js"></script>
<script type="text/javascript" src="ext3.2/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext3.2/ext-all.js"></script>
<script type="text/javascript" src="ext3.2/src/local/ext-lang-zh_CN.js"></script>

<script type="text/javascript">
function login() {
var reqeustConfig = {
url: 'login_xml.action',
xmlData: getXml(), //发送xml数据
callback: function(options, success, response) {
var msg = ['请求是否成功:', success, '\n', '服务器返回值:', response.responseText];
alert(msg.join(''));
}
};
Ext.Ajax.request(reqeustConfig);
}

/**
* 生成xml对象
*/
function getXml() {
var username = document.forms['loginForm'].username.value;
var pwd = document.forms['loginForm'].password.value;
var dom = new ActiveXObject("msxml2.DOMDocument");
var header = dom.createProcessingInstruction("xml", "version='1.0'");
dom.appendChild(header);
var root = dom.createElement("loginInfo");
var userName = dom.createElement("userName");
userName.text = username;
var password = dom.createElement("password");
password.text = pwd;
root.appendChild(userName);
root.appendChild(password);
dom.appendChild(root);
return dom;
}
</script>
</head>
<body>
<form id="loginForm" method="post">
用户名:<input name="username" type="text" /><br/>
密码:<input name="password" type="password"/><br/>
<input type="button" name="button" id="button" value="提交" οnclick="login();"/>
<input type="reset" name="button" value="重置"/>
</form>
</body>
</html>

2:Action端的代码

/**
* 接收客户端传过来的xml数据
*/
public void login_xml() {
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(request.getInputStream());
Element root = doc.getRootElement();
String username = root.element("userName").getStringValue();
String password = root.element("password").getStringValue();

System.out.println(username);
System.out.println(password);

String msg = " ";
if (username != null && username.equals("admin") && password != null
&& password.equals("admin")) {
msg = "恭喜你登录成功!";
} else {
msg = "登录失败";
}

response.getWriter().write(msg);
response.getWriter().flush();

} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}



3:程序效果

                                   图:输入正确,请求成功的情况

 

                                  图:输入错误的情况