servlet接受到的中文被编码成html实体是什么原因?
服务端:
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
String priceStr = req.getParameter("price");
double price = Double.parseDouble(priceStr);
String countStr = req.getParameter("count");
int count = Integer.parseInt(countStr);
String remarks = req.getParameter("remarks");
System.out.println("name=" + name);
System.out.println("price=" + price);
System.out.println("count=" + count);
System.out.println("remarks=" + remarks);
Fruit fruit = new Fruit();
fruit.setName(name);
fruit.setPrice(price);
fruit.setCount(count);
if (fruit.save()) {
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
前端:
<html><head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<div>
<form action="add" method="post">
name: <input type="text" name="name"><br>
price: <input type="text" name="price"><br>
count: <input type="text" name="count"><br>
remarks: <input type="text" name="remarks"><br>
<button type="submit">submit</button>
</form>
</div>
</body>
</html>
结果:
回答:
没配置字符编码过滤器吧?
<filter> <filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
以上是 servlet接受到的中文被编码成html实体是什么原因? 的全部内容, 来源链接: utcz.com/p/945285.html