最近是在写javaweb的课程实验,目前是在通过JavaBean来进行功能的实现, 进行数据传输的时候,会有出现相关的中文乱码问题。
分享一下我是怎么解决的吧(图中下半节的出生年月日 是我修改过的)
方法一:
输入的姓名:<%= new String(c.getName().getBytes("iso-8859-1"),"utf-8")%><br>--%>
通过<%= new String( .getBytes("iso-8859-1"),"utf-8")%><br>
来进行相对应的单行编码修改
方法二
通过设置page 来进行整页的乱码修改
<% request.setCharacterEncoding("UTF-8"); %>
可能会爆红,但是 你别管。用就行,因为我也爆红但是。。。。就是可以用
用这种方式 你当前页面的代码就正常书写就行,显示的时候不会出现中文乱码的问题。
注意:以上的方法一和方法二是不能共用的,否则 在共用的地方依旧还是会有相应的中文乱码问题!
上一下我对应的页面代码吧。
register.jsp 页面
<%--
Created by IntelliJ IDEA.
User: lan
Date: 2022/10/11
Time: 21:40
To change this template use File | Settings | File Templates.
--%>
<!-- 程序input.jsp -->
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<html>
<head> <title>注册页面</title> </head>
<body>
<h3> 注册页面</h3><br>
<form action="output.jsp" method="post">
姓名:<input name="name"><br><br>
性别:<input name="sex"><br><br>
出生年月:<input name="birth"><br><br>
民族:<input name="nation"><br><br>
个人介绍:<input name="introduction"><br><br>
<%-- 第二个加数:<input name="shuju2"><br><br>--%>
<input type=submit value="注册">
</form>
</body>
</html>
output.jsp 页面
<%--
Created by IntelliJ IDEA.
User: lan
Date: 2022/10/11
Time: 21:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% request.setCharacterEncoding("UTF-8"); %>
<html>
<head> <title>注册信息</title> </head>
<body>
<jsp:useBean id="c" class="bean.JavaBean" scope= "request"/>
<%--这个语句执行相应的Adder 类中的setShuju1 方法--%>
<%--<h3>先调用jsp:setProperty标签设置参数</h3>--%>
<%--<jsp:setProperty name="c" property="shuju1"/>--%>
<%--<jsp:setProperty name="c" property="shuju2"/>--%>
<jsp:setProperty name="c" property="*"/>
<h3>调用jsp:getProperty标签以及求和方法获取数据并显示:</h3>
姓名为:
<jsp:getProperty name="c" property="name"/><br>
<%--<jsp:getProperty name="c" property="shuju2"/>=--%>
<%--<%= c.perimeter()%><br><br>--%>
性别:
<jsp:getProperty name="c" property="sex"/><br>
<%--<%= c.area()%>--%>
民族:
<jsp:getProperty name="c" property="nation"/><br>
出生年月日:
<jsp:getProperty name="c" property="birth"/><br>
个人介绍:
<jsp:getProperty name="c" property="introduction"/><br>
<h3>调用使用类的方法获取数据并显示:</h3>
<%--输入的姓名:<%= new String(c.getName().getBytes("iso-8859-1"),"utf-8")%><br>--%>
输入的姓名:<%= c.getName()%><br>
输入的性别:<%= c.getSex()%><br>
输入的民族:<%= c.getNation()%><br>
输入的出生年月日:<%= c.getBirth()%><br>
输入的个人介绍: <%= c.getIntroduction()%>
<%--<h3>调用使用类的方法获取面积并显示:</h3>--%>
<%--<%= c.getShuju1()%>的面积= <%= c.area()%><br>--%>
</body>
</html>
javaBean 类
package bean;
public class JavaBean {
private String name;
private String sex;
private String birth;
private String nation;
private String introduction;
public JavaBean() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setBirth(String birth){
this.birth=birth;
}
public String getBirth(){
return birth;
}
public void setNation(String nation){
this.nation=nation;
}
public String getNation(){
return nation;
}
public void setIntroduction(String introduction){
this.introduction=introduction;
}
public String getIntroduction(){
return introduction;
}
}
又是一个秃头小技巧~