zoukankan      html  css  js  c++  java
  • HttpServletRequestWrapper类的使用

    HttpServletRequestWrapper类的使用
    servlet规范中中引入的filter是非常有用的,因为它引入了一个功能强大的拦截模式。

    filter是这样的一种java对象。它可以在request到达servlet之前拦截HttpServletRequest对象,也可以在服务方法转移控制后拦截HttpServletResponse对象。

    我们可以使用filter对象完成的任务有:检查用户的输入、以及压缩web内容。

    但是,当我们在使用filter的时候却会发现至少有一半的时间我们都想改变HttpServletRequest对象的参数。如:用filter在HttpServletRequest对象到达Servlet之前将用户输入的空格去掉。但是由于java.util.Map包装的HttpServletRequest对象的参数是不可改变的,那要怎么办呢?

    幸运的是,尽管我们不能改变对象本身,但是可以通过装饰模式来改变其状态。

    比如在上文中编写的内部类Request就是HttpServletRequest类的装饰类。

    该类继承的HttpServletRequestWrapper类是HttpServletRequest类的装饰类。

    这在jsp/servlet 中是非常有用的,web程序通过调用httpServletRequest对象的getParameter方法来处理表单,因此通过重写装饰类中的此方法就可以改变HttpServletRequest对象的状态。所以在上题的内部类Request中就重写了getParameter方法和getParameterValues方法。

    因此,想要改变在httpServletRequest中的参数,可以通过httpServletRequest的装饰类HttpServletRequestWrapper来实现,只需要在装饰类中按照需要重写其getParameter(getParameterValues)方法即可。

  • 相关阅读:
    twitter api的使用获取关注者的时间线
    使用CloudSight API进行图像识别的Python脚本
    发送请求工具—Advanced REST Client
    windows使用celery遇到的错误
    Pythonic
    celery学习之入门
    windows中安装redis
    pandas 读写sql数据库
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
    MySQL 存储引擎
  • 原文地址:https://www.cnblogs.com/xuhewei/p/11474389.html
Copyright © 2011-2022 走看看