zoukankan      html  css  js  c++  java
  • 入门servlet:request获取请求头数据

     1 @WebServlet("/RequestDemo2")
     2 public class RequestDemo2 extends HttpServlet {
     3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     4 
     5     }
     6 
     7     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     8         //演示获取请求头数据
     9 
    10         //1.获取所有请求头名称
    11         Enumeration<String> headerNames = request.getHeaderNames();
    12         //2.遍历
    13         while(headerNames.hasMoreElements()){
    14             String name = headerNames.nextElement();
    15             //通过请求头的名称获取请求头的值
    16             String value = request.getHeader(name);
    17             System.out.println(name+"----"+value);
    18         }
    19     }
    20 }
     1 @WebServlet("/RequestDemo3")
     2 public class RequestDemo3 extends HttpServlet {
     3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     4 
     5     }
     6 
     7     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     8         //演示获取请求头数据:user-agent
     9         String agent = request.getHeader("user-agent");
    10         //判度agent的浏览器版本
    11         if(agent.contains("Chrome")){
    12             //谷歌
    13             System.out.println("谷歌。。。。");
    14         }else if (agent.contains("Firefox")){
    15             System.out.println("火狐。。。");
    16         }
    17 
    18 
    19     }
    20 }
    @WebServlet("/RequestDemo4")
    public class RequestDemo4 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //演示获取请求头数据:referer
            String referer = request.getHeader("referer");
            System.out.println(referer);
    
            //防盗链
            if(referer != null){
                if(referer.contains("/request")){
                    //正常访问
                   // System.out.println("正常访问");
                    //显示中文
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("正常访问");
                }else{
                    //盗链
                    //System.out.println("盗链");
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("存在盗链,不允许访问");
                }
            }
        }
    }
  • 相关阅读:
    微信推送给服务器的XML消息解析-springmvc 解析xml数据流
    request.getInputStream() 的两种解析方式
    微信的token验证
    springmvc 解析xml数据
    Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
    纯CSS实现图片
    Java线程池应用
    JavaScript 插件的书页翻转效果
    c语言中字符串函数的使用
    窗体显示类
  • 原文地址:https://www.cnblogs.com/flypig666/p/11480349.html
Copyright © 2011-2022 走看看