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");
java中的乱码问题的更多相关文章
- java中避免乱码
response.setContentType("text/html;charset=UTF-8"); 这个是在action中的 这个是在json中设置乱码的 contentTyp ...
- java中byte, iso-8859-1, UTF-8,乱码的根源
Post@https://ryan-miao.github.io 背景 还是多语言, 在项目中遇到本地环境和服务端环境不一致乱码的情形.因此需要搞清楚乱码产生的过程,来分析原因. 获取多语言代码如下: ...
- java中几种常见字符集与乱码介绍
1. ASCII和Ansi编码 字符内码(charcter code)指的是用来代表字符的内码 .读者在输入和存储文档时都要使用内码,内码分为 单字节内码 -- Single-Byte chara ...
- Java中url传递中文参数取值乱码的解决方法
java中URL参数中有中文值,传到服务端,在用request.getParameter()方法,得到的常常会是乱码,这将涉及到字符解码操作. 方法一: http://xxx.do?ptname=’我 ...
- java中可以出现的中文乱码的集中解决
从学习javaweb开始就会经常遇到中文乱码,今天就做以下记录: 1. 要避免项目中遇到乱码,首先就是在搭建项目的设置工作空间的字符编码,若是多人开发,就更应该做到统一,在eclipse中选择widn ...
- JAVA中传递参数乱码问题
url传递中文如果jsp页面,myeclipse.web.xml中org.springframework.web.filter.CharacterEncodingFilter,都是UTF-8编码,直接 ...
- java中的中文参数存到数据库乱码问题
关于java中的中文参数乱码问题,遇见过很多,若开发工具的字符集环境和数据库的字符集环境都一样,存到数据库中还是乱码的话,可以通过以下方法解决: 用数据库客户端检查每个字段的字符集和字符集校对和这个表 ...
- java乱码详解(java中byte与char的转换)
转自:http://hi.baidu.com/%C6%F3%D2%B5%BC%D2%D4%B0/blog/item/825a4858d6248e8b810a181a.html java byte与 ...
- Java中FTPClient上传中文目录、中文文件名乱码问题解决方法【好用】
转: Java中FTPClient上传中文目录.中文文件名乱码问题解决方法 问题描述: 使用org.apache.commons.net.ftp.FTPClient创建中文目录.上传中文文件名时,目录 ...
随机推荐
- Linux_sudo权限
一.sudo权限(只能由管理员操作) 1. 操作对象 --> 命令(命令也是文件) 2. 命令存放路径/sbin与/bin --> 只由root管理员用户操作 3. 实际工作中,是不允许你 ...
- CWnd与HWND的简单辨析
今天在写一个小的网络应用,需要用到HWND类型的一个参数.而程序中有的“窗口操作句柄”只有一个CWnd类型的指针.这俩不都是“窗口句柄”么?而且反正都是地址直接转换使用如何?结果出现了调用失效的情况. ...
- javaweb实验五
product类: package com.lab;public class Product { private int id; // 商品编号 private S ...
- DOS运行命令
运行命令主要是DOS操作系统的运行方式.DOS时代的时候,为了方便用户的操作,微软公司将一些常用的命令,如DIR,CD等命令全部集成在系统里面. 基本定义 对于DOS来说是一个很大的优点.而存放这些内 ...
- php引用&符号详解——————给变量起小名
学习了这篇博客[http://blog.csdn.net/jiedushi/article/details/6428585] php中引用采用的是“写时拷贝”的原理,就是除非发生写操作,指向同一个地址 ...
- jquery 同id使用指针寻找具体每个id的值
this来判断<script language="javascript"> function t(obj) { alert(obj.value); } </scr ...
- Cocos2dx集成于windows桌面窗口程序的步骤
2D游戏需要做编辑器,而编辑器总是希望可以复用游戏中的逻辑来运行场景试看效果. 对于cocos2dx开发的程序,这个需求可以描述为: 实现一种方法,在桌面窗口程序中的某个控件上显示cocos2dx的场 ...
- 【测试分析】HTSM模型
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5508428.html 概述 HTSM全称Heuristic ...
- 贴片三极管-MOS管型号手册
详细请查阅PDF: http://files.cnblogs.com/files/BinB-W/贴片三极管-MOS管型号手册.pdf
- Python之路-python(常用模块学习)
模块介绍 time &datetime模块 random os sys shutil shelve xml处理 yaml处理 configparser hashlib re正则表达式 1.模块 ...