日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过过滤器控制页面输出内容

發布時間:2025/3/20 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过过滤器控制页面输出内容 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在打開的頁面中彈出一個對話框,可通過過濾器來控制頁面輸出的內容,進行實現在每個響應的頁面中都彈出一個對話框的功能

在完成過濾任務時,將請求的對象返回到自定義的應答對象中,通過自定義應答對象對請求的數據進行編譯,編譯完成后通過自定義的方法返回響應數據,通過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>

總結

以上是生活随笔為你收集整理的通过过滤器控制页面输出内容的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。