在打开的页面中弹出一个对话框,可通过过滤器来控制页面输出的内容,进行实现在每个响应的页面中都弹出一个对话框的功能

在完成过滤任务时,将请求的对象返回到自定义的应答对象中,通过自定义应答对象对请求的数据进行编译,编译完成后通过自定义的方法返回响应数据,通过replace()方法向响应的数据中添加调用弹出对象框的代码:

replace()方法:将方法中的newChar替换指定字符串中出现的所有oldChar
public String replace(char oldChar,char newChar)


创建OutputStream.java文件继承ServletOutputStream类,替换父类的输出流

public class OutputStream extends ServletOutputStream {ByteArrayOutputStream stream; //创建字节数组输出流@Overridepublic boolean isReady() {return false;}@Overridepublic void setWriteListener(WriteListener arg0) {}public OutputStream(ByteArrayOutputStream stream){        //构造方法初始化输出流this.stream=stream;}@Overridepublic void write(int b) throws IOException {stream.write(b);        //使用此类的输出流替换父类的输出方法     }}

创建ResponseWrapper.java文件继承HttpServletResponseWrapper,使响应对象进行重新编译并返回响应的数据

public class ResponseWrapper extends HttpServletResponseWrapper {private OutputStream stream;        //声明一个输出流private ByteArrayOutputStream byteStream;      //声明字节数组输出流private PrintWriter pw;              //声明打印输出流public ResponseWrapper(HttpServletResponse response) {super(response);byteStream=new ByteArrayOutputStream();     //数据流初始化stream=new OutputStream(byteStream);pw=new PrintWriter(byteStream);}public ServletOutputStream getOutputStream()throws IOException{return stream;     //返回字节输出流并重写父类方法}public PrintWriter getWriter()throws IOException{return pw;            //返回打印输出流重写父类方法}public String getContent()throws UnsupportedEncodingException{return byteStream.toString();     //返回响应数据}}

创建过滤器的实现类OutFilter,在doFilter()方法中完成对过滤器的操作,并用getContent()获取到响应的数据,用replace()方法将弹出对话框的代码层加到response响应的数据中

public class OutFilter implements Filter {private boolean variable=true;        //如果variable为真,每次都生成HTML首页private FilterConfig filterConfig=null;@Overridepublic void destroy() {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {HttpServletResponse httpResp=(HttpServletResponse) response;ResponseWrapper responseWrapper=new ResponseWrapper(httpResp);chain.doFilter(request, responseWrapper);    //过滤器操作PrintWriter out=response.getWriter();      //创建输出流responseWrapper.getWriter().flush();         //获取输出流并强制刷新String str=responseWrapper.getContent();String stres="</head><script>window.open('message.html','','width='+300+',height='+180+',top='+'+window.screen.width-300+'+',left='+'+window.screen.height+180+');</script>";out.println(str.replace("</head>", stres));}public void log(String msg){filterConfig.getServletContext().log(msg);}@Overridepublic void init(FilterConfig arg0) throws ServletException {this.filterConfig=filterConfig;}}

创建CharacterEncodingFilter.java文件

public class CharacterEncodingFilter implements Filter{protected String encoding = null;protected FilterConfig filterConfig = null;public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;this.encoding = filterConfig.getInitParameter("encoding");}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {if (encoding != null) {request.setCharacterEncoding(encoding);response.setContentType("text/html; charset="+encoding);}chain.doFilter(request, response);}public void destroy() {this.encoding = null;this.filterConfig = null;}
}

index.jsp页

<%@page contentType="text/html; charset=gbk"%>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gbk"><title>通过过滤器控制页面输出内容</title><style type="text/css"><!--.STYLE1 {font-size: 18px;color: #311439;}div{position:absolute;left:481px;top:387px;}
body {margin-left: 0px;margin-top: 0px;margin-right: 00px;margin-bottom: 0px;
}
a:link {text-decoration: none;
}
a:visited {text-decoration: none;
}
a:hover {text-decoration: none;
}
a:active {text-decoration: none;
}--></style></head><body><div style="width:151px;height:80px;overflow:auto"><table width="123" height="74" ><tr><td height="70" class="STYLE1" align="center"><a href="indexsure.jsp">单击刷新</a></td></tr></table></div><table width="995" height="666" border="0" align="center" cellpadding="0" cellspacing="0" background="images/main.jpg"><tr><td>&nbsp;</td></tr></table></body>
</html>

弹出框message.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
sss
</body>
</html>

web.xml文件配置

<filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>com.cn.zj.Filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>GBK</param-value></init-param></filter><filter><filter-name>outFilter</filter-name><filter-class>com.cn.zj.Filter.OutFilter</filter-class></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping><filter-mapping><filter-name>outFilter</filter-name><url-pattern>/index.jsp</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping><filter-mapping><filter-name>outFilter</filter-name><url-pattern>/indexsure.jsp</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping><session-config><session-timeout>30</session-timeout></session-config><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><login-config><auth-method>BASIC</auth-method></login-config>

通过过滤器控制页面输出内容相关推荐

