zoukankan      html  css  js  c++  java
  • 乱码问题

    乱码分为请求乱码,响应乱码

    请求乱码分为 get请求和post请求

    如果是post请求,则简单一点,request.setCharacterEncoding("utf-8");

    如果是get 请求  则要麻烦一点,对于获取的值进行编解码  String str2=new String(str.getBytes("iso8859-1"),"utf-8");

    如果想解决全站乱码就要用到过滤器了(因为我们从request中获取到值的时候已经是乱码了,所以要想解决乱码就要解决request几个方法的乱码问题,大致是这三个方法,request.getParameters(),getParameterValues(),getParameterMap()).

    在这三个方法中解决乱码问题,首先我们想到的是过滤器,当请求进来的时候我们对请求进行过滤拦截,然后对request进行包装,并重写这三个方法,在方法中根据请求类型进行乱码处理

    具体看下面代码:

    package filter;

    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.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class MyFilter implements Filter {

    @Override
    public void destroy() {
    // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    HttpServletRequest myRequest=(HttpServletRequest)request;     //强转
    HttpServletResponse myResponse=(HttpServletResponse)response;//

    myRequest.setCharacterEncoding("utf-8");                                     //设置请求编码,只对post有效
    myResponse.setCharacterEncoding("utf-8");                                    //设置服务器响应编码格式
    myResponse.setContentType("text/html;charset=utf-8");                 //设置响应页面编码格式和编码格式

    MyRequest mr=new MyRequest(myRequest);                                  //对request进行包装并继承HttpServletRequestWrapper并重写三个方法;
    chain.doFilter(mr, myResponse);                                                     //放行

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    // TODO Auto-generated method stub

    }

    }

    //重写三个方法,进行重新编码

    package filter;

    import java.io.UnsupportedEncodingException;
    import java.util.Map;
    import java.util.Set;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;

    public class MyRequest extends HttpServletRequestWrapper {

    private HttpServletRequest request;

    public MyRequest(HttpServletRequest request) {
    super(request);
    this.request=request;
    }


    @Override
    public String getParameter(String name) {
    try {
    String type =this.request.getMethod();
    String myName = this.request.getParameter(name);
    if ("get".equalsIgnoreCase(type)) {

    return new String(myName.getBytes("iso8859-1"),"utf-8");
    }
    return myName;
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw new RuntimeException();
    }

    }

    @Override
    public Map getParameterMap() {
    try {
    String type = this.request.getMethod();
    Map<String,String[]> map = this.request.getParameterMap();
    if(map!=null&&"get".equalsIgnoreCase(type)){
    Set<String> keySet = map.keySet();
    for (String key : keySet) {
    String[] strings = map.get(key);
    if (strings!=null&&strings.length!=0) {
    for (int i = 0; i < strings.length; i++) {
    strings[i]=new String(strings[i].getBytes("iso8859-1"),"utf-8");
    }
    }
    }

    }
    return map;
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw new RuntimeException("getParameterMap出错");
    }
    }

    @Override
    public String[] getParameterValues(String name) {

    try {
    String type = this.request.getMethod();
    String[] values = this.request.getParameterValues(name);
    if("get".equalsIgnoreCase(type)){
    if (values!=null&&values.length!=0) {
    for (int i = 0; i < values.length; i++) {
    values[i]=new String(values[i].getBytes("iso8859-1"),"utf-8");
    }
    }
    }
    return values;
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw new RuntimeException("getParameterValues出错");
    }
    }

    }

    最后,不要忘记在web.xml中配置过滤器

    <filter>
    <filter-name>myFilter</filter-name>
    <filter-class>filter.MyFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

  • 相关阅读:
    【POJ】[1703]Find them, Catch them
    【杭电】[2717]Catch That Cow
    【杭电】[2717]Catch That Cow
    【杭电】[1716]排列2
    【杭电】[1716]排列2
    【杭电】[2084]数塔
    【杭电】[2084]数塔
    【杭电】[1003]Max Sum
    【杭电】[1003]Max Sum
    [leetcode]117. Populating Next Right Pointers in Each NodeII用next填充同层相邻节点
  • 原文地址:https://www.cnblogs.com/shaoyang/p/5701431.html
Copyright © 2011-2022 走看看