美文网首页
JavaBean解决中文乱码

JavaBean解决中文乱码

作者: Jianbaozi | 来源:发表于2020-11-13 14:45 被阅读0次

Jsp页面编码设置:
在JSP内部定义的字符串(直接在JSP中定义,而不是从浏览器提交的数据)出现乱码时该参数设置和pageEncoding不一致引起

1.png

News

package com.baozi;

public class News {
    private String title;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    private String content;
}

CharactorEncoding

package com.baozi;
import java.io.UnsupportedEncodingException;
public class CharactorEncoding {
    public CharactorEncoding(){}
    public String toString(String str){
        String text="";
        if(str!=null&&!"".equals(str)){
            try{
                text=new String(str.getBytes("ISO8859-1"),"GB18030");
            }catch(UnsupportedEncodingException e){
            e.printStackTrace();
            }
        }
        return text;
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>新闻发布系统</title>
</head>
<body>
    <form action="release.jsp" method="post">
        <table align="center" width="450" height="260" border="1">
            <tr>
                <td align="center" colspan="2" height="40" >
                    <b>新闻发布</b>
                </td>
            </tr>
            <tr>
                <td align="right">标 题:</td>
                <td>
                    <input type="text" name="title" size="30">
                </td>
            </tr>
            <tr>
                <td align="right">内 容:</td>
                <td>
                    <textarea name="content" rows="8" cols="40"></textarea>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <input type="submit" value="发 布">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

release.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>发布结果</title>
<style type="text/css">
    #container{
        width: 450px;
        border: solid 1px;
        padding: 20px;
    }
    #title{
        font-size: 16px;
        font-weight: bold;
        color: #3399FF;
    }
    #content{
        font-size: 12px;
        text-align: left;
    }
</style>
</head>
<body>
    <jsp:useBean id="news" class="com.baozi.News"></jsp:useBean>
    <jsp:useBean id="encoding" class="com.baozi.CharactorEncoding"></jsp:useBean>
    <jsp:setProperty property="*" name="news"/>
    <div align="center">
        <div id="container">
            <div id="title">
                <%= encoding.toString(news.getTitle())%>
            </div>
            <div id="title">
                <%= news.getTitle()%>  
            </div>
            <hr>
            <div id="content">
                <%= encoding.toString(news.getContent())%>
            </div>
        </div>
    </div>
</body>
</html>
20201113144737.jpg

相关文章

网友评论

      本文标题:JavaBean解决中文乱码

      本文链接:https://www.haomeiwen.com/subject/fsgvbktx.html