对Servlet 中 request.setEncoding("utf-8")的分析
来源:互联网 发布:小学生网络教育 编辑:程序博客网 时间:2024/05/18 03:54
对Servlet 中 request.setEncoding("utf-8")的分析
首先要知道这里的request为HttpServletRequest,如果我们打印request的话,发现打印出的类型为 RequestFacade(package org.apache.catalina.connector; public class RequestFacade implements HttpServletRequest ) ,这里的RequestFacade对象实现了HttpServletRequest接口。
通过源码有如下发现:
protected Request request=null;
(package org.apache.catalina.connector)
public RequestFacade(Request request) {
this.request = request;
}
request对象通过RequestFacade对象的构造方法来设值
request.setCharacterEncoding(env);
2、package org.apache.catalina.connector
public class Request implements HttpServletRequest
public void setCharacterEncoding(String enc)
throws UnsupportedEncodingException {
if (usingReader)
return;
// 确保编码格式可用
byte buffer[] = new byte[1];
buffer[0] = (byte) 'a';
String dummy = new String(buffer, enc);
// Save the validated encoding
coyoteRequest.setCharacterEncoding(enc);
}
在这里发现有个usingReader变量,默认值为false,
protected boolean usingReader = false;
在如下两个地方将usingReader设置为true
protected boolean usingInputStream = false;
public ServletInputStream getInputStream() throws IOException {
if (usingReader)
throw new IllegalStateException
(sm.getString("coyoteRequest.getInputStream.ise"));
usingInputStream = true;
if (inputStream == null) {
inputStream = new CoyoteInputStream(inputBuffer);
}
return inputStream;
}
public BufferedReader getReader() throws IOException {
if (usingInputStream)
throw new IllegalStateException
(sm.getString("coyoteRequest.getReader.ise"));
usingReader = true;
inputBuffer.checkConverter();
if (reader == null) {
reader = new CoyoteReader(inputBuffer);
}
return reader;
}
3、protected org.apache.coyote.Request coyoteRequest;
coyoteRequest.setCharacterEncoding(enc);
public final class Request
public void setCharacterEncoding(String enc) {
this.charEncoding = enc;
}
private String charEncoding = null;
在这里我们可以知道,request对象的setCharacterEncoding方法只是将charEncoding属性设置为enc
到现在为止request的编码设置已经完成了,但是究竟在哪使用呢,下面就来看看
request通过如下几个方法来取得请求的参数:
package org.apache.catalina.connector;
public String getParameter(String name)
public String[] getParameterValues(String name)
public Map getParameterMap()
public Enumeration getParameterNames()
下面来看一下它们的具体实现:
public String getParameter(String name) {
if (request == null) {
throw new IllegalStateException(
sm.getString("requestFacade.nullRequest"));
}
if (Globals.IS_SECURITY_ENABLED){
return (String)AccessController.doPrivileged(
new GetParameterPrivilegedAction(name));
} else {
return request.getParameter(name);
}
}
同样的道理 RequestFacade.getParameter(name)方法来调用Request.getParameter(name)
public String getParameter(String name) {
if (!parametersParsed)
parseParameters();
return coyoteRequest.getParameters().getParameter(name);
}
可以发现取参数的时候首先会将所有的参数解析出来
开始定义时 protected boolean parametersParsed = false;
- 对Servlet 中 request.setEncoding("utf-8")的分析
- Servlet机制中Request、Session、servletContext的区别分析
- Servlet机制中Request、Session、servletContext的区别分析
- Servlet机制中Request、Session、servletContext的区别分析
- request.setCharacterEncoding("UTF-8")的作用
- Servlet/jsp: 对request对象中attribute用法总结:
- Servlet中Request的getAttribute getParameter 区别
- Servlet中Request的getAttribute getParameter 区别
- servlet中request与response的用法
- request.setCharacterEncoding(“utf-8”)
- Python中对两种utf-8格式的理解
- Servlet中request请求
- servlet中传递 request
- JSP中pageEncoding、charset=UTF-8及request/response
- 页面编码为UTF-8的项目中servlet中文显示乱码的解决方法
- 8 servlet的入门request和response
- jsp中pageEncoding、charset=UTF -8"、(request/response).setCharacterEncoding("UTF-8")详
- jsp中pageEncoding、charset=UTF -8"、(request/response).setCharacterEncoding("UTF-8")详
- (转载)TOMCAT介绍
- Hibernate中Criteria的完整用法
- ubuntu10.04安装oracle10g
- Vista C盘空间越来越小完美解决方案
- AutoResetEvent 特点
- 对Servlet 中 request.setEncoding("utf-8")的分析
- 如何降低SQL Server 2005内存使用量与设置
- -stdcall详解
- 双雄夺冕
- .NET中的三种接口实现方式
- 埃及艳后
- NHibernate中DateTime空值的处理方法
- 解决软件开发效率基础理论
- ArcMap中设置字体符号Character Marker Symbol指定某字的unicode