当前位置: 技术问答>java相关
JSP能接收xmlHTTP请求吗?怎么接收?
来源: 互联网 发布时间:2015-05-26
本文导语: Example The following script example creates an XMLHTTP object, and then uses the open method to synchronously open an Active Server Pages (ASP) page. The script then posts an XML document to an ASP page, which uses the load method to load the...
Example
The following script example creates an XMLHTTP object, and then uses the open method to synchronously open an Active Server Pages (ASP) page. The script then posts an XML document to an ASP page, which uses the load method to load the document.
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
xmlHTTP.open("GET","http://myserver/save.asp", false);
xmlHTTP.send(xmlDoc)
The following code belongs on the server.
--------------------------------------------------------------------
以上这段是MSXML SDK里的例子,最后一句用ASP来接收xmlHTTP请求
我想问问,能用JSP来接收吗?如何接收呢并处理呢?
The following script example creates an XMLHTTP object, and then uses the open method to synchronously open an Active Server Pages (ASP) page. The script then posts an XML document to an ASP page, which uses the load method to load the document.
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
xmlHTTP.open("GET","http://myserver/save.asp", false);
xmlHTTP.send(xmlDoc)
The following code belongs on the server.
--------------------------------------------------------------------
以上这段是MSXML SDK里的例子,最后一句用ASP来接收xmlHTTP请求
我想问问,能用JSP来接收吗?如何接收呢并处理呢?
|
try on the client side
var 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
var 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
|
BufferedReader in = request.getReader();
String line;
while ((line = in.readLine()) != null)
{
//....
}
String line;
while ((line = in.readLine()) != null)
{
//....
}
|
ServletRequest Class
getInputStream
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
getInputStream
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
|
out.println(realPath);看看是什么,是否有问题?
|
while ((line = in.readLine()) != null)
{
out.println(line); //看看有没有东西。如果没有即没有收到XML
fw.write(line);
}
{
out.println(line); //看看有没有东西。如果没有即没有收到XML
fw.write(line);
}
|
Servlet中我们是这样处理的,不知Jsp怎样,不过应该大同小异
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 = 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();
}
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 = 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();
}
|
因为xmlHttp在Client端没进行Base64编码,所以上传过程中是以16进制数据的Ascii形式进行的,上传以后还需要将它转为Byte形式;如果你要优化,可以进行Base64编码。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。