博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javaweb 字符集
阅读量:6966 次
发布时间:2019-06-27

本文共 2899 字,大约阅读时间需要 9 分钟。

因为http协议是无状态的,所以设置了request和response对象,request为客户端请求,response为服务器响应。

request.setCharaterEnCoding()

 register.jsp

<%@ page contentType="text/html;Charset=UTF-8" pageEncoding="UTF-8"%>注册页面
添加用户信息
用 户:
密 码:
性 别:

 

 registerInfo.jsp

<%@ page contentType="text/html;Charset=UTF-8" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
用 户:
密 码:
性 别:
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

你可能感兴趣的文章
Bzoj1001 [BeiJing2006]狼抓兔子
查看>>
9.27 h5日记
查看>>
二分例题2
查看>>
Javascript单线程实现
查看>>
火电电厂相关业务知识
查看>>
Magento 模版路径
查看>>
Taking water into exams could boost grades 考试带瓶水可以提高成绩?
查看>>
一对多和多对一的关系,用mybatis写
查看>>
七 递归与二分法、匿名函数、内置函数
查看>>
hdu 1001
查看>>
JavaScript速记
查看>>
两栏布局,三栏布局,等高布局,流式布局
查看>>
CSS3弹性盒模型之box-orient & box-direction
查看>>
Codeforces Beta Round #77 (Div. 2 Only) A. Football【字符串/判断是否存在连续7个0或7个1】...
查看>>
洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]
查看>>
动态规划题库
查看>>
[30期] 第一个项目
查看>>
[31期] 第一个项目结束之际-->俺很高调但很真诚地感谢一个人-->涛爷
查看>>
回头再看第一次项目
查看>>
sql 50题
查看>>