package
com.test.servlet;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
javax.servlet.ServletException;
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.InputStreamEntity;
import
org.apache.http.entity.StringEntity;
import
org.apache.http.impl.client.DefaultHttpClient;
public
class
StringServlet
extends
HttpServlet {
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
HttpClient client =
new
DefaultHttpClient();
InputStream is = request.getInputStream();
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(is));
StringBuffer result =
new
StringBuffer();
String len;
while
((len = reader.readLine()) !=
null
){
result.append(len);
}
String xml = result.toString();
System.out.println(
"struts2.StringServlet:"
+ xml);
InputStreamEntity reqEntity =
new
InputStreamEntity(is,-
1
);
reqEntity.setContentType(
"binary/octet-stream"
);
reqEntity.setChunked(
true
);
HttpPost post =
new
HttpPost(
"http://localhost:8088/struts2/SendXmlServlet"
);
post.setEntity(reqEntity);
HttpResponse res = client.execute(post);
HttpEntity entity = res.getEntity();
if
(entity !=
null
){
System.out.println(
"Response content length: "
+ entity.getContentLength());
}
BufferedReader resultReader =
new
BufferedReader(
new
InputStreamReader(entity.getContent()));
String line =
null
;
while
((line = resultReader.readLine()) !=
null
){
System.out.println(line);
}
if
(entity !=
null
){
entity.consumeContent();
}
}
}