reprinted from: http://blog.csdn.net/it_man/article/details/7556903
Background: The SOA architecture used by the project, using the oracle10g SOA suite, adds a filter to the access policy used to resolve settings in the suite. In which there is a problem, oracle10g can not bind the IP with the instance number, so from the filter to start, try to Httpservletrequestwrapper, Httpservletresponsewrapper intercept the method of setting parameters. The result of the request can be modified for the requested parameter, but this may cause the oracle10g SOA suite to function properly, and want to get the returned content from response, but the result is null, so far the problem has not been resolved. There are good ways to welcome contact me, thank you.
1) Create a response wrapper. Extended Javax.servlet.http.HttpServletResponseWrapper. 2) provides a cache output of PrintWriter. Overloads the Getwriter method, returns a printwriter that saves everything sent to it, and stores the results in a field that can be accessed later. 3) Pass the wrapper to Dofilter. This call is legal because Httpservletresponsewrapper implements HttpServletResponse. 4) Extract and modify the output. After calling Filterchain's Dofilter method, the output of the original resource can be obtained using the mechanism provided in step 2. You can modify or replace it as long as it is appropriate for your application. 5) Send the modified output to the client. These outputs must be sent because the original resource is no longer sending output to the client (these outputs have been stored in your response wrapper). In this way, your filter needs to get PrintWriter or OutputStream from the original response object and pass the modified output to the stream. (2). Gzipfilter Class (3). Gziputil Class (4). Configuring Gzipfilter in Web. xml |
Attached Httpservletrequestwrapper, Httpservletresponsewrapper usage
1 classFilteredrequestextendsHttpservletrequestwrapper2 { 3 4 Publicfilteredrequest (servletrequest request)5 { 6 Super((httpservletrequest) request); 7 } 8 9 Publicstring GetParameter (string paramname)Ten { OneString value =Super. GetParameter (paramname); A if("Myparameter". Equals (paramname)) - { - //change the value of a request parameter theValue + = "|127.0.0.1"; - } - returnvalue; - } + - Publicstring[] Getparametervalues (String paramname) + { AString values[] =Super. Getparametervalues (paramname); at returnvalues; - } -}
1 ImportJava.io.ByteArrayOutputStream; 2 Importjava.io.IOException; 3 ImportJava.io.PrintWriter; 4 Importjava.io.UnsupportedEncodingException; 5 6 ImportJavax.servlet.ServletResponse; 7 ImportJavax.servlet.http.HttpServletResponse; 8 ImportJavax.servlet.http.HttpServletResponseWrapper; 9 Ten Public classWrapperresponseextendsHttpservletresponsewrapper One { A PrivateMyprintwriter Tmpwriter; - - PrivateBytearrayoutputstream output; the - Publicwrapperresponse (servletresponse httpservletresponse) - { - Super((HttpServletResponse) httpservletresponse); +Output =NewBytearrayoutputstream (); -Tmpwriter =Newmyprintwriter (output); + } A at Public voidFinalize ()throwsThrowable - { - Super. Finalize (); - Output.close (); - Tmpwriter.close (); - } in - PublicString getcontent () to { + Try - { theTmpwriter.flush ();//refresh the buffer of the stream, see Java.io.Writer.flush () *String s = Tmpwriter.getbytearrayoutputstream (). toString ("UTF-8"); $ //The output stream as well as the writer reset operation can be performed here as neededPanax Notoginseng //such as Tmpwriter.getbytearrayoutputstream (). Reset () - returns; the}Catch(unsupportedencodingexception e) + { A return"Unsupportedencoding"; the } + } - $ //overwrite the Getwriter () method, using our own defined writer $ PublicPrintWriter getwriter ()throwsIOException - { - returnTmpwriter; the } - Wuyi Public voidClose ()throwsIOException the { - Tmpwriter.close (); Wu } - About //custom PrintWriter to write response stream into the input stream of your own assignment $ //rather than the default Servletoutputstream - Private Static classMyprintwriterextendsPrintWriter - { -Bytearrayoutputstream Myoutput;//This is the object that holds the response input stream A + Publicmyprintwriter (bytearrayoutputstream output) the { - Super(output); $Myoutput =output; the } the the Publicbytearrayoutputstream Getbytearrayoutputstream () the { - returnMyoutput; in } the } the}
Call Place
1 New filteredrequest (request); 2 New Wrapperresponse (response); 3 Filterchain.dofilter (Filterrequest, filterresponse); 4 String content = Filterresponse.getcontent ();
or implement the HttpServletResponse and HttpServletRequest interfaces yourself, but this is troublesome. If you want to manage the session, or the session to achieve sharing with Httpsessionwrapper Learning Oscache page cache from the revelation: Http://blog.sina.com.cn/s/blog_ 866b16a60100z2bi.html first said a irrelevant, but the master must understand. What is the method of saving session ID in the URL of the output stream of the servlet's response? A. The Encodeurl method of the HttpServletRequest Interfaceb. The Encodeurl method of the HttpServletResponse interfacec. The Rewriteurl method of the HttpServletRequest interfaced. The Rewriteurl method of the HttpServletResponse interface answer selected B. After learning the Oscache page cache, it's implementation principle is as follows (principle and oscache like, but Oscache do better, I here just with pseudo-code simple description): Cachefilter
ImplementsFilter{dofilter (Request, response, chain) {String URLPath = req,...; if (Oscache.contains (URLPath)) {String content = Oscache.getkey (URLPath); Response.Write (content);} Else{cachehettpservletresponsewrapper wrapper = new Cachehettpservletresponsewrapper (respone); Chain.doFilter ( Reques, wrapper); String content = Wrapper.getcontent ();//Gets the HTML code oscache.put (URLPath, content) that the server outputs to the client; Response.Write (content);}}
Public Cachehettpservletresponsewrapper extends Hettpservletresponsewrapper{private String content;public Cachehettpservletresponsewrapper (httpservletresponse response) {...} Rewrite a method of hettpservletresponsewrapper, here may not be the Write method, forget what to call, go to see for yourself. Then I looked at the next one.
1 // overwrite the Getwriter () method, using our own defined writer 2 Public throws IOException
Hettpservletresponsewrapper.
public void Getwriter (String Content) {this. content = content;} Public String getcontent () {return this.content;}} : The inherited hettpservletresponsewrapper can be used to control the output to the client, or to get the content to output before the client outputs. I also hope that we will httpservletrequestwrapper. or implement the HttpServletResponse and HttpServletRequest interfaces yourself, but this is troublesome.
1) Create a response wrapper. Extended Javax.servlet.http.HttpServletResponseWrapper. 2) provides a cache output of PrintWriter. Overloads the Getwriter method, returns a printwriter that saves everything sent to it, and stores the results in a field that can be accessed later. 3) Pass the wrapper to Dofilter. This call is legal because Httpservletresponsewrapper implements HttpServletResponse. 4) Extract and modify the output. After calling Filterchain's Dofilter method, the output of the original resource can be obtained using the mechanism provided in step 2. You can modify or replace it as long as it is appropriate for your application. 5) Send the modified output to the client. These outputs must be sent because the original resource is no longer sending output to the client (these outputs have been stored in your response wrapper). In this way, your filter needs to get PrintWriter or OutputStream from the original response object and pass the modified output to the stream. (2). Gzipfilter Class (3). Gziputil Class (4). Configuring Gzipfilter in Web. xml |
--------------------------------------------------page Cache---HTML--->client level two cache--Action-->service layer-- Jsp-->html-->client: From the top, we see why the page cache is faster than the level two cache. Because the two-level cache processing process is more, and also to parse the JSP and tags, etc. into HTML, this is time-consuming.
Httpservletrequestwrapper, Httpservletresponsewrapper,httpsessionwrapper usage