zoukankan      html  css  js  c++  java
  • jsp的url后跟中文参数传参出现乱码

    ①重新编码:String urlParam= request.getParameter("urlParam");
      urlParam= new String(urlParam.getBytes("ISO-8859-1"), "UTF-8");

    ②tomcat中统一编码

    tomcat  的server.xml中在相对应的端口中加下面两句
    useBodyEncodingForURI="true" 
    URIEncoding="UTF-8"

    在web.xml文件中配一个过滤器:(没有试验成功)

    第一步:<filter>
    <filter-name>filter</filter-name>
    <filter-class>com.social.filter.CodeFilter</filter-class> 设置过滤器的类名(filter包下的CoderFilter类)
    </filter>
    <filter-mapping>
    <filter-name>filter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    第二步:编写com.social.filter.CodeFilter类:

    import javax.servlet.Filter;

    public class CodeFilter implements Filter {

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

    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1,
    FilterChain arg2) throws IOException, ServletException {
    // TODO Auto-generated method stub
    //把ServletRequest强制转化为HttpServletRequest
    HttpServletRequest request=(HttpServletRequest)arg0;
    HttpServletResponse response=(HttpServletResponse)arg1;
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    arg2.doFilter(request, response);
    }

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

    }

    }

     ④设置request的编码格式

    request.setCharacterEncoding("utf-8");

    String ss=request.getParameter("ss");

    ⑤声明一个方法来处理乱码问题

    具体实现过程与操作步骤如下。
    创建页面declaration.jsp,在该页面中主要是声明一个方法,完成乱码的处理。
    <%@ page language="java" contentType="text/html; charset=GB2312"%>

    <%!String tran(String str) { String res=null; byte temp[]; try { temp=str.getBytes("Gb2312"); res=new String(temp); } catch(java.io.UnsupportedEncodingException e) { System.out.println(e.toString()); }

    return res;
    } %>

    <%! int count =10; %>

    <% String source="你好!中国"; for(int i=0;i<count;i++) out.println(tran(source)); %>
    在以上代码中,在声明的tran()方法中,使用字符串的getBytes()方法将字符集重新编码成GB2312。然后使用for循环,将“你好!中国”显示出来。

  • 相关阅读:
    KeBugCheckEx0xD1
    Ti DSP编程入门
    动态链接库
    Hello,world! x86版本
    用回调函数联系两个模块
    DataGrid模版列超级链接列传递参数问题总结(多个参数传递)
    后台代码里执行弹出脚本方法!(Response.Write)
    Oracle数据导入导出imp/exp
    安装sql2000: 出现 A previous program installation created pending file operations on the installation machine. You must restart the computer before running setup
    解决问题:  An unhandled exception of type 'System.Exception' occurred in system.data.oracleclient.dll
  • 原文地址:https://www.cnblogs.com/zhaochunhua/p/3776101.html
Copyright © 2011-2022 走看看