JSTL <fmt:requestEncoding> 标签作用与 request.setCharacterEncoding("charsetName") 相同,用于设置请求数据的字符编码。
JSP <fmt:requestEncoding> 标签语法如下:
<fmt:requestEncoding value="charsetName">
其中,value 用来指定字符编码。
下面为 <fmt:requestEncoding> 标签的简单实例。
login.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8a"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<title>城东书院(www.cdsy.xyz)</title>
</head>
<body>
<body>
<form action="index.jsp" method="post">
关键字:<input name="key" /> <br /> <br />
<input type="submit">
</form>
</body>
</html>
index.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<title>城东书院(www.cdsy.xyz)</title>
</head>
<body>
<body>
<fmt:requestEncoding value="UTF-8" />
<c:out value="${param.key}"></c:out>
</body>
</html>
运行结果如下: