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

    1如果使用的tomcat服务器,在server.xml中Connector 标签后加 URIEncoding="UTF-8";

    2使用web过滤器:

    (1)、新建一个SetCharacterEncodingFilter.java的类:

         
    package com.util;
    import java.io.IOException;
    import javax.servlet.*;

    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
     {
        if (ignore || (request.getCharacterEncoding() == null))
        {
         String encoding = selectEncoding(request);
         if (encoding != null)
          request.setCharacterEncoding(encoding);
        }
        chain.doFilter(request, response);
     }

     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);
     }


    }

    (2)web.xml中在web-app里加入:

          <!--定义一个过滤器, 并设定其初始化参数--><filter>
       <filter-name>Set Character Encoding</filter-name>
       <filter-class>com.util.SetCharacterEncodingFilter</filter-class>
       <init-param>
        <param-name>encoding</param-name>
        <param-value>GB2312</param-value>
       </init-param>
    </filter>
    <!--制定过滤器映射-->
    <filter-mapping>
       <filter-name>Set Character Encoding</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>

    3.<%@ page language="java" import="java.util.*,com.scce.entity.*" pageEncoding="utf-8"%>

       <head>

      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

       </head>

    4.String name=new String(request.getParameter("name").getBytes("ISO8859-1"),"GBK"); 

                           new String(fileName.getBytes("UTF-8"), "GBK")

    5.request.setCharacterEncoding("gbk");

      response.setContentType("text/html;charset=gbk");

  • 相关阅读:
    【Mysql】可视化工具
    【Mysql】Mysql 各个版本区别
    【Linux】rpm常用命令及rpm参数介绍
    【Mysql】mysql和mariadb的区别
    【CentOS】设置服务开机自动启动
    【Linux】查看所使用的Linux系统是32位还是64 位的方法
    【Linux】安装openssh-server依赖openssh-client版本错误的解决办法
    【Linux】apt-get 源地址汇总
    【Linux】编辑文件时,箭头按键还有BACKSPACE按键不能正常使用的解决办法
    VMware设置桥接上网
  • 原文地址:https://www.cnblogs.com/blog-yuesheng521/p/4953996.html
Copyright © 2011-2022 走看看