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

<%@ 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>
View Code

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()

 

转载于:https://www.cnblogs.com/lyd-123qw2008/p/5968153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值