在打开的页面中弹出一个对话框,可通过过滤器来控制页面输出的内容,进行实现在每个响应的页面中都弹出一个对话框的功能
在完成过滤任务时,将请求的对象返回到自定义的应答对象中,通过自定义应答对象对请求的数据进行编译,编译完成后通过自定义的方法返回响应数据,通过replace()方法向响应的数据中添加调用弹出对象框的代码:
replace()方法:将方法中的newChar替换指定字符串中出现的所有oldChar
public String replace(char oldChar,char newChar)
创建OutputStream.java文件继承ServletOutputStream类,替换父类的输出流
public class OutputStream extends ServletOutputStream {
ByteArrayOutputStream stream; //创建字节数组输出流
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener arg0) {
}
public OutputStream(ByteArrayOutputStream stream){ //构造方法初始化输出流
this.stream=stream;
}
@Override
public 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;
@Override
public void destroy() {
}
@Override
public 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);
}
@Override
public 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> </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>