jsp乱码问题(100分)

W

wchal

Unregistered / Unconfirmed
GUEST, unregistred user!
JavaBeanDemo.jsp
<%@page contentType=&quot;text/html;
charset = gb2312&quot;%>
<html>
<head>
<title>JavaBean示例</title>
</head>
<jsp:useBean id=&quot;demoBeanId&quot;
scope=&quot;session&quot;
class=&quot;jsptaglibpro.DemoBean&quot;
/>
<body>
<center>
<h3>
JavaBean示例
</h3>
<p>
<%request.setCharacterEncoding(&quot;gb2312&quot;);
%>
<%=demoBeanId.showMessage() %>
</p>
</center>
</body>
</html>

DemoBean.java
package jsptaglibpro;

public class DemoBean {
private String sample = &quot;&quot;;
public String getSample(){
return sample;
}
public void setSample(String newValue){
if (newValue != null){
sample = newValue;
}
}
public String getStr(String str)
{
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes(&quot;iso8859-1&quot;);
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{}
return &quot;null&quot;;
}
public String showMessage(){
return getStr(&quot;这是JavaBean的传入信息.&quot;);
}
}
ie上显示结果为
JavaBean示例
??JavaBean?????.
 
R

RedBeret

Unregistered / Unconfirmed
GUEST, unregistred user!
这么改一下试试:
public String getStr(String str)
{
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes(&quot;iso8859-1&quot;);
String temp=new String(temp_t,&quot;GB2312&quot;);
//改这句
return temp;
}
catch(Exception e)
{}
return &quot;null&quot;;
}
 
Y

yangxiao_jiang

Unregistered / Unconfirmed
GUEST, unregistred user!
楼上说的,应该可以。
还有就是可以加上filter.
 
W

wchal

Unregistered / Unconfirmed
GUEST, unregistred user!
RedBeret的方法我回去試一下.
另外yangxiao_jiang,請問filter怎麼用??
 
W

wchal

Unregistered / Unconfirmed
GUEST, unregistred user!
还是不行,不知道为什么?[:(]
 

网中戏

Unregistered / Unconfirmed
GUEST, unregistred user!
Y

yangxiao_jiang

Unregistered / Unconfirmed
GUEST, unregistred user!
你的工程编码是什么?
filter只能是用在提交那些地方,我现在怀疑你的工程编码可能有问题。
 

萨奇马

Unregistered / Unconfirmed
GUEST, unregistred user!
www.stonebloom.com
 

大唐电信

Unregistered / Unconfirmed
GUEST, unregistred user!
tomcat下中文的彻底解决2006-11-14 16:40tomcat下中文的彻底解决

服务器是tomcat,操作系统是xp,采用的是MVC架构,模式是采用Facade模式,总是出现乱码,自己也解决了好多天,同事也帮忙解决,也参考了网上众多网友的文章和意见,总算是搞定。但是好记性不如烂笔杆,所以特意记下,以防止自己遗忘,同时也给那些遇到同样问题的人提供一个好的参考途径:(一) JSP页面上是中文,但是看的是后是乱码:解决的办法就是在JSP页面的编码的地方<%@ page language=&quot;java&quot;
contentType=&quot;text/html;charset=GBK&quot;
%>,因为Jsp转成Java文件时的编码问题,默认的话有的服务器是ISO-8859-1,如果一个JSP中直接输入了中文,Jsp把它当作ISO8859-1来处理是肯定有问题的,这一点,我们可以通过查看Jasper所生成的Java中间文件来确认(二) 当用Request对象获取客户提交的汉字代码的时候,会出现乱码:解决的办法是:要配置一个filter,也就是一个Servelet的过滤器,代码如下:import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.UnavailableException;/*** Example filter that sets the character encoding to be used in parsing the* incoming request*/public class SetCharacterEncodingFilter implements Filter {/*** Take this filter out of service.*/public void destroy() {}/*** Select and set (if specified) the character encoding to be used to* interpret request parameters for this request.*/public voiddo
Filter(ServletRequest request, ServletResponse response,FilterChain chain)throws IOException, ServletException {request.setCharacterEncoding(&quot;GBK&quot;);// 传递控制到下一个过滤器chain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException {}}配置web.xml<filter><filter-name>Set Character Encoding</filter-name><filter-class>SetCharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>Set Character Encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping>如果你的还是出现这种情况的话你就往下看看是不是你出现了第四中情况,你的Form提交的数据是不是用get提交的,一般来说用post提交的话是没有问题的,如果是的话,你就看看第四中解决的办法。还有就是对含有汉字字符的信息进行处理,处理的代码是:package dbJavaBean;public class CodingConvert{ public CodingConvert(){//}public String toGb(String uniStr){String gbStr = &quot;&quot;;
if(uniStr == null){uniStr = &quot;&quot;;}try{byte[] tempByte = uniStr.getBytes(&quot;ISO8859_1&quot;);gbStr = new String(tempByte,&quot;GB2312&quot;);}catch(Exception ex){}return gbStr;}public String toUni(String gbStr){String uniStr = &quot;&quot;;
if(gbStr == null){gbStr = &quot;&quot;;}try{byte[] tempByte = gbStr.getBytes(&quot;GB2312&quot;);uniStr = new String(tempByte,&quot;ISO8859_1&quot;);}catch(Exception ex){}return uniStr;}}你也可以在直接的转换,首先你将获取的字符串用ISO-8859-1进行编码,然后将这个编码存放到一个字节数组中,然后将这个数组转化成字符串对象就可以了,例如:String str=request.getParameter(“girl”);Byte B[]=str.getBytes(“ISO-8859-1”);Str=new String(B);通过上述转换的话,提交的任何信息都能正确的显示。(三) 在Formget请求在服务端用request. getParameter(“name”)时返回的是乱码;按tomcat的做法设置Filter也没有用或者用request.setCharacterEncoding(&quot;GBK&quot;);也不管用问题是出在处理参数传递的方法上:如果在servlet中用doGet(HttpServletRequest request, HttpServletResponse response)方法进行处理的话前面即使是写了:request.setCharacterEncoding(&quot;GBK&quot;);response.setContentType(&quot;text/html;charset=GBK&quot;);也是不起作用的,返回的中文还是乱码!!!如果把这个函数改成doPost(HttpServletRequest request, HttpServletResponse response)一切就OK了。同样,在用两个JSP页面处理表单输入之所以能显示中文是因为用的是post方法传递的,改成get方法依旧不行。由此可见在servlet中用doGet()方法或是在JSP中用get方法进行处理要注意。这毕竟涉及到要通过浏览器传递参数信息,很有可能引起常用字符集的冲突或是不匹配。解决的办法是:1) 打开tomcat的server.xml文件,找到区块,加入如下一行: URIEncoding=”GBK” 完整的应如下: <Connector port=&quot;8080&quot;
maxThreads=&quot;150&quot;
minSpareThreads=&quot;25&quot;
maxSpareThreads=&quot;75&quot;
enableLookups=&quot;false&quot;
redirectPort=&quot;8443&quot;
acceptCount=&quot;100&quot;
debug=&quot;0&quot;
connectionTimeout=&quot;20000&quot;
disableUploadTimeout=&quot;true&quot;
URIEncoding=&quot;GBK&quot;/> 2)重启tomcat,一切OK。需要加入的原因大家可以去研究 $TOMCAT_HOME/webapps/tomcat-docs/config/http.html下的这个文件就可以知道原因了。需要注意的是:这个地方如果你要是用UTF-8的时候在传递的过程中在Tomcat中也是要出现乱码的情况,如果不行的话就换别的字符集。(四) JSP页面上有中文,按钮上面也有中文,但是通过服务器查看页面的时候出现乱码:解决的办法是:首先在JSP文件中不应该直接包含本地化的消息文本,而是应该通过<bean:message>标签从Resource Bundle中获得文本。应该把你的中文文本放到Application.properties文件中,这个文件放在WEB-INF/classes/*下,例如我在页面里有姓名,年龄两个label,我首先就是要建一个Application.properties,里面的内容应该是name=”姓名” age=”年龄”,然后我把这个文件放到WEB-INF/classes/properties/下,接下来根据Application.properties文件,对他进行编码转化,创建一个中文资源文件,假定名字是Application_cn.properties。在JDK中提供了native2ascii命令,他能够实现字符编码的转换。在DOS环境中找到你放置Application.properties的这个文件的目录,在DOS环境中执行一下命令,将生成按GBK编码的中文资源文件Application_cn.properties:native2ascii –encoding gbk Application.properties Application_cn.properties执行以上命令以后将生成如下内容的Application_cn.properties文件:name=u59d3u540d age=u5e74u9f84,在Struts-config.xml中配置:<message-resources parameter=&quot;properties.Application_cn&quot;/>。到这一步,基本上完成了一大半,接着你就要在JSP页面上写<%@ page language=&quot;java&quot;
contentType=&quot;text/html;charset=GBK&quot;
%>,到名字的那个label是要写<bean:message key=”name”>,这样的化在页面上出现的时候就会出现中文的姓名,年龄这个也是一样,按钮上汉字的处理也是同样的。(五) 写入到数据库是乱码:解决的方法:要配置一个filter,也就是一个Servelet的过滤器,代码如同第二种时候一样。如果你是通过JDBC直接链接数据库的时候,配置的代码如下:jdbc:mysql://localhost:3306/workshopdb?useUnicode=true&characterEncoding=GBK,这样保证到数据库中的代码是不是乱码。如果你是通过数据源链接的化你不能按照这样的写法了,首先你就要写在配置文件中,在tomcat 5.0.19中配置数据源的地方是在C:Tomcat 5.0confCatalinalocalhost这个下面,我建立的工程是workshop,放置的目录是webapp下面,workshop.xml的配置文件如下:<!-- insert this Context element into server.xml --><Context path=&quot;/workshop&quot;
docBase=&quot;workshop&quot;
debug=&quot;0&quot;reloadable=&quot;true&quot;
><Resource name=&quot;jdbc/WorkshopDB&quot;auth=&quot;Container&quot;type=&quot;javax.sql.DataSource&quot;
/><ResourceParams name=&quot;jdbc/WorkshopDB&quot;><parameter><name>factory</name><value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter><parameter><name>maxActive</name><value>100</value></parameter><parameter><name>maxIdle</name><value>30</value></parameter><parameter><name>maxWait</name><value>10000</value></parameter><parameter><name>username</name><value>root</value></parameter><parameter><name>password</name><value></value></parameter><!-- Class name for mm.mysql JDBC driver --><parameter><name>driverClassName</name><value>com.mysql.jdbc.Driver</value></parameter><parameter><name>url</name><value><![CDATA[jdbc:mysql://localhost:3306/workshopdb?useUnicode=true&characterEncoding=GBK]]></value></parameter></ResourceParams></Context>粗体的地方要特别的注意,和JDBC直接链接的时候是有区别的,如果你是配置正确的化,当你输入中文的时候到数据库中就是中文了,有一点要注意的是你在显示数据的页面也是要用<%@ page language=&quot;java&quot;
contentType=&quot;text/html;charset=GBK&quot;
%>这行代码的。需要注意的是有的前台的人员在写代码的是后用Dreamver写的,写了一个Form的时候把他改成了一个jsp,这样有一个地方要注意了,那就是在Dreamver中Action的提交方式是request的,你需要把他该过来,因为在jsp的提交的过程中紧紧就是POST和GET两种方式,但是这两种方式提交的代码在编码方面还是有很大不同的,这个在后面的地方进行说明。
 

Similar threads

I
回复
0
查看
725
import
I
I
回复
0
查看
2K
import
I
I
回复
0
查看
704
import
I
I
回复
0
查看
668
import
I
I
回复
0
查看
1K
import
I
顶部