//  浏览器提交的数据是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乱码以及解决的更多相关文章

  1. JS.中文乱码,Jsp\Servlet端的解决办法

    JS.中文乱码,Jsp\Servlet端的解决办法 2010-03-08 15:18:21|  分类: Extjs |  标签:encodeuricomponent  乱码  urldecoder   ...

  2. servlet 乱码解决方法

    一. servlet 发送的html 页面中文乱码 解决方法, 1.加入如下代码 response.setCharacterEncoding("UTF-8"); 2.在html页面 ...

  3. Servlet乱码解决

    后端收前端 1.post乱码 可以通过 request.setCharacterEncoding("utf-8");  这样在后端可以接收中文 2.get乱码(手动解决) 可以通过 ...

  4. Servlet中文乱码原因 解决 Get 和 Post 和客户端

    一.Get方式的中文乱码 1) 使用如下页面表单内容: <form action="http://127.0.0.1:8080/day07/params" method=&q ...

  5. servlet乱码问题总结

    在学习时servlet乱码问题还是挺严重的,总结一下有三种情况 1.新建HTML页面后浏览出现乱码 2.以post形式请求时出现乱码 3.以get形式请求时出现乱码 让我们一个一个来解决吧 1.新建H ...

  6. JAVA-----乱码的处理 乱码的解决方法总结

    为什么说乱码是程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!工作遇到各种各样的乱码的解决方法总结一下. 对于Java由于默认的编码方式是 ...

  7. JSP的学习(4)——中文乱码的解决

    本篇将以JSP页面中可能存在的中文乱码问题进行分析和解决. 中文乱码的问题一直是国人在编程过程中的一大头疼问题,这点上在JSP.Servlet或Tomcat上随处可见.比如我们在写一个Servlet时 ...

  8. SpringMVC中文乱码的解决办法

    中文乱码分类: (1)按照请求分类: GET请求乱码 POST请求乱码 (2)按照乱码位置分类 从前台传到后台的数据乱码(存储到数据库中的数据乱码) 从后台传到前台的数据乱码(显示在页面的数据乱码) ...

  9. Servlet乱码问题解决

    对于请求参数的编码处理基本上分为get和post两种情况. 1.POST index.html <!DOCTYPE html> <head> <meta http-equ ...

随机推荐

  1. ASP.NET MVC请求处理过程

  2. Java ----------- SQL语句总结(更新中。。。。。。)

    #对数据库的操作 *创建数据库 CREATE DATABASE database_name:database_name为创建的数据库的变量名称. #对表的操作

  3. Material Calendar View 学习记录(二)

    Material Calendar View 学习记录(二) github link: material-calendarview; 在学习记录一中简单翻译了该开源项目的README.md文档.接下来 ...

  4. LNMP优化

        LNMP优化 LNMP优化从系统安全,系统资源占用率,及web服务并发负载这三个方面体现,并   且主要体现在web服务并发负载这一方面.     1:首先进行linux优化加固  Linux ...

  5. JS控制文本框textarea输入字数限制的方法

    <html> <head runat="server"> <title></title> <script type=" ...

  6. hdu find the safest road

    算法:多源最短路(floyd) 题意:每条通路有一个安全系数,求始点到终点的最大的安全系数并输出,如果没有输出What a pity! c++超时啊 Problem Description XX星球有 ...

  7. 阿里云Centos7使用yum安装MySQL5.6的正确姿势

    阿里云Centos7使用yum安装MySQL5.6 阿里云Centos7使用yum安装MySQL5.6 前言:由于某些不可抗力,我要在自己的阿里云服务器上搭建hadoop+hive+mysql+tom ...

  8. WordPress插件制作教程(六): 插件函数之动作(Actions)函数

    这一篇为大家说一下WordPress插件函数吧,要制作插件,了解这些函数是非常有必要的 WordPress插件函数分为“动作”(Actions)和过滤器”(Filters),WordPress 使用这 ...

  9. Labeling Balls--poj3687

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12273   Accepted: 3516 D ...

  10. 使用ARM模板在Azure中国大规模部署DCOS集群

    容器技术是目前非常流行的技术,尤其是在以Docker作为容器引擎的推动下,让容器的轻量级,可移植,自包含,隔离性等的上了一个新的台阶,目前谈及Dev/Ops,CI/CD很少能够绕过Docker的. A ...