try on the client sidevar xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
xmlHTTP.open("POST","http://myserver/save.jsp", false);
xmlHTTP.setRequestHeader("Content-Type","text/xml");
xmlHTTP.send(xmlDoc.xml);on the server, get the text input stream and load it into your dom object
xmlHTTP.open("POST","http://myserver/save.jsp", false);
xmlHTTP.setRequestHeader("Content-Type","text/xml");
xmlHTTP.send(xmlDoc.xml);on the server, get the text input stream and load it into your dom object
我就是不知道JSP页如何接收这个xmlDoc.xml,是作为String,还是作为InputStream呢?应该怎么写呢?能否简短地写两句看看,谢谢
String line;
while ((line = in.readLine()) != null)
{
//....
}
public ServletInputStream getInputStream()
throws java.io.IOException
Retrieves the body of the request as binary data using a ServletInputStream. Either this method or getReader() may be called to read the body, not both.
Returns:
a ServletInputStream object containing the body of the request
Throws:
java.lang.IllegalStateException - if the getReader() method has already been called for this request
java.io.IOException - if an input or output exception occurred
public ServletInputStream getInputStream()
throws java.io.IOException
Retrieves the body of the request as binary data using a ServletInputStream. Either this method or getReader() may be called to read the body, not both.
Returns:
a ServletInputStream object containing the body of the request
Throws:
java.lang.IllegalStateException - if the getReader() method has already been called for this request
java.io.IOException - if an input or output exception occurred
<SCRIPT LANGUAGE="JavaScript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP.4.0");
xmlDoc.async=false; xmlDoc.load("web.xml"); if (xmlDoc.readyState==4)
{
if(xmlDoc.parseError.errorCode!=0)
{
alert(xmlDoc.parseError.reason)
}
else
{
xmlHTTP.open("GET","save.jsp",false);
xmlHTTP.send(xmlDoc.xml);
window.alert("Done");
}
}
</SCRIPT>// save.jsp
<%@page import="java.io.*"%>
<%
BufferedReader in = request.getReader();
String line;
String realPath = config.getServletContext().getRealPath("12345.txt"); File f=new File(realPath);
FileWriter fw=new FileWriter(f); try
{
while ((line = in.readLine()) != null)
{
fw.write(line);
}
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
out.println("Done");
}
%>两个文件在同一个目录下,我运行了两个文件都没有错,可是运行的时候为什么没有12345.txt文件生成呢?另外,象xmlHTTP这样的请求,不刷新页面,我应该如何调试程序呢?
<SCRIPT LANGUAGE="JavaScript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP.4.0");
xmlDoc.async=false; xmlDoc.load("web.xml"); if (xmlDoc.readyState==4)
{
if(xmlDoc.parseError.errorCode!=0)
{
alert(xmlDoc.parseError.reason)
}
else
{
xmlHTTP.open("GET","save.jsp",false);
xmlHTTP.send(xmlDoc.xml);
window.alert("Done");
}
}
</SCRIPT>// save.jsp
<%@page import="java.io.*"%>
<%
BufferedReader in = request.getReader();
String line;
String realPath = config.getServletContext().getRealPath("12345.txt"); File f=new File(realPath);
FileWriter fw=new FileWriter(f); try
{
while ((line = in.readLine()) != null)
{
fw.write(line);
}
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
out.println("Done");
}
%>两个文件在同一个目录下,我运行了两个文件都没有错,可是运行的时候为什么没有12345.txt文件生成呢?另外,象xmlHTTP这样的请求,不刷新页面,我应该如何调试程序呢?
{
out.println(line); //看看有没有东西。如果没有即没有收到XML
fw.write(line);
}
response.setContentType( "text/xml" ); // get the communication channel with the requesting client
PrintWriter out = response.getWriter(); try
{
ServletInputStream servletInputStream = request.getInputStream();
int contentLength = request.getContentLength();
byte[] b = new byte[ contentLength ];
int once = 0;
int total = 0; while ( ( total < contentLength ) && ( once >= 0 ) )
{
once = servletInputStream.read( b, total, contentLength );
total += once;
} if ( total > 0 )
{
XMLFileVisitor xmlData = new XMLFileVisitor();
Hashtable values = xmlData.getExecCommandAttributes(
new StringBufferInputStream(
new String( b, 0, total ) ),
"root" );
String command = values.get( "command" ).toString(); // 上传文件
if ( command.equals( "1" ) )
{
String fileContent = values.get( "fileContent" ).toString();
byte[] c = stringToHex( fileContent );
String filename = SystemVar.getSerialNumberString() +
values.get( "filename" ).toString();
File file = new File( SystemVar.getWorkflowUploadPath() +
filename );
BufferedOutputStream out1 = new BufferedOutputStream(
new FileOutputStream(
file ) );
out1.write( c );
out1.close(); // 输出结果
out.println( SystemVar.getWorkflowDownloadPath() +
filename );
out.close(); return;
}
}
}
catch ( Exception exp )
{
exp.printStackTrace();
}
不是Java的问题
是var xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP.4.0");
这一句,我把它改成了
var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
就OK了,真是FAINT,要用原始的版本
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
function run()
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
xmlDoc.async=false;
xmlDoc.load("web.xml"); if (xmlDoc.readyState==4)
{
if(xmlDoc.parseError.errorCode!=0)
{
alert(xmlDoc.parseError.reason);
return false;
}
else
{
xmlHTTP.open("GET","save.jsp",false);
xmlHTTP.send(xmlDoc.xml);
window.alert(xmlHTTP.responseText);
}
}
}
</SCRIPT>
</HEAD>
<BODY><INPUT id=button1 type=button value=发送xmlHTTP请求 name=run onClick="run()">
</BODY>
</HTML>
// save.jsp
<%@page import="java.io.*"%>
<%
BufferedReader in = request.getReader();
String line;
String realPath = config.getServletContext().getRealPath("\\OATree\\12345.txt"); File f=new File(realPath);
FileWriter fw=new FileWriter(realPath);
BufferedWriter bw=new BufferedWriter(fw);
try
{
while ((line = in.readLine()) != null)
{
out.println(line);
bw.write(line);
bw.newLine();
}
bw.close();
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
out.println("Mission Completed");
}
%>