zoukankan      html  css  js  c++  java
  • Java/Java Web中乱码解决汇总

    在开发Java/Java Web Application过程中,往往会出现乱码问题,而且有的时候真会弄得人很烦,浪费太多的时间。

    记得之前看过一篇帖子,详细解释了Encoding/Decoding过程,不过时间久远已经淡忘。。。

    其实针对这种乱码问题,记录问题+查阅解决方案是比较好的解决办法。

    问题1:JSP页面中的EL表达式输出出现乱码,如

    由book.jsp页面通过href转向modifyBook.jsp页面

    book.jsp

    <c:forEach var="book" items="${allBooks}">
        <tr>
            <td>${book.bookId}</td>
            <td>${book.bookName}</td>
            <td>${book.author}</td>
            <td>${book.price}</td>
            <td>${book.publisher}</td>
            <td><a href="deleteBook?bookid=${book.bookId}">删除</a></td>
            <td><a href="modifyBook?bookid=${book.bookId}&bookname=${book.bookName}&author=${book.author}&price=${book.price}&publisher=${book.publisher}">修改</a></td>
        </tr>
    </c:forEach>

    modifyBook.jsp

    <form action="${pageContext.request.contextPath}/modifyBook" method="post">
        <table>
            <tr>
                <td><input type="text" name="bookid" value="${param.bookid}" readonly/></td>
                <td><input type="text" name="book_name" value="${param.bookname}"/></td>
                <td><input type="text" name="author" value="${param.author}"/></td>
                <td><input type="text" name="price" value="${param.price}"/></td>
                <td><input type="text" name="publisher" value="${param.publisher}"/></td>
            </tr>
        </table>
        <br/>
        <input type="submit" name="提交" value="Submit"/>
    </form>

    EL表达式${param.publisher}的输出乱码。

    解决方案:

    1. 首先检查各个JSP页面的编码方式,确认均为contentType="text/html;charset=UTF-8";

    2. 检查跳转后的url地址如下:http://localhost:8080/basicmvc/modifyBook?bookid=00003&bookname=Java%20EE&author=wang&price=30.0&publisher=人民邮电,不是乱码。

    3. 由2说明${param.publisher}的源没有问题,就是输出编码的问题。折腾半天,想起tomcat的编码配置URIEncoding="UTF-8"。解决!

    问题2接上一个问题,中文数据展示没有问题了。但紧接着更新数据库后,数据库中出现中文乱码??。

    既然数据源没有问题,那就是数据库这边的问题了,但是数据库创建和表的创建都是UTF-8的方式。再查看Java代码,应该是PreparedStatement使用setString出现中文乱码。查询后,得知数据库连接中需要设置编码方式,恍然大悟(之前解决过类似问题,只是给忘了。。。),如下:

    jdbc:mysql://localhost:3306/crud?useUnicode=true&characterEncoding=UTF-8

    问题3:在另一环境中,解决了问题1和问题2,但还是出现了乱码问题。

    唯一区别就是Filter类的使用, Filter类似于Servlet,在web.xml中配置。

    解决方案:(代码胜千言)

    EncodingFilter:

    public class EncodingFilter implements Filter {
    
        //配置中默认的字符编码
        protected String encoding = null;
        protected FilterConfig filterConfig;
        //当没有指定默认编码时是否允许跳过过滤
        protected boolean ignore = true;
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            this.filterConfig = filterConfig;
            this.encoding = filterConfig.getInitParameter("encoding");
            System.out.println(this.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;
            }
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest httpServletRequest = (HttpServletRequest)request;
            HttpServletResponse httpServletResponse=(HttpServletResponse)response;
            //Conditionally select and set the character encoding to be used
            if(ignore || httpServletRequest.getCharacterEncoding() == null){
                String coding = selectEncoding(httpServletRequest);
                if(coding != null){
                    httpServletRequest.setCharacterEncoding(coding);
                    httpServletResponse.setCharacterEncoding(coding);
                }
            }
            //将控制器传向下一个filter
            chain.doFilter(httpServletRequest, httpServletResponse);
        }
    
        @Override
        public void destroy() {
            this.encoding = null;
            this.filterConfig = null;
        }
    
        protected String selectEncoding(ServletRequest request) {
            return (this.encoding);
        }
    }

    web.xml

    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>com.chris.web.filter.EncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

     问题3:待续

    清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
  • 相关阅读:
    多尺度双边滤波及基于小波变换的非线性扩散
    yum安装CentOS7+nginx+php7.3+mysql5.7
    python学习之特殊魔法__getattr__,__getattribute__
    python学习之特殊魔法__get__,__set__,__delete__
    python学习之装饰器
    python学习之私有属性
    python学习之包装与授权
    python学习之生成器(generator)
    python学习之运用特殊方法,定制类
    python学习之创建迭代器对象
  • 原文地址:https://www.cnblogs.com/hello-yz/p/5497326.html
Copyright © 2011-2022 走看看