request功能:
1. 获取请求消息数据
1. 获取请求行数据
* GET /day14/demo1?name=zhangsan HTTP/1.1
* 方法:
1. 获取请求方式 :GET
* String getMethod()
2. ()获取虚拟目录:/day14
* String getContextPath()
3. 获取Servlet路径: /demo1
* String getServletPath()
4. 获取get方式请求参数:name=zhangsan
* String getQueryString()
5. ()获取请求URI:/day14/demo1
* String getRequestURI(): /day14/demo1
* StringBuffer getRequestURL() :http://localhost/day14/demo1
* URL:统一资源定位符 : http://localhost/day14/demo1 *
* URI:统一资源标识符 : /day14/demo1 *
6. 获取协议及版本:HTTP/1.1
* String getProtocol()
7. 获取客户机的IP地址:
* String getRemoteAddr()
2. 获取请求头数据
* 方法:
* (*)String getHeader(String name):通过请求头的名称获取请求头的值
* Enumeration getHeaderNames():获取所有的请求头名称
package ypy.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 方法:
* 1. 获取请求方式 :GET
* * String getMethod()
* 2. (*)获取虚拟目录:/day14
* * String getContextPath()
* 3. 获取Servlet路径: /demo1
* * String getServletPath()
* 4. 获取get方式请求参数:name=zhangsan
* * String getQueryString()
* 5. (*)获取请求URI:/day14/demo1
* * String getRequestURI(): /day14/demo1
* * StringBuffer getRequestURL() :http://localhost/day14/demo1
* * URL:统一资源定位符 : http://localhost/day14/demo1 *
* * URI:统一资源标识符 : /day14/demo1 *
* 6. 获取协议及版本:HTTP/1.1
* * String getProtocol()
* 7. 获取客户机的IP地址:
* * String getRemoteAddr()
*/
String method = req.getMethod();
System.out.println(method);
String contestPath = req.getContextPath();
System.out.println(contestPath);
String servletPath = req.getServletPath();
System.out.println(servletPath);
String getQueryString = req.getQueryString();
System.out.println(getQueryString);
String getRequestURI = req.getRequestURI();
System.out.println(getRequestURI);
StringBuffer getRequestURL = req.getRequestURL();
System.out.println(getRequestURL);
String protocol = req.getProtocol();
System.out.println(protocol);
String remote = req.getRemoteAddr();
System.out.println(remote);
String agent = req.getHeader("user-agent");
System.out.println(agent);
if(agent.contains("Chrome")){
System.out.println("Google");
}else{
System.out.println("其他浏览器");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
运行结果: