request对象
客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。request对象具有请求域,即完成客户端的请求之前,该对象一直有效。
常用方法如下:
String getParameter(String name) | 返回name指定参数的参数值 |
String[] getParameterValues(String name) | 返回包含参数name的所有值的数组 |
void setAttribute(String,Object) | 存储此请求中的属性 |
Object getAttribute(String name) | 返回指定属性的属性值 |
String getContentType() | 得到请求体的MIME类型 |
String getProtocol() | 返回请求用的协议类型及版本号 |
String getServerName() | 返回接受请求的服务器主机名 |
int getServerPort() | 返回服务器接受此请求所用的端口号 |
String getCharacterEncoding() | 返回字符编码方式 |
void setCharacterEncoding() | 设置请求的字符编码方式 |
int getContentLength() | 返回请求体的长度(以字节数) |
String getRemoteAddr() | 返回发送此请求的客户端IP地址 |
String getRealPath(String path) | 返回一虚拟路径的真实路径 |
String request.getContextPath() | 返回上下文路径 |
中文乱码问题
1. 直接在jsp文件中添加语句 request.setCharacterEncoding("utf-8");
<%
request.setCharacterEncoding("utf-8");
%>
弊端:无法解决由URL传递进来的中文乱码问题。
2. 通过修改Tomcat文件夹下 conf- server.xml文件
server.xml文件:
添加 URIEncoding="utf-8" 属性即可解决中文乱码问题
ps: server.xml文档修改后重启Tomcat设置才能生效。