因为在一个TOMCAT的http://127.0.0.1:8080端口的一个WEB应用中,需要使用AJAX的方式访问http://127.0.0.1:8989端口下的资源,来动态更新页面上的数据,由于在firefox碰到跨域问题,不得以写了一个servlet 代理,通过代理改变AJAX的URL来获取数据,但返回的中文数据出现了乱码,一时找不到原因,请大家帮着分析一下.
servlet 中的代码如下:
public class WmsProxy extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String strUrl = "";
Enumeration param = request.getParameterNames();
while(param.hasMoreElements()){
String pname = param.nextElement().toString();
if(!pname.equalsIgnoreCase("pageNo") ){
strUrl += pname+"="+request.getParameter(pname)+"&";
}
}
if(strUrl.endsWith("&")){
strUrl = strUrl.substring(0,strUrl.lastIndexOf("&"));
}
response.setContentType("text/xml");
ServletOutputStream outputStream = response.getOutputStream();
URL url = new URL("http://127.0.0.1:8989/WMSEgi.egi?" + strUrl);
InputStream in = url.openStream() ;
BufferedReader dr = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = dr.readLine())!=null) {
System.out.println(line) ;
outputStream.print(line);
}
}
}
servlet 中的代码如下:
public class WmsProxy extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String strUrl = "";
Enumeration param = request.getParameterNames();
while(param.hasMoreElements()){
String pname = param.nextElement().toString();
if(!pname.equalsIgnoreCase("pageNo") ){
strUrl += pname+"="+request.getParameter(pname)+"&";
}
}
if(strUrl.endsWith("&")){
strUrl = strUrl.substring(0,strUrl.lastIndexOf("&"));
}
response.setContentType("text/xml");
ServletOutputStream outputStream = response.getOutputStream();
URL url = new URL("http://127.0.0.1:8989/WMSEgi.egi?" + strUrl);
InputStream in = url.openStream() ;
BufferedReader dr = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = dr.readLine())!=null) {
System.out.println(line) ;
outputStream.print(line);
}
}
}