web.xml配置 struts 提交表单乱码问题解决

本文介绍了如何在Struts框架中处理中文乱码问题,包括在web.xml中设置字符集过滤器及编写相关过滤类,确保页面提交请求时正确解析中文字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts中经常遇到提交中文乱码问题,既是头疼,所以写了一个过滤器,用来处理乱码问题。

web.xml设置对页面提交请求字符集进行设置:
1,首先在web.xml中设置对字符集过滤器
2,编写相关过滤器类

1,首先在web.xml中设置对字符集过滤器
  这里设置字符集为:GBK
过滤类为: SetCharacterEncodingFilter   <context-param>
    <param-name>weblogic.httpd.inputCharset./*</param-name>
    <param-value>GBK</param-value>
  </context-param>
  <filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>com.bjzfy.zfzj.gg.util.SetCharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>GBK</param-value>
    </init-param>
  </filter>



2,编写过滤类
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;


public class SetCharacterEncodingFilter implements Filter {

protected String encoding = null;

protected FilterConfig filterConfig = null;

protected boolean ignore = true;


public void destroy() {

    this.encoding = null;

    this.filterConfig = null;

  }


  public void doFilter(ServletRequest request, ServletResponse response,

                       FilterChain chain) throws IOException, ServletException {


// Conditionally select and set the character encoding to be used

    if (ignore || (request.getCharacterEncoding() == null)) {

      String encoding = selectEncoding(request);

      if (encoding != null)

        try{

          request.setCharacterEncoding(encoding);

        }catch(Exception e){

          e.printStackTrace();

        }

    }


// Pass control on to the next filter

    try{

      chain.doFilter(request, response);

    }

    catch(IOException e){

      e.printStackTrace();

    }

    catch(ServletException se){

      se.printStackTrace();

    }


  }


  public void init(FilterConfig filterConfig) throws ServletException {


    this.filterConfig = filterConfig;

    this.encoding = filterConfig.getInitParameter("encoding");

    String value = filterConfig.getInitParameter("ignore");

    if (value == null)

      this.ignore = true;

    else if (value.equalsIgnoreCase("true"))

      this.ignore = true;

    else if (value.equalsIgnoreCase("yes"))

      this.ignore = true;

    else

      this.ignore = false;


}


protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}


}


  http://www.glassfanr.com/thread-176-1-1.html
来自: 谷歌眼镜社区    尊重他人劳动成果,转载请说明出处
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值