-----------------------------------------------------------------------------
- Java code
- <%@ page contentType="text/html; charset=gb2312" errorPage=""%> <% request.setCharacterEncoding("gb2312"); %> <html> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <body> <hr> <br>::增加一个course::<br> <form action="result.jsp" method="post" name="add"> id:<input type=text name="id"><br> name:<input type=text name="name"><br> <input type=submit value="submit"><br> </form> <a href=viewAll.jsp>::查看所有Course::</a> </body> </html>数据挖掘实验室
result.jsp:
----------------------------------------------------------------------
- Java code
- <%@ page contentType="text/html; charset=gb2312" errorPage=""%> <% request.setCharacterEncoding("gb2312"); %> <html> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <body> <% String name = request.getParameter("name"); if(name!=null){ System.out.println("name:"+name); out.println("name:"+name); } %> </body> </html>数据挖掘研究院
输入:11111,语文
显示:name:??
页面上已经加了相应的编码转换,怎么还是乱码呢?request得到的,需要转码,或者加上filter。filter怎么加呢?能给个例子吗?谢谢http://topic.csdn.net/u/20071127/16/fc0edc1a-4b1b-4f96-afc8-8ee68d39f2a4.html方法1:
String name = toGBK(request.getParameter("name"));
public static String toGBK(String str)
{
try
{
byte[] bys = str.getBytes("ISO8859_1");
return new String(bys, "GBK");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
return str;
}
}
方法二:
建议所有页面编码都采用utf-8,这样就避免了中文转码问题,如果还是乱码就加一个过滤器,上百度搜索有很多过滤器的例子。
<%@ page language="java" pageEncoding="UTF-8"%>
String name = new String(request.getParameter("name").getBytes("iso8859-1"));
我这里 没有问题啊 可以正确显示声明一下,
如果你的程序只有这2个文件,那个代码没有任何问题。
至于乱码,我唯一想到的原因,就是加上了不必要的 filter.
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> com.wiley.SetCharacterEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> utf-8 </param-value>
</init-param>
<init-param>
<param-name> ignore </param-name>
<param-value> true </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<servlet-name> action </servlet-name> 数据挖掘实验室
</filter-mapping>
http://www.qqread.com/jsp/f211521.html
看下吧~~~懒的弄过来了现在request没问题了,可是我用hibernate来存储数据,jsp页面用 <jsp:getProperty ../> 标签,存储到数据库是乱码!!
jsp页面如下:
-----------------------------------------
- HTML code
- <%@ page contentType="text/html; charset=gb2312" errorPage=""%> <% request.setCharacterEncoding("gb2312"); %> <jsp:useBean id="course" class="com.rizon.study.hibernate.Course" scope="page"> <jsp:setProperty name="course" property="*"/> </jsp:useBean> <jsp:useBean id="courseBusiness" class="com.rizon.study.hibernate.CourseBean" scope="page"/> <html> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <body> <center> <% try{ if(course.getId().equals(null)) course.setId(""); if(course.getId().equals("")){ }else{ courseBusiness.addCourse(course);//调用hibernate存储到库的业务逻辑,库里头是乱码?? } %> 成功添加了Course:<br> name:<%=course.getName()%> //这里输出的也是乱码?? Id:<%=course.getId()%> <% }catch(Exception e){ } %> <br>::增加一个course::<br> <form action="course.jsp" method="post" name="add"> id:<input type=text name="id"><br> name:<input type=text name="name"><br> <input type=submit value="submit"><br> </form> </body> </html>数据挖掘研究院
各位高手支招!!!name = course.getName();
name = new String(name.getBytes("ISO8859_1"),"GBK");
out.print(name);或者在web.xml里加过滤器长知识了!~!~!帮顶一下to sunwei_07: 这样name = new String(name.getBytes("ISO8859_1"),"GBK"); 是没错。
关键是我存到库里头的已经是乱码了,我的解决方法是在Course.java的setName()做如下转换:
- Java code
- public void setName(String name){ this.name = new String(name.getBytes("ISO8859_1"),"gb2312"); }数据挖掘研究院
这样存到库里头的就不是乱码了,但是读取时是乱码,于是乎,我也在getName()里头把它转换回来:
- Java code
- public String getName(void){ return new String(this.name.getBytes("gb2312"),"ISO8859_1"); }数据挖掘研究院
但是,还是乱码!!
感觉很郁闷,就算不是乱码,这也让人觉得很麻烦,有N多库,然道每个与中文相关的属性都要这样转换一下吗?那也太麻烦了。还不如直接用jdbc呢。555.
- Java code
- package beans; public class chStr { public String chStr(String str){ if(str==null){ str=""; }else{ try{ str=(new String(str.getBytes("iso-8859-1"),"GB2312")).trim(); }catch(Exception e){ e.printStackTrace(System.err); } } return str; } }

