Something a master has to do.
Background: The SOA architecture used in the project, using the oracle10g SOA suite, adds a filter for the access policy used to resolve the settings. In which a problem was encountered, oracle10g could not bind the IP to the instance number, and starting from the filter, the Httpservletrequestwrapper, Httpservletresponsewrapper interception method of setting parameters was attempted. The resulting request can be modified for the requested parameter, but this may cause the oracle10g SOA suite to run normally and want to get the returned content from the response, but the result is null, so far the problem is still unresolved. Have a good method of welcome to 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 saves the result in a field that can be accessed later.
3) Pass the wrapper to Dofilter. This call is legal because the Httpservletresponsewrapper implements HttpServletResponse.
4) Extract and modify the output. Once the Filterchain Dofilter method is invoked, the output of the original resource can be obtained only with 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. Because the original resource is no longer sending the output to the client (the output is already stored in your response wrapper), you must send the output. 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). Configure Gzipfilter in Web.xml |
Attached Httpservletrequestwrapper, Httpservletresponsewrapper usage
Class Filteredrequest extends Httpservletrequestwrapper
{public
filteredrequest (ServletRequest request)
{
super ((httpservletrequest) request);
}
public string GetParameter (string paramname)
{
String value = Super.getparameter (paramname);
if ("Myparameter". Equals (paramname))
{
//change the value of the request parameter
= + = "|127.0.0.1";
}
return value;
}
Public string[] Getparametervalues (string paramname)
{
String values[] = super.getparametervalues ( paramname);
return values;
}
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.io.UnsupportedEncodingException;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletResponse;
Import Javax.servlet.http.HttpServletResponseWrapper;
public class Wrapperresponse extends Httpservletresponsewrapper {private Myprintwriter tmpwriter;
Private Bytearrayoutputstream output;
Public Wrapperresponse (Servletresponse httpservletresponse) {super ((HttpServletResponse) httpservletresponse);
Output = new Bytearrayoutputstream ();
Tmpwriter = new Myprintwriter (output);
public void Finalize () throws Throwable {super.finalize ();
Output.close ();
Tmpwriter.close (); Public String getcontent () {try {tmpwriter.flush ();//Refresh the buffer of the stream, see JAVA.IO.WRITER.FL
Ush () 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 the Getwriter () method, using our own defined writer public PrintWriter getwriter () throws IOException {Retu
RN Tmpwriter;
public void Close () throws IOException {tmpwriter.close (); //Custom PrintWriter, in order to write the response stream to the specified input stream//rather than the default Servletoutputstream private static class Myprintwri ter extends PrintWriter {bytearrayoutputstream myoutput;//This is the object that holds the response input stream Myprintwrit
ER (bytearrayoutputstream output) {super (output);
Myoutput = output;
Public Bytearrayoutputstream Getbytearrayoutputstream () {return myoutput; }
}
}
Call Office
Filteredrequest filterrequest = new Filteredrequest (request);
Wrapperresponse filterresponse = new Wrapperresponse (response);
Filterchain.dofilter (Filterrequest, filterresponse);
String content = Filterresponse.getcontent ();
or implement HttpServletResponse and HttpServletRequest interfaces yourself, but that's a hassle.
If you want to manage session, or session implementation share, use Httpsessionwrapper
Lessons learned from Oscache page caching: http://blog.sina.com.cn/s/blog_866b16a60100z2bi.html
First of all, say a irrelevant, but the master must understand. Which is the method by which the servlet's response output stream holds the session ID in the URL. A. The Encodeurl method of the HttpServletRequest interface B. Encodeurl method of the HttpServletResponse interface C . The Rewriteurl method is the HttpServletRequest interface D. Rewriteurl method of the HttpServletResponse interface
Choose b for the answer.
After learning the Oscache page cache, the principle of its implementation is roughly as follows (the principle and oscache, but Oscache do better, I just use pseudo-code description): Cachefilter
Implementsfilter{Dofilter (Request, response, chain) {string urlpath = req,...; if (Oscache.contains (URLPath)) {String content = Osca Che.getkey (URLPath); Response.Write (content); }else{Cachehettpservletresponsewrapper wrapper = new Cachehettpservletresponsewrapper (respone); Chain.doFilter ( Reques, wrapper); String content = Wrapper.getcontent ();//Get 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 cachehettp Servletresponsewrapper (httpservletresponse response) {...}//Rewrite a method of Hettpservletresponsewrapper, this may not be the Write method , forget what to call, see for yourself. Then look at the next is/overlay Getwriter () method, using our own defined writer
PublicPrintWriter getwriter ()
throwsIOException
Hettpservletresponsewrapper. public void Getwriter (String Content) {this. Content = content; Public String getcontent () {return this.content}}}
: Use the inheritance hettpservletresponsewrapper to control the output to the client, or to obtain the output before the client outputs. I also hope that everyone will httpservletrequestwrapper. or implement HttpServletResponse and HttpServletRequest interfaces yourself, but that's a hassle.
|
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 saves the result in a field that can be accessed later.
3) Pass the wrapper to Dofilter. This call is legal because the Httpservletresponsewrapper implements HttpServletResponse.
4) Extract and modify the output. Once the Filterchain Dofilter method is invoked, the output of the original resource can be obtained only with 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. Because the original resource is no longer sending the output to the client (the output is already stored in your response wrapper), you must send the output. 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). Configure Gzipfilter in Web.xml |
--------------------------------------------------Page Cache---"HTML--->client level two cache--" Action-->service layer--> Jsp-->html-->client
: To see why the page cache is faster than the level two cache. Because the level two cache processing process is more, and also to parse JSP and tags, etc. into HTML, this is time-consuming.