对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;
相关推荐
首先要知道这里的request为HttpServletRequest,如果我们打印request的话,发现打印出的类型为 RequestFacade(package org.apache.catalina.connector; public class RequestFacade implements HttpServletRequest )...
首先,说说JSP/Servlet中的几个编码...charset=UTF-8"、request.setCharacterEncoding("UTF- 8")和response.setCharacterEncoding("UTF-8"),其中前两个只能用于JSP中,而后两个可以用于JSP和 Servlet中。 。。。。。
// request.setCharacterEncoding("utf-8"); // //接收method属性的值 // String methodName = request.getParameter("method"); // // //根据method属性的值调用相应的方法 // if("login".equals(methodName)){ // ...
request.setCharacterEncoding("utf-8"); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); AdvertDao adverDao=new ...
一、有关中文乱码: mysql部分: url配置:jdbc:mysql://localhost:3306/XXX?useUnicode=true&... request.setCharacterEncoding("utf-8"); *在html或者jsp界面更改为UTF-8 ;charset=utf-8" %/>
request.setCharacterEncoding("utf-8"); int userId=Integer.parseInt(request.getParameter("userId")); String userName = request.getParameter("username"); String userPassword = request....
public class ActionServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-...
wml中文乱码及wml按钮设置。 在servlet里面的doget和dopost方法...request.setCharacterEncoding("UTF-8"); response.setContentType(Const.CONTENT_TYPE);//Const.CONTENT_TYPE为:text/vnd.wap.wml;charset=UTF-8
request.setCharacterEncoding("UTF-8"); doPost(request, response); } /** * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to ...
request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); ListUserInfo(request, response); out.flush(); out.close(); } private ...
xml version="1.0" encoding="UTF-8"?> 02.<web-app xmlns:xsi=...
response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); // out.print(" 上传的文件为:"+name+" "); // out.print("保存在服务器上 的地址为:"+path+"</font>"); } } }...
request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); ...
2、如果乱码是在servlet中出现的,则有两种方法:一种是在每个servlet中doget和doPost方法头部加上request.setCharacterEncoding(“UTF-8″);第二种最保险,一劳永逸,是专门写一个过滤器类,也称国际化,类名为...
request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); // 调用servlet层去数据库查找是否有相同用户名 并返回到页面中的其他记录 String client_id = request.getParameter("client_id...
而ajax传输数据的时候用的是utf-8 ,还有对ajax get方法时最好escape 或urlcode, 代码如下: <%@ page contenttype=”text/html;charset=gb2312%> 如果是用servlet就加 代码如下: response.setcontenttype(...
同时为了解决中文乱码问题,在后台servlet的doPost()或者doGet()方法体的前几行,写request.setCharacterEncoding("GBK");(因为该组件内部使用GBK编码,所以如果使用utf-8的话,反而从表单中会获取中文乱码)...
// You'll use UTF-8 for the XML encoding. serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"response.dtd"); // Set the doctype to the custom DTD. serializer.setOutputProperty(OutputKeys....
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...
request请求数据设置编码使用:request.setCharacterEncoding(“UTF-8”); response响应数据设置编码使用:response.setContentType(“text/html; charset=UTF-8”);点击按钮,发送请求,响应回来的数据还是显示”???...