jive中文問題解決方案

袁新宇發表於2003-08-20
jive中文解決方案

1、將所有的jsp頁面中的charset由ISO-8859-1改為GBK。
2、在web.xml中加入如下的filter:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>jsp</servlet-name>
</filter-mapping>
該類的原始檔見附件。
3、在中文環境下重新編譯src下面的所有的java檔案。

上面三步完成後,基本上就可以輸入和顯示中文了。

4、在根目錄下的header.jsp中加入<%@ page contentType="text/html;charset=GBK" %>
5、將所有頁面中的<%@ include file="header.jsp" %>改為<jsp:include page="header.jsp" />

這兩步解決標頭檔案的中文問題。

6、修改com.jdon.picture.Upload.java,將36行中的MultipartRequest(request,uploaddir,size * 1024)改為MultipartRequest(request,uploaddir,size * 1024,"GBK")
7、修改upload.jsp,在String icon=request.getParameter(request.getParameter("picname"))後面增加下面這一行
icon=(String)session.getAttribute("picname")+icon.substring(icon.lastIndexOf('.'));
在<input type="hidden" name="picname" value="<%=picname%>">後面增加下面這一行
<%session.setAttribute("picname",picname);%>

這兩步解決圖片上傳時的中文問題。

以上在windows2000+jdk1.4+tomcat4.18+oracle8下測試透過。

下面是SetCharacterEncodingFilter.java的原始碼

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;
import javax.servlet.UnavailableException;

public class SetCharacterEncodingFilter implements javax.servlet.Filter {

protected String encoding = "GBK";

protected FilterConfig filterConfig = null;


public void setFilterConfig(FilterConfig config) {
this.filterConfig = config;
}


public FilterConfig getFilterConfig() {
return filterConfig;
}

public void destroy() {

this.encoding = null;
this.filterConfig = null;

}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

// Select and set (if needed) the character encoding to be used
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
System.out.println("encoding:"+encoding);
}

// Pass control on to the next filter
chain.doFilter(request, response);

}

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
System.out.println("³õʼ»¯encoding...");
}

protected String selectEncoding(ServletRequest request) {

return (this.encoding);
}


}

相關文章