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 ...
随机推荐
- Controller返回值类型ActionResult
在mvc中所有的controller类都必须使用"Controller"后缀来命名 并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonA ...
- Server.HTMLEncode用法
Server.HTMLEncode用法!! Server.HTMLEncode HTMLEncode 一.HTMLEncode 方法对指定的字符串应用 HTML 编码. 语法 Server.HTMLE ...
- AngularJs练习Demo10 ngInclude
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- Java的优先级
序列号 符号 名称 结合性(与操作数) 目数 说明 1 . 点 从左到右 双目 ( ) 圆括号 从左到右 [ ] 方括号 从左到右 2 + 正号 从右到左 单目 - 负号 从右到左 单目 ++ 自增 ...
- win8 Pro 64位在 UEFI模式下Ghost系统 备份 恢复
一:在win8 安装U 盘中 1. 新建 “Ghost” 文件夹 2. 将下载的Ghost64.exe 文件拷贝到文件夹 二: 启动的时候 按下F12 选择 HDDUSB 1.Windows 安装 ...
- C++的学习记录 - 0
最近玩Arduino发现,在编写函数库的时候要用到C++.正好手头有一本教材,于是时隔2年,开始重学. 又看到重载.构造.拷贝这些词竟然还有些小兴奋. 开个系列日志记录一下学习过程中的问题和体会. 看 ...
- 怎么在网页中加入ICO图标
1.首先制作一个16x16的icon图标,命名为cssbbs.ico(这里的名字可以随便改!),放在根目录下.2.然后将下面的代码嵌入head区:<link rel="icon&quo ...
- C和C++的学习过程总结
总是被同学们问到,如何学习C和C++才不茫然,才不是乱学,想了一下,这里给出一个总的回复. 一家之言,欢迎拍砖哈. 1.可以考虑先学习C. 大多数时候,我们学习语言的目的,不是为了成为一个语言专家,而 ...
- stm32的FSMC
之前用的stm32f103rbt6,它是100引脚以内的,不带FSMC.驱动液晶屏或者SRAM要自己写时序方面的程序,比较麻烦.后来换成stm32f103zet6,带有FSMC.不过在学习FSMC的时 ...
- 通过IIS发布站点和VS2012自带发布网站
vs2012通过IIS发布站点 http://jingyan.baidu.com/article/0964eca2d7beeb8285f536bd.html 用VS2012自带发布网站 http:// ...