Struts中Jsp中文乱码解决方案

    技术2022-05-11  124

    Struts中Jsp中文乱码解决方案     我们以UTF-8编码格式为例,首先确保我们所编写的Jsp文件为UTF-8编码格式的文本文件(中文操作系统文本文件默认存储格式为GBK,这一点要注意),在jsp文件按开头注明<%@ page contentType="text/html;charset=UTF-8" language="java" %>, 以便使tomcat等web服务器正常识别。     我们可以采用filter方案。代码如下: package heroking.etc; import javax.servlet.*; import java.io.IOException; public class SetCharacterEncodingFilter implements Filter {      protected FilterConfig filterConfig;     protected String encodingName;     protected boolean enable;     public SetCharacterEncodingFilter() {         this.encodingName = "UTF-8";         this.enable = false;     }     public void init(FilterConfig filterConfig) throws ServletException {         this.filterConfig = filterConfig;         loadConfigParams();     }     private void loadConfigParams()  {         //encoding         this.encodingName = this.filterConfig.getInitParameter("encoding");         //filter enable flag...         String strIgnoreFlag = this.filterConfig.getInitParameter("enable");         if (strIgnoreFlag.equalsIgnoreCase("true"))       {             this.enable = true;         } else  {             this.enable = false;         }      }      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException        {         if(this.enable) {             request.setCharacterEncoding(this.encodingName);         }         chain.doFilter(request, response);     }     public void destroy() {     } } 在web.xml配置如下: 1,web.xml的头描述应该是2.3版本以上, <!DOCTYPE web-app   PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"   "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> 2,filter配置:     <filter>         <filter-name>UTF-8Filter</filter-name>         <display-name>UTF-8Filter</display-name>         <filter-class>heroking.etc.SetCharacterEncodingFilter</filter-class>         <init-param>             <param-name>encoding</param-name>             <param-value>UTF-8</param-value>         </init-param>         <init-param>             <param-name>enable</param-name>             <param-value>true</param-value>         </init-param>     </filter>     <filter-mapping>         <filter-name>UTF-8Filter</filter-name>             <url-pattern>/*</url-pattern>     </filter-mapping> 另外一种方案为struts框架服务的: 代码如下: package heroking.etc; import org.apache.struts.action.ActionServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ActionServletUTF8Encoding  extends ActionServlet {     protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException {         //如果JSP用GBK,这里就用GBK吧!         request.setCharacterEncoding("UTF-8");         super.process(request, response);     } } 在web.xml配置如下: <servlet>     <servlet-name>action</servlet-name>     <servlet-class>heroking.etc.ActionServletUTF8Encoding</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>2</param-value>     </init-param>     <init-param>       <param-name>detail</param-name>       <param-value>2</param-value>     </init-param>     <load-on-startup>2</load-on-startup>   </servlet>

    最新回复(0)