zoukankan      html  css  js  c++  java
  • 会话与过滤器

    会话跟踪
      Cookie机制
       javax.sevlet.http.Cookie
       request.getCookie();
       response.addCookie();
       保存中文:
        Cookie cookie=new Cookie(URLEncoder.encode("姓名","UTF-8"),URLEncoder.encode("邓远奇","UTF-8"));
       保存二进制图片:
        File file=new File(image.path);
        byte [] binary=new bate[(int)file.lenth];
        InputStream in=this.getServletContext().getResourceAsStream(file.getName());
        in.read(binary);
        in.close;
        String content=BASE64Encoder.class.newInstance().encode(binary);
        Cookie cookie=new Cookie("file",content);
     
     过滤器Filter
      Filter:
       用户在servlet之外对request/response进行修改
       一个Filter必须实现javax.servlet.Filter接口,该接口有三个方法:
        public void init(FilterConfig config){};
        public void doFilter(Request request,Response response,FilterChain chain){};
         在doFilter()方法中一定要执行chain.doFilter(request,response,chain);
        public void destory();
       Filter配置:
        在web.xml
         <filter>
          <filter-name></filter-name>
          <filter-class></filter-class>
          <init-param></init-param>
         </filter>
         <filter-mapping>
          <filter-name></filter-name>
          <url-pattern></url-pattern>
          <dispathcher>REQUEST/RESPONSE </dispathcher>  //配置到达servlet的方式
         </filter-mapping>
       字符编码过滤器:
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
       登陆过滤器:
        session.getAttribute("username");  //从session中取出username判断是否为null

  • 相关阅读:
    linux shell获取用户输入
    yii2 笔记(1)
    yii2 数据库查询
    Yii2 环境配置生产环境和测试环境
    mysql 中find_in_set()和in()用法比较
    使用js提交form表单的两种方法
    base64编码的原理及实现
    浅析HTTP/2的多路复用
    Nginx 所使用的 epoll 模型是什么?
    git代码统计
  • 原文地址:https://www.cnblogs.com/dengyuanqi/p/6378335.html
Copyright © 2011-2022 走看看