哪儿能做网站建设,菏泽去哪了做网站,开发公司各部门岗位职责,找人做个网站大概多少钱在使用JSP的过程中#xff0c;最使人头疼的一个问题就是中文乱码问题#xff0c;以下是我在软件开发中遇到的乱码问题以及解决方法。 1、JSP页面乱码 这种乱码的原因是应为没有在页面里指定使用的字符集编码#xff0c;解决方法#xff1a;只要在页面开始地方用下面代码指定…在使用JSP的过程中最使人头疼的一个问题就是中文乱码问题以下是我在软件开发中遇到的乱码问题以及解决方法。 1、JSP页面乱码 这种乱码的原因是应为没有在页面里指定使用的字符集编码解决方法只要在页面开始地方用下面代码指定字符集编码即可 2、数据库乱码 这种乱码会使你插入数据库的中文变成乱码或者读出显示时也是乱码解决方法如下 在数据库连接字符串中加入编码字符集 String Urljdbc:mysql://localhost/digitgulf?userrootpasswordrootuseUnicodetruecharacterEncodingGB2312; 并在页面中使用如下代码 response.setContentType(text/html;charsetgb2312); request.setCharacterEncoding(gb2312); 3、中文作为参数传递乱码 当我们把一段中文字符作为参数传递个另一页面时也会出现乱码情况解决方法如下 在参数传递时对参数编码比如 RearshRes.jsp?keywords java.net.URLEncoder.encode(keywords) 然后在接收参数页面使用如下语句接收 keywordsnew String(request.getParameter(keywords).getBytes(8859_1)); 4、JSP页面乱码加这句? % page contentTypetext/html; charsetgb2312 languagejava importjava.sql.* errorPageerr.jsp % 5、在form中用get方法传参乱码解决方法 如 1、 login.jsp % page languagejava contentTypetext/html;charsetGBK%htmlhead titleget传参乱码问题/title/headbody form nameform1 actionlogin_do.jsp methodGET input typetext nameusername/br input typepassword namepassword/input typesubmit value提交/ /form/body/html 2、login_do.jsp % page languagejava contentTypetext/html;charsetGBK%% String temprequest.getParameter(username); if(temp!null){ tempnew String(temp.getBytes(8859_1),GBK); } out.println(temp);% 6、在ajax中url传中文参数时乱码要注意的地方 例如下面这个方法 //增加类别函数function addSort(){var name document.getElementById(name).value; //取得id为name的文本框的值(中文的)if(name){ alert(类别名称不能为空!); document.getElementById(name).focus(); return false;}var url actionaddnamename; //这个name是中文参数createXMLHttpRequest();XMLHttpReq.onreadystatechange AddStateChange;XMLHttpReq.open(POST,adminSort,true); //通过post方式传送XMLHttpReq.setRequestHeader(Content-Type,application/x-www-form-urlencoded);XMLHttpReq.send(url);} 在servlet中获取参数的时候 //解决url中文参数乱码的关键是这里因为post方法提交数据默认的字符编码是utf-8//如果后台是gb2312或其他编码数据就会产生乱码所以这里也要将请求参数设为utf-8//尽管你的jsp页面是contentTypetext/html;charsetGBK request.setCharacterEncoding(UTF-8); String name request.getParameter(name); 当输出返回信息时 response.setContentType(text/xml;charsetUTF-8); //这里有点怪当设为GBK时ie显示不正常firefox则正常设为utf-8时两者都显示正常转载于:https://www.cnblogs.com/hqr9313/archive/2012/09/19/2693459.html