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>