HttpServletRequest对象代表客户端的请求
通过Request对象进行的常用操作
- 获取客户机信息
- 获取请求头信息
- 获取请求参数
- 利用请求域传递对象(request域对象)
- 重定向和转发的区别
获取客户机信息
获取请求头信息
- referer 网页来源
- user-agent 浏览器类型
- MSIE IE浏览器
- Firefox 火狐浏览器
- Chrome google浏览器
/**
* 获取客户机的内容 和请求头内容
* @author limaoquan
*
*/
public class RequestServlet1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取IP
String ip = request.getRemoteAddr();
String method = request.getMethod();
String path = request.getContextPath();
System.out.println("IP地址" + ip);
System.out.println("虚拟路径是 "+ path);
System.out.println("请求方式"+ method);
//获取请求头
String referer=request.getHeader("referer");//网页来源(防盗链)
String agent = request.getHeader("user-agent");//判断浏览器(文件下载)
//遍历所有请求头
Enumeration names = request.getHeaderNames();
while(names.hasMoreElements()){
String name = names.nextElement();
System.out.println(name + ":" + request.getHeader(name));
}
System.out.println("------------");
System.out.println("您使用浏览器:"+ request.getHeader("user-agent"));
//判断referer是否存在和有效
if(referer!=null&&referer.startsWith("http://localhost/day")){
//不属于盗链
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("机密信息");
}else{
//盗链
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("您的请求盗链");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
获取请求参数(重要)
Insert title here