Use filter to intercept the contents of servlet and JSP pages, filter and output __JS

Source: Internet
Author: User
Tags throwable

Idea: Use filter to intercept servlet or JSP page content, such as content substitution, and then show users the modified page
This can happen if you use normal filter to intercept page content

As you can see from the entry, the output does not go through the filter.

As you can see from the entry, the output does not go through the filter.

Since the use of normal filter can not achieve the purpose, then a different way, before the servlet or JSP received response, the response use a "false response" replacement, so that the servlet or JSP content output to this " Fake response ", and then the output of the processing, and finally the whole through the real response output to the client
This is illustrated below:

To implement the steps in the previous diagram:
1. Create a filter--to use the "fake response" to replace the actual response
2. Create a "fake response"--Inherit Httpservletresponsewrapper class
3. Replace content and actual output in filter

The code for the main class is as follows:

Filter

public class Contentchangefilter implements filter{public

	void Destroy () {
		
	} public

	void Dofilter ( ServletRequest request, Servletresponse response, Filterchain FC) throws IOException, servletexception {
		Coverresponse cr = new Coverresponse ((httpservletresponse) response);
		Fc.dofilter (Request, CR);
		Process replacement
		String content = Cr.getcontent ();
		Content = Content.replace ("Test", "actual");
		Response.getwriter (). print (content);

	public void init (Filterconfig arg0) throws servletexception {
		
	}
}


Fake Resonse:

public class Coverresponse extends httpservletresponsewrapper{private myprintwriter tmpwriter;

	Private Bytearrayoutputstream output;
		Public Coverresponse (HttpServletResponse response) {super (response);    
		Output = new Bytearrayoutputstream ();
	Tmpwriter = new Myprintwriter (output);    
		 public void Finalize () throws Throwable {super.finalize ();    
		 Output.close ();    
	Tmpwriter.close ();   Public String getcontent () {try {tmpwriter.flush ();    
		    Refreshes the buffer of the stream, detailing the Java.io.Writer.flush () String s = Tmpwriter.getbytearrayoutputstream (). toString ("UTF-8");    
		This can be done on an as-needed basis for the output stream and the reset operation of the writer, such as Tmpwriter.getbytearrayoutputstream (). Reset () return s;    
		catch (Unsupportedencodingexception e) {return "unsupportedencoding"; //Overwrite Getwriter () method, using our own defined writer public PrintWriter getwriter () throws IOException {return tmp    
	Writer; public void Close () throws IOException {    
		Tmpwriter.close (); ///Custom PrintWriter, in order to write the response stream to its own specified input stream//rather than the default Servletoutputstream private static class Myprintwriter ext   Ends PrintWriter {bytearrayoutputstream myoutput;    
			This is the object that holds the response input stream Myprintwriter (Bytearrayoutputstream output) {super (output);    
		Myoutput = output;    
		Public Bytearrayoutputstream Getbytearrayoutputstream () {return myoutput; }    
	} 
}


The function is to replace test in the output page with actual.

The test found that if you add a cookie to the "fake response", you can put the cookie on the client normally. This is probably one of the reasons why creating "fake response" requires a reference to the original response.

Modify 2012-09-20

After actual use, found that the use of character streams as the destination of PrintWriter will cause garbled situation, in view of repeated coding more difficult, so use simpler StringWriter as the output destination.

Package com.aspire.pams.tag.sichuan.tools;
Import java.io.IOException;
Import Java.io.PrintWriter;

Import Java.io.StringWriter;
Import Javax.servlet.http.HttpServletResponse;

Import Javax.servlet.http.HttpServletResponseWrapper;
	public class Coverresponse extends httpservletresponsewrapper{private printwriter tmpwriter;

	Private StringWriter output;
		Public Coverresponse (HttpServletResponse response) {super (response);    
		Output = new StringWriter ();
	Tmpwriter = new PrintWriter (output);    
		 public void Finalize () throws Throwable {super.finalize ();    
		 Output.close ();
	Tmpwriter.close ();   Public String getcontent () {Tmpwriter.flush ();    
		Refresh the buffer of the stream to see Java.io.Writer.flush () String s = output.tostring ();    
	This can be done on an as-needed basis for the output stream and the reset operation of the writer, such as Tmpwriter.getbytearrayoutputstream (). Reset () return s;     
	//Overwrite the Getwriter () method, using our own defined writer public PrintWriter getwriter () throws IOException {return tmpwriter; }  
	
	public void Close () throws IOException {tmpwriter.close (); }
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.