关于struts项目中页面传值到后台时乱码问题解决总结



Struts1 ActionForm取值乱码解决方案:
 1. 更改jsp编码:
  (1)加入 pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"(无效)
  (2)<% request.setCharacterEncoding("utf-8"); %> (无效)--post
 2. tomcat配置conf/server.xml在<Connector/>中加入URIEncoding="UTF-8" useBodyEncodingForURI="true"(无效)--get
 3. 更改IE编码: 打开IE,按ALT键--查看--编码--UTF8(无效)
 4. 后台更改编码:
  (1) String a=request.getParameter("a"); a=new String(a.getBytes("ISO8859_1"),"UTF-8");(无效)--get
  (2) getParameter()前设置request.setCharacterEncoding("UTF-8");(无效)
  (3) model类里面设置编码: (有效)
   public void reset(ActionMapping mapping, HttpServletRequest request) {
    try {
     request.setCharacterEncoding("UTF-8");
    } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
    }
   }
 5. 添加过滤器:有效)
  (1) 过滤器类:
    package com.util;
 
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
   
    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.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    //过滤器处理表单传到servlet的乱码问题
    public class MyFilter implements Filter{
     //自写一个request换掉原来的request,重写里面的getParemeter方法,可以设置编码
     class MyRequest extends HttpServletRequestWrapper{
     
      @Override
      public String getParameter(String param) {
       String value = null;
       try {
        //post
        super.setCharacterEncoding(encoding);//把编码转换为encoding
        value = super.getParameter(param);
        if(super.getMethod().equalsIgnoreCase("GET")){
         if(value!=null){
          value = new String(value.getBytes("iso8859-1"),encoding);
         }
        }
       } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
       return value;
      }

     public MyRequest(HttpServletRequest request) {
       super(request);
      }
     
     }
     protected String encoding=null;
     public void destroy() { //销毁
      // TODO Auto-generated method stub
      this.encoding=null;
     }
    //对编码问题进行转换
     public void doFilter(ServletRequest request, ServletResponse response,
       FilterChain chain) throws IOException, ServletException {
      // TODO Auto-generated method stub
      response.setContentType("text/html;charset="+encoding);
      //过滤未登录用户
      HttpServletRequest req = (HttpServletRequest) request;
      HttpServletResponse resp = (HttpServletResponse) response;
      String path=req.getServletPath();
      String param=req.getQueryString();
      if(path!=null){
       path=path+"?"+param;//全请求路径
      }
      if(path.endsWith("myAddress")||path.endsWith("myJingDong")||path.indexOf("myShouCang")!=-1||path.endsWith("updateUser")||path.indexOf("showOrder")!=-1||path.indexOf("showValidOrder")!=-1||path.indexOf("showCancelOrder")!=-1||path.indexOf("fillOrder")!=-1){
       HttpSession session = req.getSession();
       String userName = (String) session.getAttribute("username");
       if(userName == null){
        session.setAttribute("url", path.replaceFirst("/", ""));
        System.out.println(session.getAttribute("url"));
        resp.sendRedirect("user.do?op=loginAction");
        return;
       }
      }
      //把过滤器给下一个过滤器或资源处理器
      chain.doFilter(new MyRequest((HttpServletRequest) request), response);
     }
   
     public void init(FilterConfig filterConfig) throws ServletException {
      // TODO Auto-generated method stub
      this.encoding=filterConfig.getInitParameter("encoding");//encoding在web.xml中指定
     }
   
    }
  (2) web.xml对该过滤器进行注册和映射:
   <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.util.MyFilter</filter-class>
    <init-param>
     <param-name>encoding</param-name>
     <param-value>utf-8</param-value>
    </init-param>
   </filter>
   <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值