jsp传值乱码情况分析与归纳

jsp 传值到action 或者servlet中,默认提交为get的时候,会出现乱码问题。
可以在action接收参数时,写一个公共方法如下:
/**
	   * 从request得到中文 
	   * @param request
	   * @param name
	   * @param code
	   * @return
	   */
	  public static String getChinaStringByCODE(HttpServletRequest request, String name,String codeType){
		  String value = request.getParameter(name);
		  String returnValue = "";
		  if("null".equalsIgnoreCase(value) || null==value){
			  return returnValue;
		  }else{
			  byte[] bytes;
			try {
				bytes = value.getBytes("iso-8859-1");
				returnValue = new String(bytes,codeType);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				Log.debug("转换中文编码出错"+e.getMessage());
			}
		  }
		  return returnValue ;
	  }
	  
	  /**
	   * 转换为utf-8编码方式中文
	   * @param request
	   * @param name
	   * @return
	   */
	  public static String getChinaStringByUTF8(HttpServletRequest request, String name){
		  return getChinaStringByCODE(request, name, "utf-8");
	  }
	  
	  /**
	   * 转换为gb2312编码方式中文
	   * @param request
	   * @param name
	   * @return
	   */
	  public static String getChinaStringByGB2312(HttpServletRequest request, String name){
		  return getChinaStringByCODE(request, name, "gb2312");
	  }
你可以随时添加其他类型编码或者直接调用第一个方法,传入想转的其他编码方式。
乱码问题会在其他时候想起来继续更新不同出现情况。
jsp URL 跳转传中文情况,待续:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值