Handles the HTTP POST method
来源:互联网 发布:js控制浏览器自动全屏 编辑:程序博客网 时间:2024/05/16 07:38
String line;
while((line = reader.readLine()) != null){
out.println(line);
}
reader.close();
}
}
/**
* Handles the HTTP POST method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取参数,最后得到请求url地址类似于:url = http://192.168…/AJAX/AJAXServer?aa=11&bb=22&cc=33
String url = request.getParameter("url");
//获取访问的跨域地址url = http://192.168…/AJAX/AJAXServer
StringBuffer paramBuffer = new StringBuffer();
Enumeration enu = request.getParameterNames();
boolean flag = false; //定义标志变量,表示是否为拼接的第一个参数
while(enu.hasMoreElements()){
String paramName = (String) enu.nextElement();
if(!paramName.equals("url")){
String paramValue = request.getParameter(paramName);
paramValue = URLEncoder.encode(paramValue,"utf-8");
if(!flag){
paramBuffer.append(paramName)。append("=")。append(paramValue);
flag = true;
} else {
paramBuffer.append("&")。append(paramName)。append("=")。append(paramValue);
}
}
}
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
if(url != null && url.trim()。length() > 0){
URL connectUrl = new URL(url);
URLConnection connection =connectUrl.openConnection();
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(paramBuffer.toString());
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String line;
while((line = reader.readLine()) != null){
out.println(line);
}
writer.close();
reader.close();
}
}
对于问题2
(1)响应数据解决中文乱码的方法:保证页面端定义的charset和http响应头的Content-Type中定义的charset一致。
①http响应头的Content-Type中charset设置为utf-8
②仅仅使用"MSXML2.XMLHTTP","Miscrosoft.XMLHTTP"这两个中的一个来创建XMLHttpRequest对象。
(2)请求数据中文乱码解决方法:由于服务器端getParameter()方法会自动进行一次ISO8859规则解码,因此页面端应使用javascript中的encodeURI()方法对请求数据进行两次编码,服务器端使用URLDecode(utf-8)进行一次解码。
总结 Ajax异步交互主要通过XMLHttpRequest对象实现,搜索Suggest、GoogleMap等应用的友好体验使得此对象的使用愈显重要,希望本文对读者更好的使用XMLHttpRequest提供了一定帮助。
- Handles the HTTP POST method
- wxWidgets http post method example
- HTTP Method小结(GET,POST.etc)
- HTTP Method小结(GET,POST.etc)
- HTTP Method小结(GET,POST.etc)
- HTTP Method小结(GET,POST.etc)
- HTTP Method小结(GET,POST.etc)
- How the Java virtual machine handles method invocation and return - Java World
- Nginx HTTP Post Method: 405 Method not allowed 解决办法
- ASP.NET中的Http Handles
- ASP.NET中的Http Handles
- ASP.NET中的Http Handles
- 淺談 HTTP Method:表單中的 GET 與 POST 有什麼差別?
- HTTP method POST is not supported by this URL
- http表单中method的post与get区别
- HTTP method POST is not supported by this URL
- 关于HTTP Status 500 - Request method 'POST' not supported分析
- HTTP Status 405 - Request method 'POST' not supported (Spring MVC)
- am335x gpio驱动
- iOS App性能优化
- 模态对话框与非模态对话的几种销毁方法与区别
- Nginx+tomcat集群环境搭建
- Java程序中的“内存泄漏”问题
- Handles the HTTP POST method
- Metasploit简介
- 变长数组
- eclipse ndk配置
- 十道海量数据处理面试题与十个方法大总结
- c++函数指针
- dlut1218-spfa(链式向前星)模板题
- 数据类型、操作过程和程序
- aspcms当前位置链接怎么去?