javaweb 字符集
因为http协议是无状态的,所以设置了request和response对象,request为客户端请求,response为服务器响应。
request.setCharaterEnCoding()
register.jsp
<%@ page contentType="text/html;Charset=UTF-8" pageEncoding="UTF-8"%> <HTML><head><title>注册页面</title></head> <BODY ><Font size=3> <FORM action="registerInfo.jsp" method="post" name=form> <table align="center" width="400" height="200" border="1"> <tr> <td align="center"colspan="2" height="40"> <b>添加用户信息</b> </td> </tr> <tr> <td align="right">用 户:</td> <td> <input type="text"name="name"> </td> </tr> <tr> <td align="right">密 码:</td> <td> <input type="password" name="password"> </td> </tr> <tr> <td align="right">性 别:</td> <td> <input type="text" name="sex"> </td> </tr> <tr> <td align="center" colspan="2"> <input type="submit" value="添 加"> </td> </tr> </table> </FORM> </FONT></BODY></HTML>
registerInfo.jsp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ page contentType="text/html;Charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="person" class="com.lyq.bean.Person" scope="page">
<jsp:setProperty name="person" property="*"/>
</jsp:useBean>
<table align="center" width="400">
<tr>
<td align="right">用 户:</td>
<td>
<jsp:getProperty property="name" name="person"/>
</td>
</tr>
<tr>
<td align="right">密 码:</td>
<td>
<jsp:getProperty property="password" name="person"/>
</td>
</tr>
<tr>
<td align="right">性 别:</td>
<td>
<jsp:getProperty property="sex" name="person"/>
</td>
</tr>
</table>
</body>
</html>
javaBean表单传参数时发现乱码,再获取数据前加上<%request.setCharacterEncoding("utf-8"); %>,乱码解决。
后来尝试<%=request.getCharacterEncoding()%>,发现网页打印null值。查api文档得知
getCharacterEncoding
java.lang.String getCharacterEncoding()
Returns the name of the character encoding used in the body of this request. This method returns null if the request does not specify a character encoding
Returns:
a String containing the name of the character encoding, or null if the request does not specify a character encoding
因为我一开始没指定character encoding,所以返回了null值。我这里使用的是myeclipse 里的tomcat默认配置,应不是utf-8.所以乱码。
使用getWriter()前设置字符集编码
setContentType
void setContentType(java.lang.String type)
Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response's character encoding is only set from the given content type if this method is called before getWriter is called.
This method may be called repeatedly to change content type and character encoding. This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.
Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.
Parameters:
type - a String specifying the MIME type of the content
See Also:
setLocale(java.util.Locale), setCharacterEncoding(java.lang.String), getOutputStream(), getWriter()