zoukankan      html  css  js  c++  java
  • 解决中文乱码(不可能解决不了)

    一.直接上代码

              1.1写一个GET和POST代码分别解决中文乱码问题

                       注意:我们需要了解一个知识点,Tomcat7和Tomcat8之间的不同点,Tomcat8在Get请求中不会产生中文乱码问题,而在Tomcat7的GET请求中会产生中文乱码, Tomcat7和Tomcat8的Post请求都会产生中文乱码,在Tomcat官方文档中提到了Tomcat7和Tomcat8在编码过程中有不同,tomcat7用得是ISO-8859-1编码方式,而Tomcat用的则是"UTF-8"编码格式

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>登录</title>
    </head>
    <body>
    --------------GET请求--------------<br>
    <form action="${pageContext.request.contextPath}/LoginServlet"  method="get">
          用户名:<input type="text" name="username"><br>
         密码:<input type="password" name="password"><br>
         <input type="submit" value="登录">
    </form>
    --------------POST请求--------------<br>
    <form action="${pageContext.request.contextPath}/LoginServlet"  method="post">
          用户名:<input type="text" name="username"><br>
         密码:<input type="password" name="password"><br>
         <input type="submit" value="登录">
    </form>
    </body>
    </html>

               1.2Servlet代码

    package web_servlet;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException {
            //response.setHeader("content-type","text/html;charset=utf-8");
            String vString=request.getParameterMap().get("username")[0];
            System.out.println("dopost里面的request"+request);
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            System.out.println(username);
            System.out.println(password);
            if("张三".equals(username)&"123".equals(password)){
                response.getWriter().write("登录成功");
                response.getWriter().write(username+password);
            }
            else{
                response.getWriter().write("登录失败");
            }
        }
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException {
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            System.out.println(username);
            System.out.println(password);
            if("张三".equals(username)&"123".equals(password)){
                response.getWriter().write("登录成功");
                response.getWriter().write(username+password);
            }
            else{
                response.getWriter().write("登录失败");
            }
        }
    }

           1.3过滤器的配置

    package web_Filter;
    
    import java.io.IOException;
    import java.util.Map;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import javax.servlet.http.HttpServletResponse;
    
    public class MyEncodingFilter  implements Filter{
        @Override
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
            System.out.println("过滤器的初始化");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            // TODO Auto-generated method stub
            //1.设置编码格式为UTF-8
            System.out.println("dofilter里面的request"+request);
            HttpServletResponse response2=(HttpServletResponse)response;
            response2.setHeader("content-type", "text/html;charset=utf-8");
            //request.setCharacterEncoding("UTF-8");
            //2.如果编码格式为UTF-8则放行
            
            myRequest myRequest=new myRequest((HttpServletRequest)request);
            chain.doFilter(myRequest, response);
        }
    
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
            System.out.println("过滤器的销毁");
        }
    }
    /**
     * Wrapper包装类,内部有个真实对象的引用
     * @author maskpd
     *
     */
    class myRequest extends HttpServletRequestWrapper{
        private HttpServletRequest request;
        public myRequest(HttpServletRequest request) {
            super(request);
            this.request=request;
        }
        @Override
        public String getParameter(String name) {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public Map<String, String[]> getParameterMap() {
            // TODO Auto-generated method stub
            return super.getParameterMap();
        }
        
    }

    1.4配置过滤器

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>2020-1-20webproject</display-name>
      <!-- 编码过滤器 -->
      <filter>
            <filter-name>MyEncodingFilter</filter-name>
            <filter-class>web_Filter.MyEncodingFilter</filter-class>  
      </filter>
      <filter-mapping>
            <filter-name>MyEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>

     二.OK了

  • 相关阅读:
    [PHP]解决PHP Call to undefined function ldap_connect()
    [Nginx] Nginx配置PHP应用传递PATH_INFO变量
    [PHP]解决PHP Fatal error: Call to undefined function mcrypt_get_iv_size()
    [CentOS] centos下安装最新稳定版nginx
    [CentOS]查看centos的发行版本情况
    [MySQL]创建用户并指定某一数据库权限
    [PHP] 安装memcached扩展
    [http]301和302的区别
    [javascript] 报错SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
    [GO项目]开源免费在线客服系统-真正免费开源-GOFLY0.3.5发布-极简强大Go语言开发WEB网页客服
  • 原文地址:https://www.cnblogs.com/whr-blogs/p/12221091.html
Copyright © 2011-2022 走看看