标签:
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中得所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户端这些信息。
//request的常用方法
public class Demo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//getRequestURI返回请求行的资源名部分
System.out.println(request.getRequestURI()); //父亲
//getRequestURL返回客户端发出请求时的完整URL
System.out.println(request.getRequestURL()); //仔
//getQueryString返回请求行的参数部分
System.out.println(request.getQueryString());
//getRemoteAddr返回发出请求的客户机完整主机名
System.out.println(request.getRemoteAddr());
//getRemotePort返回客户机所使用的网络端口号
System.out.println(request.getRemoteHost());
//getLocalAddr返回WEB服务器的ip地址
System.out.println(request.getRemotePort());
//getLocalName返回web服务器的主机名
System.out.println(request.getLocalName());
//getMethod得到客户机请求方式
System.out.println(request.getMethod());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
获取请求头和请求数据
public class Demo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test2(request);
}
//获取请求的数据,一般是表单提交过来的数据,一般先检查在使用
private void test2(HttpServletRequest request) {
//方式1
String value = request.getParameter("username");
if(value != null && value.trim().equals("")){
System.out.println(value);
}
//方式2
Enumeration e = request.getParameterNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
value = request.getParameter(name);
System.out.println(name + "=" + value);
}
//方式3
String[] values = request.getParameterValues("username");
for(int i = 0; values != null && i < values.length; i ++){
System.out.println(values);
}
//方式4--要导入commons-beanutils-1.9.2.jar和commons-logging-1.2.jar
Map map = request.getParameterMap();
User user = new User();
try{
BeanUtils.populate(user, map); //用map集合数据填充bean
//BeanUtils.copyProperties(user,formbean); bean的拷贝
}catch(Exception e1){
e1.printStackTrace();
}
}
//获取头相关的方法
private void test1(HttpServletRequest request) {
String headValue = request.getHeader("Accept-Encoding");
System.out.println(headValue);
Enumeration e = request.getHeaders("Accept-Encoding");
while(e.hasMoreElements()){
String value = (String) e.nextElement();
System.out.println(value);
}
e = request.getHeaderNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
String value = request.getHeader(name);
System.out.println(name + "=" + value);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
解决乱码:post和get
//解决get提交的乱码问题(表单,超链接)
private void test2(HttpServletRequest request)
throws UnsupportedEncodingException {
String username = request.getParameter("username");
username = new String(username.getBytes("iso-8859-1"), "utf-8");
System.out.println(username);
}
//解决post提交的乱码问题
private void test1(HttpServletRequest request)
throws UnsupportedEncodingException {
request.setCharacterEncoding("utf-8"); //
String username = request.getParameter("username");
System.out.println(username);
}
request转发:
//请求转发,以及使用request域对象吧数据带给转发资源
public class Demo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "dddddd";
//每一个servlet请求都有自己的request容器,都能往容器里面写入数据,拿出的也是自己的数据,
request.setAttribute("data", data);
//request可以实现抓换
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
message.jsp,获取数据:
<body>
${data}
<%
String data = (String)request.getAttribute("data");
out.write(data);
%>
</body>
request转发的小细节:
forword方法用于将请求转发到RequestDispatcher对象封装的资源。
如果在调用forword方法之前,在Servlet程序中写入的部分内容已经被真正的传送到了客户端,forword方法将抛出java.lang.IllegalStateException异常
/IllegalStateException异常
private void test1(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
String data = "dddddd";
PrintWriter write = response.getWriter();
write.write(data);
write.close();
//会出现java.lang.IllegalStateException异常
request.getRequestDispatcher("message.jsp").forward(request, response);
}
如果在调用forword方法之前向Servlet引擎的缓冲区(response)中写入了内容,只要写到了缓冲区中的内容还没被真正输出到客户端,forword方法将可以被正常执行,原来写入到缓冲区中的内容将被清空,已经写入到HttpServletReponse对象中的响应头字段信息保持有效。
private void test2(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
String data = "dddddd";
response.getWriter().write(data);//数据写给浏览器,写到了response,但是没有写到客户端.在跳转之前,把response里面的数据清空了。
request.getRequestDispatcher("index.jsp").forward(request, response);
}
请求转发:客户端只发一次请求,而服务器端有多个资源使用/客户端浏览器地址没有发生变换
包含页面:request.getRequestDispatcher("/public/head.jsp").include(request, response);
标签:
原文地址:http://www.cnblogs.com/lcpholdon/p/4340561.html