星空网 > 软件开发 > Java

过虑器基础3—页面缓存和生成验证码

设置某些页面缓存或是不缓存

要想让所有浏览器不缓存页面:

    <%

        response.setHeader("expires","-1");

        response.setHeader("pragma","no-cache");

        response.setHeader("cache-control","no-cache");

    %>

分析:通过一个过虑器,对所有*.jsp过虑,设置三个头,全部不缓存。

第一步:实现过虑器接口

public void doFilter(ServletRequest request, ServletResponse response,

           FilterChain chain) throws IOException, ServletException {

       //转换

       HttpServletResponse resp = (HttpServletResponse) response;

       resp.setHeader("expires","-1");

        resp.setHeader("pragma","no-cache");

        resp.setHeader("cache-control","no-cache");

       //都放行

       chain.doFilter(request, response);

    }

第二步:配置过虑器,url-pattern=*.jsp

<!-- 配置控制缓存的filter -->

    <filter>

       <filter-name>cache</filter-name>

       <filter-class>cn.itcast.filter.CacheFilter</filter-class>

    </filter>

    <filter-mapping>

       <filter-name>cache</filter-name>

       <url-pattern>*.jsp</url-pattern>

    </filter-mapping>

 

第三步:设置对某些页面缓存N天

       HttpServletResponse resp = (HttpServletResponse) response;

       //计算2天以后的毫秒值

       //日历

       Calendar cl = Calendar.getInstance();

       //日历上加2天

       cl.add(Calendar.DATE, 2);

       long time = cl.getTimeInMillis();

       resp.setDateHeader("expires", time);[W2] 

       //resp.setHeader("expires",""+time);

//         resp.setHeader("pragma","no-cache");

//         resp.setHeader("cache-control","no-cache");

       //都放行

       chain.doFilter(request,resp);


生成验证码:

    public void doGet(HttpServletRequest req, HttpServletResponse resp)

           throws ServletException, IOException {

       //设置响应类型

       resp.setContentType("image/jpeg");

       int width=60;

       int height=30;

       BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

       Graphics g = img.getGraphics();

       g.setColor(Color.WHITE);

       g.fillRect(0,0, width, height);

       g.setFont(new Font("宋体", Font.BOLD,18));

       Random r = new Random();

       for(int i=0;i<4;i++){

           Color c = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));

           int code = r.nextInt(10);

           g.setColor(c);

           g.drawString(""+code,i*15,10+r.nextInt(20));

       }

       for(int i=0;i<10;i++){

           Color c = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));

           g.setColor(c);

           g.drawLine(r.nextInt(60),r.nextInt(30),r.nextInt(60),r.nextInt(30));

       }

       //图片生效

       g.dispose();

       //写到

       ImageIO.write(img, "JPEG",resp.getOutputStream());

    }



 



原标题:过虑器基础3—页面缓存和生成验证码

关键词:缓存

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊的销量像坐过山车?你知道是什么原因吗?:https://www.ikjzd.com/articles/91002
速卖通2019:箱包行业6大发展趋势:https://www.ikjzd.com/articles/91006
广告投放策略早知道:如何备战亚马逊Prime Day:https://www.ikjzd.com/articles/91013
亚马逊A9算法是怎样给lisitng打分的?熟记这6个要点!:https://www.ikjzd.com/articles/91017
细数亚马逊的这些露骨丑闻,难道你不想吐槽?:https://www.ikjzd.com/articles/91018
跨境电商怎么做?卖家做跨境电商的步骤和规划:https://www.ikjzd.com/articles/91022
在英国有分公司的中国公司:https://www.xlkjsw.com/tag/30029.html
英国成立:https://www.xlkjsw.com/tag/30031.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流