小巧,但在中文语境下,还是要注意的。

以下是关键语句,注意转码的先后顺序,这源于GET是HTTP服务器处理,而POST是WEB容器处理:

String name = request.getParameter("nameGet");
name = new String(name.getBytes("ISO-8859-1"), "gb2312");

================================

request.setCharacterEncoding("gb2312");
String name = request.getParameter("namePost");

package cc.openhome;

import java.io.IOException;
import java.io.BufferedReader;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/encoding")
public class EncodingServlet extends HttpServlet{
  protected void doGet(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
    String name = request.getParameter("nameGet");
    name = new String(name.getBytes("ISO-8859-1"), "gb2312");
    System.out.println("GET: " + name);
  }

  protected void doPost(HttpServletRequest request,
                      HttpServletResponse response)
                      throws ServletException, IOException {
    request.setCharacterEncoding("gb2312");
    String name = request.getParameter("namePost");
    System.out.println("POST: " + name);
    String body = readBody(request);
    System.out.println("Body: " + body);
                      }
    private String readBody(HttpServletRequest request)
                        throws IOException{
    BufferedReader reader = request.getReader();
    String input = null;
    String requestBody = "";
    while((input = reader.readLine()) != null) {
      requestBody += input;
    }
    return requestBody;
                        }
}

Servlet请求参数编码处理(POST & GET)的更多相关文章

  1. jsp连接数据库的乱码问题 servlet请求参数编码处理get post

    1.在所有需要读取数据的地方用下面的方式.同时jsp必须统一编码,如我都是UTF-8 String userName= new String(request.getParameter("us ...

  2. Servlet请求参数的方式

    今天整理了以下几种常用的Servlet请求参数的方式,下面简单地介绍 1)getParameter(String key)返回一个字符串,获得name和key 一样的表单控件的数据,如果有重复的nam ...

  3. js处理url中的请求参数(编码/解码)

    在处理 a 链接跳转其他页面时,总会遇到需要传递一些当前页面的信息到其他页面,然后其他页面利用这些信息进行相关操作.利用 get 请求或 hash 传递是常见的方式. 首先,需要对传递的参数进行编码, ...

  4. Http协议入门、响应与请求行、HttpServletRequest对象的使用、请求参数获取和编码问题

    1 课程回顾 web入门 1)web服务软件作用: 把本地资源共享给外部访问 2)tomcat服务器基本操作 : 启动:  %tomcat%/bin/startup.bat 关闭: %tomcat%/ ...

  5. servlet请求编码与响应编码问题(编码不一致可能会导致乱码)

    html中的编码 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&g ...

  6. 浏览器发起Get,Post请求时候传递的参数编码问题

    浏览器发起Get,Post请求时候传递的参数编码问题 最近开发一个网站的时候,用了很多ajax方法,在页面发起Get,post请求,中间自然捎带有很多参数,有中文,有英文,英文一般是不存在编码问题的, ...

  7. Spring MVC请求参数绑定 自定义类型转化 和获取原声带额servlet request response信息

    首先还在我们的框架的基础上建立文件 在domian下建立Account实体类 import org.springframework.stereotype.Controller; import org. ...

  8. 入门servlet:request获取请求参数通用方式

    一.获取请求参数通用方式 1. String getParameter(String name):根据参数名称获取参数值 例:username=flypig&password=2343 2. ...

  9. Spring Cloud Gateway 动态修改请求参数解决 # URL 编码错误传参问题

    Spring Cloud Gateway 动态修改请求参数解决 # URL 编码错误传参问题 继实现动态修改请求 Body 以及重试带 Body 的请求之后,我们又遇到了一个小问题.最近很多接口,收到 ...

随机推荐

  1. 【高德地图API】Pivot控件中加载地图并禁止Pivot手势

    如题,解决方案,参考[Windows phone应用开发[20]-禁止Pivot手势]http://www.cnblogs.com/chenkai/p/3408658.html. xaml代码清单   ...

  2. ubuntu/linuxmint下java环境变量设置

    1.root权限下使用vi或gedit打开/etc目录下的profile文件,末尾加入环境变量. 1)命令: sudo gedit /etc/profile 2)环境变量个人案例: export JA ...

  3. Docker为什么刚运行就退出了

    引言 最近群里的好多新接触Docker的朋友,好多都遇到了相同的问题,使用 $ docker run -d ubuntu /bin/bash 运行了一个简单的容器后,然后docker ps -a 进行 ...

  4. 微信小程序图片选择,预览和删除

    这里均用的是小程序原生api 废话不多说直接上栗子: <view class="addImv"> <!--这个是已经选好的图片--> <view wx ...

  5. [ POI 2017 ] Podzielno

    \(\\\) \(Description\) \(B\)进制数,每个数字\(i(i\in [0,B-1])\)有\(A_i\)个.用这些数字组成一个最大的\(B\)进制数\(X\)(不能有前导零,不需 ...

  6. WinServer2008配置任务计划

    window server 2008下配置任务计划 打开window servers 2008下任务计划配置工具 点击“开始”-->“管理工具”-->“任务计划程序”,打开任务计划配置工具 ...

  7. ABP初始化

    默认认为你手中已经有abp-zero项目,当前4.6.0 angularJS切换到jquery 运行项目,初始化是跳转到~/App/common/views/layout/layout.cshtml, ...

  8. shell脚本网络流量实时查看

    Linux网络流量实时查看脚本,Centos默认没有自带流量查看工具,通过网上的资料做了一些修改 #!/bin/bash # Author: Ca0gu0 # Script Name: idev.sh ...

  9. Concurrency and Application Design

    Concurrency and Application Design In the early days of computing, the maximum amount of work per un ...

  10. C# 字符串到字节数组,字节数组转整型

    ; , ); byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组 num = BitConverter.ToInt32(bytes, ) ...