Apache+Tomcat url传递的中文字符串乱码问题 在4.0以后的版本,已经不需要象3.23版本中使用字符转换函数:(1)编译javabean时 javac 命令行加上-encoding ISO8859_1(2)在Jsp头部中加入<%@ page contentType="text/html;charset=ISO8859_1" %> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static String getStr(String strInString) { try { return new String(strInString.getBytes("ISO8859-1"),"gb2312"); } catch (Exception ex) { return ""; } } public static String charToISO(String strInString){ try { return new String(strInString.getBytes("gb2312"),"ISO8859-1"); } catch (Exception ex) { return ""; } }.....String ranmessage = getStr(message);response.sendRedirect("login.jsp?message="+ranmessage); <%@ page contentType="text/html;charset=ISO8859_1" %>charToISO(message)都可以解决,但是charToISO(message)在Tomcat下又显示乱码,所以选择<%@ page contentType="text/html;charset=ISO8859_1" %>谁能解释一下字符集的问题,什么服务器用的是什么字符集,我什么时候该用什么样的字符集1.往数据库里存数据2.从数据库里读出数据显示3.通过url传递参数时解决完这个问题马上结帖 解决办法:第一:在jsp页面加入:<%@ page contentType="text/html; charset=gb2312" %>或者在servlet里面 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=gb2312");上面的如果在不行就用如下的方法在数据入库前进行调用:public static String UnicodeToChinese(String s){ try{ if(s==null||s.equals("")) return ""; String newstring=null; newstring=new String(s.getBytes("ISO8859_1"),"gb2312"); return newstring; } catch(UnsupportedEncodingException e) { return s; } }public static String ChineseToUnicode(String s){ try{ if(s==null||s.equals("")) return ""; String newstring=null; newstring=new String(s.getBytes("gb2312"),"ISO8859_1"); return newstring; } catch(UnsupportedEncodingException e) { return s; } } <%@ page contentType="text/html;charset=ISO8859_1" %> <%@ page contentType="text/html; charset=gb2312" %>加上这个在页面上一定行的,我想是这样的,我的网页都有的,不知道是我的对不对,不对的地方请多多指教。 如果在你的URL中需要传递中文,就用java.net.URLEncoder.encode()进行编码,这样就可以解决了。示例:<a href=hello.jsp?send=<%=java.net.URLEncoder.encode("大家好")%>> 点击</a>在接受页面上面加上<%@ page contentType="text/html; charset=gb2312" %>如果要将数据插入数据库要做处理的如将content的内容插入数据库,就要处理一下。content = new String(content.getBytes("ISO8859_1"),"GB2312") ; 用什么测试软件 这个空指针问题折磨我一天了 工厂模式的好处在哪里 ? flex+java的三种方式? jsp在处理mysql字段中字串截取问题 哪里有"JavaScript开发工具免费下载"呀? 有关hibernate级联更新问题 一个比较变态的substring问题,欢迎讨论 在JBuilder 中怎样把要执行的页面设为首页? 如何得到访问者的ip并判断是不是国外的ip??请大虾指教!!! 大家帮帮我看看这段程序为什么会出错的啊!比较急的了! ===========怎样将数据逆序显示出来==========
{
try {
return new String(strInString.getBytes("ISO8859-1"),"gb2312");
}
catch (Exception ex) {
return "";
}
}
public static String charToISO(String strInString){
try {
return new String(strInString.getBytes("gb2312"),"ISO8859-1");
}
catch (Exception ex) {
return "";
}
}
.....
String ranmessage = getStr(message);
response.sendRedirect("login.jsp?message="+ranmessage);
charToISO(message)
都可以解决,但是charToISO(message)在Tomcat下又显示乱码,所以选择<%@ page contentType="text/html;charset=ISO8859_1" %>谁能解释一下字符集的问题,什么服务器用的是什么字符集,我什么时候该用什么样的字符集1.往数据库里存数据
2.从数据库里读出数据显示
3.通过url传递参数时解决完这个问题马上结帖
第一:
在jsp页面加入:
<%@ page contentType="text/html; charset=gb2312" %>
或者在servlet里面
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");
上面的如果在不行就用如下的方法在数据入库前进行调用:
public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}public static String ChineseToUnicode(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
加上这个在页面上一定行的,我想是这样的,我的网页都有的,不知道是我的对不对,不对的地方请多多指教。
示例:
<a href=hello.jsp?send=<%=java.net.URLEncoder.encode("大家好")%>> 点击</a>
在接受页面上面加上
<%@ page contentType="text/html; charset=gb2312" %>
如果要将数据插入数据库要做处理的
如将content的内容插入数据库,就要处理一下。
content = new String(content.getBytes("ISO8859_1"),"GB2312") ;