少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
实现servlet的跳转,以输入流的形式来传输数据

测试UnionPayServlet的httpClient测试类:
  package com.abin.lee.https;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import junit.framework.TestCase;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClientTest extends TestCase {
private static final String Url = "http://localhost:9090/Spa/UnionPayServlet";
@Test
public void testHttpClient() throws Exception {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Url);
ContentProducer create = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
Writer writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write("start");
writer.flush();
writer.close();
}
};
HttpEntity request = new EntityTemplate(create);
post.setEntity(request);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
System.out.println("the last message is: "+result);
}
}



//被访问的servlet,也就是中间servlet
package org.litsoft.air.servlet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class UnionPayServlet extends HttpServlet {
private static final String Url = "http://localhost:9090/Spa/changeServlet";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("UnionPayServlet");
String result=null;
BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));
int num=0;
char[] buffer=new char[1024];
while((num=reader.read(buffer))!=-1){
result=new String(buffer,0,num);
}
System.out.println("from HttpCLient message is: ="+result);
final String transfer=result;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Url);
ContentProducer create = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
Writer writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write(transfer);
writer.flush();
writer.close();
}
};
HttpEntity httpEntity = new EntityTemplate(create);
post.setEntity(httpEntity);
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result1 = EntityUtils.toString(entity);
// System.out.println(result1);
ServletOutputStream out=response.getOutputStream();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write("this message is received by UnionPayServlet is: "+result1);
writer.flush();
writer.close();
}
@Override
public void destroy() {
super.destroy();
}
}




//最终要处理的servlet
package org.litsoft.air.servlet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.litsoft.air.unionpay.CreateJdomOne;
public class ChangeServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 接收Servlet传回来的信息
BufferedReader reader = new BufferedReader(new InputStreamReader(
request.getInputStream()));
String show = null;
StringBuffer stb = new StringBuffer();
while ((show = reader.readLine()) != null) {
stb.append(show);
}
System.out.println("from UnionPayServlet message is :" + stb.toString());
reader.close();
ServletOutputStream out=response.getOutputStream();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write("this message is received by ChangeServlet is :"+stb.toString());
writer.flush();
writer.close();
}
}


//servlet的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>UnionPayServlet</servlet-name>
<servlet-class>org.litsoft.air.servlet.UnionPayServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UnionPayServlet</servlet-name>
<url-pattern>/UnionPayServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>changeServlet</servlet-name>
<servlet-class>org.litsoft.air.servlet.ChangeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>changeServlet</servlet-name>
<url-pattern>/changeServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
posted on 2012-09-27 20:31 abin 阅读(1830) 评论(0)  编辑  收藏 所属分类: SIPHMPASTERISKHTTPS

只有注册用户登录后才能发表评论。


网站导航: