-
设置请求字符编码:
确保在处理表单数据之前设置请求的字符编码。你已经有request.setCharacterEncoding("UTF-8");
,确保它在所有代码之前执行 -
表单编码:
确保你的表单设置了accept-charset="UTF-8"
属性:<form action="displayUser.jsp" method="post" accept-charset="UTF-8"> 姓名: <input type="text" name="name"><br> 年龄: <input type="text" name="age"><br> 性别: <input type="text" name="sex"><br> <input type="submit" value="Submit"> </form>
-
页面和响应编码:
确保你的 JSP 页面设置了正确的编码,并且在返回响应时也设置了编码:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ page import="java.io.UnsupportedEncodingException" %> <% request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); %>
-
JavaBean设置:
确保你的 JavaBean 类正确处理字符串的编码。如果使用的是request.getParameter()
方法,确保在调用之前设置编码:public class User { private String name; private int age; private String sex; // Getter and Setter methods public String getName() { return name; } public void setName(String name) { try { // Tomcat的默认编码是 SO-8859-1 编码存储的 //name.getBytes("ISO-8859-1") 将字符串 name 按照 ISO-8859-1 编码转换成字节数组 //"UTF-8"将第一步得到的字节数组重新解码为字符串,这次使用 UTF-8 编码进行解码。 this.name = new String(name.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { try { this.sex = new String(sex.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
具体步骤:
-
修改表单:
<form action="displayUser.jsp" method="post" accept-charset="UTF-8"> 姓名: <input type="text" name="name"><br> 年龄: <input type="text" name="age"><br> 性别: <input type="text" name="sex"><br> <input type="submit" value="Submit"> </form>
-
修改 JSP 页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%--实例化一个名为 "user" 的 JavaBean 对象--%> <jsp:useBean id="user" class="yulin.zhu.User" scope="request" /> <%--将所有的请求参数都自动设置到 JavaBean 对象的对应属性中--%> <jsp:setProperty property="*" name="user" /> <% request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); %> <html> <head> <title>Display User Information</title> </head> <body> <h2>用户信息</h2> <%--通过<jsp:getProperty>标签获取并显示user对象的属性值--%> 姓名: <jsp:getProperty name="user" property="name" /><br> 年龄: <jsp:getProperty name="user" property="age" /><br> 性别: <jsp:getProperty name="user" property="sex" /><br> </body> </html>
-
修改 JavaBean:
package yulin.zhu; public class User { private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { try { this.name = new String(name.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { try { this.sex = new String(sex.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }