`

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

    博客分类:
  • 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 跳转传中文情况,待续:

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics