在服务器端用request.getParameter()读取参数时,很容易出现中文乱码现象,其中有很多种方法可以显示出中文,以下先列出一种方法:
源程序:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here </title>
</head>
<body>
<% String uname="张三";%>
<jsp:include page="receive-para.jsp">
<jsp:param value="<%=uname%>" name="username"/>
</jsp:include>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p>用户名: <%=request.getParameter("username")%></p>
</body>
</html>
此时会出现乱码现象:
之后我们在第一个页面中的<body>里加入一行代码:
<body>
<%request.setCharacterEncoding("utf-8");%>
此时显示的内容变为: