couse1.jsp:
-----------------------------------------------------------------------------<%@ 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:
----------------------------------------------------------------------<%@ 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:??页面上已经加了相应的编码转换,怎么还是乱码呢?
-----------------------------------------------------------------------------<%@ 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:
----------------------------------------------------------------------<%@ 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:??页面上已经加了相应的编码转换,怎么还是乱码呢?
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"%>
如果你的程序只有这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>
看下吧~~~懒的弄过来了
-----------------------------------------<%@ 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 = new String(name.getBytes("ISO8859_1"),"GBK");
out.print(name);
this.name = new String(name.getBytes("ISO8859_1"),"gb2312");
}
这样存到库里头的就不是乱码了,但是读取时是乱码,于是乎,我也在getName()里头把它转换回来:public String getName(void){
return new String(this.name.getBytes("gb2312"),"ISO8859_1");
}
但是,还是乱码!!感觉很郁闷,就算不是乱码,这也让人觉得很麻烦,有N多库,然道每个与中文相关的属性都要这样转换一下吗?那也太麻烦了。还不如直接用jdbc呢。555.
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;
}
}