JavaWeb中常常遇到的乱码问题处理-更正

【服务端和浏览器乱码问题】通常都是由于编码解码采用的的码表不一致导致的。

提交:

方法一:针对POST提交,对GET没有效果
浏览器采用utf-8编码发送数据给服务器端,而服务器ISO8859-1的码表去解码,必然得到乱码。怎么解决这个问题呢。ServletRequest中提供了setCharacterEncoding方法,可以通知服务器在处理请求时使用哪个指定编码.

request.setCharacterEncoding("utf-8");

方法二:针对GET和POST提交

浏览器采用utf-8编码发送数据给服务器端,而服务器ISO8859-1的码表去解码,必然得到乱码。怎么解决这个问题呢,此时需要程序员手动编解码,思路是我们只要将乱码字符getBytes(“iso8859-1”)转换为字节,就是正确的字节,再将这些字节new String(bytes,“utf-8”)按照正确的码表解码,就可以转换回正确的字符了。从而解决了乱码
String name = request.getParameter("name");
name = new String(name.getBytes("iso8859-1"),"utf-8");

方法三:专门针对GET请求,因为这种方法只对请求行进行编码

修改配置文件server.xml



响应:服务端采utf-8编码发送数据

通过response.setHeader("Content-Type","text/html;charset=utf-8");通知服务器发送数据时的码表。

通过response.setCharacterEncoding("utf-8");通知浏览器解析时使用的码表。

response.setContextType("text/html;utf-8")===>等价于
response.setHeader("Content-Type","text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");

***********************************************************************************************************************************************************************************

***********************************************************************************************************************************************************************************

此时常用的两种提交方式都可以采用上面的几行代码处理。但是有一个不好的地方时,当开发者调用request.getParameter("name");时总是要进行编解码,能不能一劳
永逸,当开发者调用getParameter()时自动进行编解码呢?采用什么方法一劳永逸呢?
1:方法重写可以吗??---不可以,因为此时request对象已经产生了,无法再重写。仔细想想代码怎么写,似乎不合逻辑
2:采用装饰设计模式,即创建一个类实现和被装饰者(request)相同的接口;并提供一个构造方法,将被装饰者放在新建类的内部;实现父类的中的方法,对于不想
改造的方法直接调用原来的方法,对于想改造的方法进行相关改造。
【装饰设计模式】
改造getParameter()方法:考虑一个参数一个值;多个参数多个值(一一对应);一个参数对应多个参数值。主要上述3种情况,改造的时候主要改造3个方法
getParameter(String name);getParameterMap();getParameterValues(String name)


class MyHttpServletRequest extends HttpServletRequestWrapper{
	//将装饰对象保存到类的内部
	private HttpServletRequest request = null;
	//设置一个是否完成编解码的标识,默认设置为false
	private boolean encoding = false;
	public MyHttpServletRequest(HttpServletRequest request) {
		super(request);
		this.request = request;
	}
	//装饰3个方法
	public String getParameter(String name){
		return getParameterValues(name) == null? null : getParameterValues(name)[0];
	}
	public String[] getParameterValues(String name) {
		return getParameterMap().get(name);
		
	}
	public Map<String,String[]> getParameterMap(){
		//先判断请求的类型
		String method = request.getMethod();
		//当为post时....
		try {
			if("POST".equals(method)){
				request.setCharacterEncoding("utf-8");
				return request.getParameterMap();//可以直接使用装饰前的request
			}else if("GET".equals(method)){
				Map map = request.getParameterMap();
				if(encoding==false){
					//先获得map,然后逐个编解码
					Set<Entry<String,String[]>> set = map.entrySet();
					for (Entry<String, String[]> entry : set) {
						String[] v = entry.getValue();
						for (int i = 0; i < v.length; i++) {
							v[i] = new String(v[i].getBytes("iso8859-1"),"utf-8");
						}
					}
					encoding = true;
				}
				return map;
			}else{
				return request.getParameterMap();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
}


  
  


    
    








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值