servlet乱码以及解决
// 浏览器提交的数据是000110011(码表中对应的《编码》 )等东西。
// 浏览器以什么《码表》打开浏览器(而空中浏览器使用的编码是:<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 这条语句控制的),那么就以什么《码表》提交数据。
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test1(request, response);
}
//get提交时候的乱码处理
private void test1(HttpServletRequest request, HttpServletResponse response)
throws IOException, UnsupportedEncodingException {
response.getWriter().write(request.getCharacterEncoding()+"<br/>");
//得到以get类型提交的数据,由于设置request.setCharacterEncoding("UTF-8")对get类型的无效
String username=request.getParameter("username");
username=new String(username.getBytes("iso8859-1"),"UTF-8");
response.getWriter().write(username);
}
我用的是tomcat服务器,tomcat默认使用的码表ISO8889

也可以设服务器的conf下的server.xml文件(不过这个一般不推荐使用!)
服务器下的server.xml 文件的设置:
如: <Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
另外一种设置(这样就表示使用post方式中的编码):
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" useBodyEncodingForURI="true"/>
假如使用这种形式:那么request.setCharacterEncoding("UTF-8");也对get类型提交的数据有效
也就是下面这种设置:
//处理浏览器提交的数据《post》类型的
private void test(HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException, IOException {
//这种只能处理《post提交的数据有效,对get提交的数据没有用》
request.setCharacterEncoding("UTF-8");//假如提交的时候用的是《UTF-8》的编码,那么在这里使用
//UTF-8来解析 request.getParameter("username");在这里的时候就会以UTF-8来解析了 response.setCharacterEncoding("UTF-8");//由于response.getWriter()返回的字符流,所以需要设置输出的
response.setHeader("Content-type", "text/html;charset=UTF-8");//这个高速浏览器以什么编码解析html文件
String username=request.getParameter("username");
String password=request.getParameter("userpassWord");
response.getWriter().write(username);
response.getWriter().write(password);
}
如有错误,欢迎指点
最终版本,使用filer解决全站乱码
public class CharacterEncodingFilter implements Filter {
private FilterConfig config=null;
private String defaultCharset="UTF-8";//缺省的编码
public void init(FilterConfig filterConfig) throws ServletException {
this.config=filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) resp;
String charset=this.config.getInitParameter("charset");
if(charset==null||"".equals(charset)){
charset=defaultCharset;
}
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
response.setContentType("text/html;charset="+charset);
CharacterEncodingRequest requestWrapper=new CharacterEncodingRequest(request);
chain.doFilter(requestWrapper, response);
}
public void destroy() {
}
}
class CharacterEncodingRequest extends HttpServletRequestWrapper{
private HttpServletRequest requst;
public CharacterEncodingRequest(HttpServletRequest request) {
super(request);
this.requst=request;
}
@Override
public String getParameter(String name) {
try {
String value=this.getParameter(name);
if(value==null)return null;
if(!this.requst.getMethod().equalsIgnoreCase("get"))return value;//忽略大小写
value=new String (value.getBytes("ISO8859-1"),this.requst.getCharacterEncoding());//这里是没有更改tomcat默认编码的时候
return value;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
xml文件
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>cn.my.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
servlet乱码以及解决的更多相关文章
- JS.中文乱码,Jsp\Servlet端的解决办法
JS.中文乱码,Jsp\Servlet端的解决办法 2010-03-08 15:18:21| 分类: Extjs | 标签:encodeuricomponent 乱码 urldecoder ...
- servlet 乱码解决方法
一. servlet 发送的html 页面中文乱码 解决方法, 1.加入如下代码 response.setCharacterEncoding("UTF-8"); 2.在html页面 ...
- Servlet乱码解决
后端收前端 1.post乱码 可以通过 request.setCharacterEncoding("utf-8"); 这样在后端可以接收中文 2.get乱码(手动解决) 可以通过 ...
- Servlet中文乱码原因 解决 Get 和 Post 和客户端
一.Get方式的中文乱码 1) 使用如下页面表单内容: <form action="http://127.0.0.1:8080/day07/params" method=&q ...
- servlet乱码问题总结
在学习时servlet乱码问题还是挺严重的,总结一下有三种情况 1.新建HTML页面后浏览出现乱码 2.以post形式请求时出现乱码 3.以get形式请求时出现乱码 让我们一个一个来解决吧 1.新建H ...
- JAVA-----乱码的处理 乱码的解决方法总结
为什么说乱码是程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!工作遇到各种各样的乱码的解决方法总结一下. 对于Java由于默认的编码方式是 ...
- JSP的学习(4)——中文乱码的解决
本篇将以JSP页面中可能存在的中文乱码问题进行分析和解决. 中文乱码的问题一直是国人在编程过程中的一大头疼问题,这点上在JSP.Servlet或Tomcat上随处可见.比如我们在写一个Servlet时 ...
- SpringMVC中文乱码的解决办法
中文乱码分类: (1)按照请求分类: GET请求乱码 POST请求乱码 (2)按照乱码位置分类 从前台传到后台的数据乱码(存储到数据库中的数据乱码) 从后台传到前台的数据乱码(显示在页面的数据乱码) ...
- Servlet乱码问题解决
对于请求参数的编码处理基本上分为get和post两种情况. 1.POST index.html <!DOCTYPE html> <head> <meta http-equ ...
随机推荐
- SQL Server -ISNULL()函数
SQL中有多种多样的函数,下面将为您介绍SQL中的ISNULL函数,包括其语法.注释.返回类型等,供您参考,希望对您学习SQL能够有所帮助. ISNULL 使用指定的替换值替换 NULL. 语法ISN ...
- VC++ try catch (转)
VC++ try catch (转) 以前都是用try{} catch(-){}来捕获C++中一些意想不到的异常, 今天看了Winhack的帖子才知道,这种方法在VC中其实是靠不住的.例如下面的代 ...
- PHP MAIL DEMO(程序代码直接发送邮件)
php代码 <?php // 收件人邮箱地址 $to = 'xxxxxx@qq.com'; // 邮件主题 $title = '测试邮件发送'; // 邮件内容 $msg = '这是一封测试邮件 ...
- 函数:我的地盘听我的 - 零基础入门学习Python019
函数:我的地盘听我的 让编程改变世界 Change the world by program 函数与过程 在小甲鱼另一个实践性超强的编程视频教学<零基础入门学习Delphi>中,我们谈到了 ...
- debian系统下安装ssh服务
它是什么?? SSH 为 Secure Shell 的缩写,简单地说,SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用 SSH 协议可 ...
- javascript中遍历EL表达式List集合中的值
http://www.cnblogs.com/limeiky/p/6002900.html
- JS 浮点计算BUG
最近做项目的时候遇到一个比较纠结的js浮点计算问题. 当时是做利率计算,因为利率大多数涉及到小数点,精度要求也很高. 0.6+0.1+0.1=? 结果出现:0.7999999999999 网上查找了一 ...
- Square spiral
Square spiral Nikola picks up a strange circuit board. All of its elements are connected in a spiral ...
- UDP数据接收服务器
简介 这是我在做一个要用UDP方式进行数据传输时,自己写的一个多线程的UDP数据接收服务器, 它能将接收到的UDP数据包存成文件,并提供数据包接收时间监测: 还支持键盘命令响应,以将数据写到新的文件, ...
- 【转】repo 的一些用法和理解-不错
原文网址:http://blog.csdn.net/yasin_lee/article/details/5975068 repo的用法(zz) 注:repo只是google用Python脚本写的调用g ...