解决Struts乱码问题报错。只有这么多分了,解决后立刻给分
解决Struts乱码问题报错。
我的解决方法是:
1.所有页面都定义
<%@ page language= "java " contentType= "text/html;charset=UTF-8 " %>
2.配置Servelet的过滤器
新建一个类SetCharacterEncodingFilter.java
package com.vivian8117.struts;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.sun.org.apache.xalan.internal.xsltc.dom.Filter;
public class SetCharacterEncodingFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
request.setCharacterEncoding( "UTF-8 ");
// 传递控制到下一个过滤器
chain.doFilter(request, response);
}
public boolean test(int node) {
// TODO Auto-generated method stub
return false;
}
}
3配置web.xml 加上下面这段就报错,不加就没问题
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> com.vivian8117.struts.SetCharacterEncodingFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
4.打开tomcat的server.xml文件,找到区块,加入如下一行:
URIEncoding= "UTF-8 "
完整的应如下:
<Connector port= "8080 " maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 " enableLookups= "false " redirectPort= "8443 " acceptCount= "100 " debug= "0 " connectionTimeout= "20000 " disableUploadTimeout= "true " URIEncoding= "UTF-8 "/>
我的web.xml是
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app xmlns= "http://java.sun.com/xml/ns/j2ee " xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance " version= "2.4 " xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<display-name> vivian8117 </display-name>
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> com.vivian8117.struts.SetCharacterEncodingFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
<servlet>
<servlet-name> action </servlet-name>
<servlet-class> org.apache.struts.action.ActionServlet </servlet-class>
<init-param>
<param-name> config </param-name>
<param-value> /WEB-INF/struts-config.xml </param-value>
</init-param>
<init-param>
<param-name> debug </param-name>
<param-value> 3 </param-value>
</init-param>
<init-param>
<param-name> detail </param-name>
<param-value> 3 </param-value>
</init-param>
<load-on-startup> 0 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name> action </servlet-name>
<url-pattern> *.do </url-pattern>
</servlet-mapping>
<display-name> defaultroot </display-name>
<servlet>
<servlet-name> debugjsp </servlet-name>
<description> Added to compile JSPs with debug info </description>
<servlet-class> org.apache.jasper.servlet.JspServlet </servlet-class>
<init-param>
<param-name> classdebuginfo </param-name>
<param-value> true </param-value>
</init-param>
<load-on-startup> 3 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name> debugjsp </servlet-name>
<url-pattern> *.jsp </url-pattern>
</servlet-mapping>
</web-app>
报错内容:
HTTP Status 404 - /vivian8117/a.html
type Status report
message /vivian8117/a.html
description The requested resource (/vivian8117/a.html) is not available.
Apache Tomcat/5.0.28
index.jsp为
<%@ page language= "java " contentType= "text/html;charset=UTF-8 " %>
<html> <body> loading... <div>
<script> window.location.href= '/vivian8117/index.do '; </script>
</div> </body> </html>
Console中报错为:
2007-1-14 12:33:40 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2007-1-14 12:33:40 org.apache.catalina.core.StandardContext start
严重: Context startup failed due to previous errors
[解决办法]
web.xml 中关于中文乱码处理的部分我是这样写的
<!-- ******************* Character Encoding ********************* -->
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> hoso.common.FcEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> Shift_JIS </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
你参考修改一下看看吧