protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求方式:GET
String method = request.getMethod();
System.out.println(method);
// (*)获取虚拟目录:/JavaWeb001
String contextPath = request.getContextPath();
System.out.println(contextPath);
// 获取Servlet路径
String servletPath = request.getServletPath();
System.out.println(servletPath);
// 获取get方式请求参数:name=zhangsan
String queryString = request.getQueryString();
System.out.println(queryString);
// (*)获取请求的URI:/JavaWeb001/demo1
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
// 获取协议及版本
String protocol = request.getProtocol();
System.out.println(protocol);
// 获取客户机的IP
String remoteAddress = request.getRemoteUser();
System.out.println(remoteAddress);
//1、获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
//2、遍历
while (headerNames.hasMoreElements()){
String name = headerNames.nextElement();
String value = request.getHeader(name);
System.out.println(name+"-----"+value);
}
//获取请求头数据:user-agent
String agent = request.getHeader("user-agent");
// 判断agent浏览器版本
if (agent.contains("Firefox")){
System.out.println("火狐。。。。");
}else{
System.out.println("by~");
}
//演示请求头数据:referer
String referer = request.getHeader("referer");
System.out.println(referer);
if (referer!=null){
if (referer.contains("/day")){
//正常访问
System.out.println("播放电影、、、");
}else{
System.out.println("盗版");
}
}
}
Request获取请求头数据
最新推荐文章于 2024-09-18 06:12:02 发布