也可以在每个formbean的reset方法中加try {
httpServletRequest.setCharacterEncoding("GBK");
}
catch (UnsupportedEncodingException ex) {
}
httpServletRequest.setCharacterEncoding("GBK");
}
catch (UnsupportedEncodingException ex) {
}
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>把上面这段加在<web-app>中然后把TOMCAT_HOME\webapps\examples\WEB-INF\classes\filters下的SetCharacterEncodingFilter.class拷贝到你自己的相应目录下
这个函数需要在网页中定义
public String codeString(String s)
{
String str=s;
try
{
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
return str;
}
catch(Exception e)
{
return str;
}
}
我如你所提示的将以上内容放到D:\Tomcat\webapps\ROOT\WEB-INF\下的web.xml中,并将文件SetCharacterEncodingFilter.class拷贝到D:\Tomcat\webapps\ROOT\WEB-INF\classes\filters下(我的文件放在D:\Tomcat\webapps\ROOT\下),还是不能显示从表单文本框提取的汉字。不知错在何处?
另,我其实通过在ch10-2.jsp源代码中加入以下语句<% request.setCharacterEncoding("GB2312");%>实现中文显示,但觉得你这种方法可行的话,就不必每个文件都写一句,省去很多麻烦,所以还想请你详细说明一下,谢谢!!!
修改后的ch10-2.jsp源代码如下:
<%@page contentType="text/html;charset=GBK"%>
<html>
<title>param的使用方法</title>
<body><% request.setCharacterEncoding("GB2312");%><jsp:useBean id="data" scope="page" class="ch10.UserData"/>
<jsp:setProperty name="data" property="userName" param="myname"/>
<jsp:setProperty name="data" property="userNum" param="phone"/>
用户:<font color=blue>
<jsp:getProperty name="data" property="userName"/>
</font><br>
分机号码:<font color=blue>
<jsp:getProperty name="data" property="userNum"/>
</body>
</html>
public String convert(String s){
try{
return new String(s.getBytes("ISO-8859-1"));
}catch(Exception e){return null;}
}
%>
...
<%=convert(myname)%>
...