Request

HttpServletRequest

  我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。
  service方法中的request的类型是ServletRequest,而doGet/doPost方法的request的类型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口,功能和方法更加强大

request获得请求行

  获得客户端的请求方式:String getMethod()
  获得请求的资源:
   String getRequestURI()
   StringBuffer getRequestURL()
   String getContextPath() —web应用的名称
   String getQueryString() —- get提交url地址后的参数字符串
   username=zhangsan&password=123

  注意:request获得客户机(客户端)的一些信息
  request.getRemoteAddr() — 获得访问的客户端IP地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.rexyan.request;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求的方法
System.out.println(request.getMethod());
//获取URL【http://localhost:8080/Request/requestservlet】
System.out.println(request.getRequestURL());
//获取URI【/Request/requestservlet】
System.out.println(request.getRequestURI());
//获取Web应用的名称
System.out.println(request.getContextPath());
//获取get方式请求的参数
System.out.println(request.getQueryString());
//获取客户机的IP地址
System.err.println(request.getRemoteAddr());

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

request获得请求头

  long getDateHeader(String name)
  String getHeader(String name)
  Enumeration getHeaderNames()
  Enumeration getHeaders(String name)
  int getIntHeader(String name)
  referer头的作用:执行该此访问的的来源做防盗链

request获得请求体

  请求体中的内容是通过post提交的请求参数,格式是:
   username=zhangsan&password=123&hobby=football&hobby=basketball
   key value
   username [zhangsan]
   password [123]
   hobby [football,basketball]

  以上面参数为例,通过一下方法获得请求参数:
   String getParameter(String name)
   String[] getParameterValues(String name)
   Enumeration getParameterNames()
   Map<String,String[]> getParameterMap()
  注意:get请求方式的请求参数 上述的方法一样可以获得

  解决post提交方式的乱码:
   request.setCharacterEncoding(“UTF-8”);
  解决get提交的方式的乱码:
   parameter = new String(parameter.getbytes(“iso8859-1”),”utf-8”);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.rexyan.request;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.net.www.content.audio.x_aiff;

public class RequestServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置GET方式提交的数据编码【原理是先将传来的数据进行ISO编码,后使用UTF-8解码】
String username = request.getParameter("name");
username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
//设置POST方式提交的数据编码
request.setCharacterEncoding("utf-8");
//获取所有的name
Enumeration<String> parameterNames = request.getParameterNames();
//遍历name
while (parameterNames.hasMoreElements()) {
String string = (String) parameterNames.nextElement();
//根据name获取值
System.out.println(request.getParameter(string));
}
//--------------------------------------------------------
//用于获取只有一个名称,但是多个值,例如:checkbox
request.getParameterValues("name");
//根据name单个获取值
request.getParameter("username");
request.getParameter("password");
//将获取的值组装成一个Map
Map<String, String[]> parameterMap = request.getParameterMap();
for (Entry<String, String[]> entry: parameterMap.entrySet()) {
System.out.println(entry.getKey());
for(String value:entry.getValue()){
System.out.println(value);
}
}

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

request域

  目前学到的域:ServLetContext、Request

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.rexyan.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//往域对象中设置一个名为name值为zhangsan的值
request.setAttribute("name", "zhangsan");
//获取
request.getAttribute("name");
//删除
request.removeAttribute("name");
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

request请求转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.rexyan.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

ServletContext域与Request域比较

  注意:ServletContext域与Request域的生命周期比较?
  ServletContext:
   创建:服务器启动
   销毁:服务器关闭
   域的作用范围:整个web应用
  request:
   创建:访问时创建request
   销毁:响应结束request销毁
   域的作用范围:一次请求中

  注意:转发与重定向的区别?
   1)重定向两次请求,转发一次请求
   2)重定向地址栏的地址变化,转发地址不变
   3)重新定向可以访问外部网站 转发只能访问内部资源
   4)转发的性能要优于重定向

  注意:客户端地址与服务器端地址的写法?
   客户端地址:
    是客户端去访问服务器的地址,服务器外部的地址,特点:写上web应用名 称,直接输入地址:
    例如:重定向
     response.sendRedirect(“/WEB15/test.jsp”);

   服务器端地址:
    服务器内部资源的跳转的地址,特点:不需要写web应用的名称
    例如:转发
     request.getRequestDispatcher(“/index.jsp”).forward(request, response);

总结

  总结:
   request获得行的内容
   request.getMethod()
   request.getRequestURI()
   request.getRequestURL()
   request.getContextPath()
   request.getRemoteAddr()
   
   request获得头的内容
   request.getHeader(name)

   request获得体(请求参数)
   String request.getParameter(name)
   Map<String,String[]> request.getParameterMap();
   String[] request.getParameterValues(name);
 注意:客户端发送的参数 到服务器端都是字符串

 获得中文乱码的解决:
  post:
   request.setCharacterEncoding(“UTF-8”);
  get:
   parameter = new String(parameter.getBytes(“iso8859-1”),”UTF-8”);

  request转发和域
   request.getRequestDispatcher(转发的地址).forward(req,resp);
   request.setAttribute(name,value)
   request.getAttribute(name)