ajax发XML给Struts,Struts收不到??
在struts的action应用
request.setCharacterEncoding( "UTF-8 ");
BufferedReader reader = request.getReader();
boolean is = false;
while ((line = reader.readLine()) != null) {
is = true;
}
结果System.out.println(is);打印结果为false,不知其中缘由,传说request.getReader();方法只能调用一次
但是呢,一个简单的例子就能成功,如下:
Servlet> > >
package servlet;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class TestServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public TestServlet() {
super();
}
/**
* Destruction of the servlet. <br/>
*/
public void destroy() {
super.destroy(); // Just puts "destroy " string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding( "UTF-8 ");
System.out.println( "Ajax Start...... ");
String name = "no ";
String password = "00 ";
/**
* 读取xml格式的参数
*/
StringBuffer readXml = new StringBuffer();
String line = null;
boolean is = false;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
readXml.append(line);
is = true;
}
} catch (Exception e) {
System.out.println(e.toString());
}
if (is) {
String xml = readXml.toString();
Document xmlDoc = null;
try {
xmlDoc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes()));
} catch (ParserConfigurationException e) {
System.out.println( "ParserConfigurationException ");
} catch (SAXException e) {
System.out.println( "SAXException ");
} catch (Exception e) {
System.out.println(e.toString());
}
System.out.println( "......已读取xml格式的参数 ");
/**
* 参数1
*/
name = xmlDoc.getElementsByTagName( "name ").item(0).getFirstChild()
.getNodeValue();
password = xmlDoc.getElementsByTagName( "password ").item(0)
.getFirstChild().getNodeValue();
System.out.println( "name> > > " + name + ",password> > > " + password);
}
/**
* 返回xml格式的信息
*/
/**
* 设置输出信息的格式及字符集
*/
response.setContentType( "text/xml;charset=UTF-8 ");
response.setHeader( "Cache-Control ", "no-cache ");
PrintWriter out = response.getWriter();
StringBuffer results = new StringBuffer(
" <?xml version= "1.0 " encoding= "utf-8 "?> ");
results.append( " <users> ");
results.append( " <user> ");
results.append( " <name> ");
results.append(name);
results.append( " </name> ");
results.append( " <password> ");
results.append(password);
results.append( " </password> ");
results.append( " </user> ");
results.append( " </users> ");
out.write(results.toString());
// out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
web.xml> > >
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app version= "2.4 "
xmlns= "http://java.sun.com/xml/ns/j2ee "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<servlet>
<description> This is the description of my J2EE component </description>
<display-name> This is the display name of my J2EE component </display-name>
<servlet-name> TestServlet </servlet-name>
<servlet-class> servlet.TestServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> TestServlet </servlet-name>
<url-pattern> /servlet/test </url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file> index.html </welcome-file>
</welcome-file-list>
</web-app>
index.html> > >
<input type= "button " onclick= "sendto() " namd= "s_b " value= "Ajax ">
<script language= "javascript ">
var XMLHttpReq = false;
// 创建 XMLHttpRequest 对象(主函数不需改变)
function createXMLHttpRequest(){
if(window.XMLHttpRequest){ // Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}else if (window.ActiveXObject){ // IE 浏览器
try{
XMLHttpReq = new ActiveXObject( "Msxml2.XMLHTTP ");
}catch(e){
try{
XMLHttpReq = new ActiveXObject( "Microsoft.XMLHTTP ");
}catch(e){}
}
}
}
//发送请求函数(提交xml格式的请求参数)
function sendRequest(url,xml){
createXMLHttpRequest();
XMLHttpReq.open( "POST ",url,true);
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
XMLHttpReq.setRequestHeader( "Content-Type ", "application/x-www-form-urlencoded ");
XMLHttpReq.send(xml);//发送请求
}
//创建XML
function createXML(){
var xml = " <user> ";
xml = xml + " <name> sunjianbin </name> ";
xml = xml + " <password> 7980421 </password> ";
xml = xml + " </user> ";
return xml;
}
//处理返回信息函数(处理xml格式的返回信息)
function processResponse(){
if(XMLHttpReq.readyState == 4){ // 判断对象状态
if(XMLHttpReq.status == 200){ // 信息已经成功返回,开始处理信息
readXml();
}else { //页面不正常
window.alert( "您所请求的页面有异常。 ");
}
}
}
function readXml(){
var objects=XMLHttpReq.responseXML.getElementsByTagName( "user ");
for(var i=0;i <objects.length;i++){
var object = objects[i];
var name=object.getElementsByTagName( "name ")[0].firstChild.data;
var password=object.getElementsByTagName( "password ")[0].firstChild.data;
alert( "name> > > "+name+ ",password> > > "+password);
}
}
function sendto(){
var url= "/test/servlet/test ";
var xml=createXML();
sendRequest(url,xml)
}
</script>
[解决办法]
while ((line = reader.readLine()) != null) {
readXml.append(line);
//在这里打印line是可以的
is = true;