  1. JavaScript向页面输出内容的四种方法

    javascript可以通过不同的方式来输出数据 1.使用window.alert()输出警告框 <!DOCTYPE html> <html lang="en"& ...

  2. php获取页面输出内容,PHP CURL获取页面内容输出例子

    使用PHP curl获取页面内容或提交数据,有时候希望返回的内容作为变量储存,而不是直接输出.这个时候就必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true. 1.curl ...

  3. 使用fn函数控制页面显示内容

    在使用EL的时候,不可避免的遇到,截取字符串,判断字符串长度等情况.这里给出简单的通过fn函数操作字符串的deamon. 1.页面引入标签 <%@ taglib prefix="c&q ...

  4. ASP.NET 缓存技术(一)——启用页面输出缓存

    作者寄语:MSDN 是最好的老师,互联网是最智慧的生命体,分享是最重要的成长途径,技术的进步在于学习.实践和创新! 本系列所讲述的技术和展示的代码适用于 .NET Framework 4.0 和 II ...

  5. 在进行 ASP.NET 开发时,有时候需要对页面输出的最终 HTML 源代码进行控制

    在进行 ASP.NET 开发时,有时候需要对页面输出的最终 HTML 源代码进行控制,是页面的 render 方法中很容易实现这个功能.下面就是一个实现的方法,注释都在代码中. [c-sharp] v ...

  6. 在Module中使用自定义过滤器,来统一对站内所有请求响应的输出内容进行采集或更改。...

    因项目需要,对每一个访问网站的请求要做原始数据记录,其中要包括几个要素: 1.客户端的IP 2.客户端请求的页面路径 3.客户端发出的请求头 4.服务器返回的正文内容. 在代码设计前分析了一下,前三个 ...

  7. php输出内容到页面,php实时输出内容

    实时输出如果放在js中我们可以直接使用settimeout来守时输入很方便,但是如果在php中实现起来就不能这样了,下面我来给介绍利用 ob_flush() 和 flush()函数实现即时实时输出内容 ...

  8. Vue2.0使用嵌套路由实现页面内容切换/公用一级菜单控制页面内容切换

    vue2.0使用嵌套路由实现页面跳转切换/公用一级菜单控制页面内容切换/routes配置children demo需求分析: 图中菜单区域为header公用组件,点击相应的菜单选项跳转到对应的路由,在 ...

  9. 将文件中的内容在控制台上输出

    Q: 在C:盘根目录下创建纯文本文件 test.txt,并键入下列内容: 呦呦鹿鸣,食野之蒿. 今夕何夕,见此良人. 桃之夭夭,灼灼其华. 如月之恒,如日之升. 巧笑倩兮,美目盼兮. 琴瑟在御,莫不静 ...

最新文章

  1. Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)
  2. 2.Python中的reload函数以及not defined reload
  3. 十三、熵编码算法(4):H.264使用CAVLC解析宏块的残差数据
  4. 7号团队-团队任务3:每日例会(2018-12-05)
  5. cocos2dx游戏解决方案
  6. 如何制定项目工作计划
  7. boost::make_tuple用法的测试程序
  8. 浅谈智能语音交互,看一个Windows语音识别程序
  9. JAVAWeb项目 微型商城项目-------(五)用户登录实现
  10. sersync+rsync多实例网站数据同步
  11. linux 反汇编 静态库,如何反汇编.lib静态库?
  12. 汉诺塔c 语言程序代码,汉诺塔c语言程序代码.docx
  13. Vue的axios封装
  14. 【LeetCode - 317】离建筑物最近的距离
  15. 拉格朗日、牛顿、拟合的应用
  16. 重视“互联网+政务服务”改革工作 推进智慧城市建设
  17. 敏之澳分享拼多多的宝贝标题要怎么写?
  18. 一点、两点、三点透视投影的python3实现-计算机图形学
  19. iOS测试之接口测试总结
  20. 优达学城机器学习之--支持向量机(SVM)

热门文章

  1. 成功解决OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor
  2. matlab eps 字体用AI打开乱码的解决
  3. bzoj1452: [JSOI2009]Count
  4. Java当中的运算符
  5. getsockname的使用
  6. STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
  7. ESP8266编译脚本之四
  8. 大连理工大学 计算机复试分数线,2020大连理工大学考研复试分数线已公布
  9. 波卡链Substrate (7)Grandpa协议三“2阶段同步”
  10. 区块链BaaS云服务(36)欧盟EBSI 区块链