码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet第四篇【Request常用方法】

时间:2020-04-05 15:09:52      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:header   over   print   ons   第四篇   row   Servle   ted   cep   

防盗链

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String referer = req.getHeader("Referer");
        if(referer==null|| !"http://localhost:8080/a".equals(referer)){
            PrintWriter writer = resp.getWriter();
            writer.write("防盗链");
        }else{
            PrintWriter writer = resp.getWriter();
            writer.write("路飞在跑");
        }
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

请求转发

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write("a");
        req.getRequestDispatcher("/b").forward(req,resp);
    }

RequestDispatcher的include方法

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        req.getRequestDispatcher("/c").include(req, resp);//匹配的Servlet代码writer.write("c");
        writer.write("a");
        req.getRequestDispatcher("/d").include(req, resp);//匹配的Servlet代码writer.write("d");
    }

    //页面显示:cad

Servlet第四篇【Request常用方法】

标签:header   over   print   ons   第四篇   row   Servle   ted   cep   

原文地址:https://www.cnblogs.com/kwdlh/p/12637268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!