Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using a servlet filter to modify output of a jsp?

Reply
Thread Tools

Using a servlet filter to modify output of a jsp?

 
 
Harry Bosch
Guest
Posts: n/a
 
      12-01-2005
I was wondering if anyone had implemented some sort of filter that
could alter the generated contents of a JSP. What I want to do is
grab the the results of a jsp before it is sent back to the client so
that I can add something to the dom tree. Is this possible?

Thanks,

Harry.

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      12-01-2005

"Harry Bosch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I was wondering if anyone had implemented some sort of filter that
> could alter the generated contents of a JSP. What I want to do is
> grab the the results of a jsp before it is sent back to the client so
> that I can add something to the dom tree. Is this possible?


Yes.

One simple way to do it is to have the your code fetch the JSP as if it
were a web browser, connecting via HTTP to the local loopbrack address and
everything, and then do whatever modifications you want, and then send that
to the user.

It's sort of like writing a proxy.

- Oliver


 
Reply With Quote
 
 
 
 
kiranmalla.in@gmail.com
Guest
Posts: n/a
 
      12-01-2005
Hi Harry

I think this should be perfectly possible. Haven't tried it though. You
can implement a filter to do such type of stuff. I have implemented a
filter which gives me log information about when the modeler (can be
used to provide the info abt jsp as well) is executed.

A filter would provide you callback methods which you can use to alter
request or response object. So i would assume you want to add some
attribute to the response object before serving it to the client which
should be perfectly possible using a filter. I have used filter to
explicitly set the character encoding to UTF-8 before serving the
response..

Hope this helps!

-Kiran

 
Reply With Quote
 
Abhijat Vatsyayan
Guest
Posts: n/a
 
      12-01-2005
Harry Bosch wrote:
> I was wondering if anyone had implemented some sort of filter that
> could alter the generated contents of a JSP. What I want to do is
> grab the the results of a jsp before it is sent back to the client so
> that I can add something to the dom tree. Is this possible?
>
> Thanks,
>
> Harry.
>

Yes, it will be painful but you should be able to achieve this.

I would suggest following approach (which I have not tried of course
since it is painful).

* Create a proxy implementation of HttpServletResponse interface which
should delegate all method invocations to the underlying
HttpServletResponse object provided by the container. You can write this
using a brute force approach or use java's proxying capability (see
java.lang.reflet.Proxy).

class ProxyHttpServletResponse implements HttpServletResponse
{
private HttpServletResponse proxiedObject ;
// some buffer ..
private ServletOutputStream buffer= new MyServletOutputStream(...) ;
private PrintWriter writer ;
ProxyHttpServletResponse (HttpServletResponse proxiedObject)
{
this.proxiedObject = proxiedObject ;
// You will have to do a better job with encoding!
this.writer = new ......(create using the buffer)
}
public void addCookie(Cookie cookie)
{
this.proxiedObject.addCookie(cookie) ;
}
...
...

}

* Change the implementation of getOutputStream and getWriter methods in
your proxy to return a stream/writer that keeps all the written content
in memory (or cache, file etc. ) and does not send it out.
public PrintWriter getWriter( )
{
return this.writer ;
}
public ServletOutputStream getOutputStream()
{
return buffer;
}

* Make sure you do the appropriate things with methods like
"flushBuffer", "reset" and "resetBuffer" . There may be more
stream/writer handling methods in HttpServletResponse interface.

* When forwarding call from the filter (doFilter method), create a new
HttpServletRequest object using your implementation and use your
implementation in place of container provided object.

doFilter(... )
{
//check and ensure that the types are all OK ..
ProxyHttpServletResponse bufferedResponse = new
ProxyHttpServletResponse ((HttpServletResponse) response) ;
filterChain.doFilter(... , bufferedResponse,...) ;
bufferedResponse.processJSPOutputAndFlush() ;
}

* Finally when the control gets back to you in the filter, you may want
to signal (send a message to) your proxy (servlet response object) to
manipulate the buffered output. Then write the contents to the proxied
HttpServletResponse object that was provided by the container.

void processJSPOutputAndFlush()
{
//do the processing ...
// write the contents to the original HttpServletResponse
objects stream and flush ..
}
 
Reply With Quote
 
Harry Bosch
Guest
Posts: n/a
 
      12-06-2005
Thanks for the reply Abhijat (and everyone else), I think I will go
with your solution. I'll start coding now and see how it goes. FYI,
the reason I am doing this is so I can set up environments for
development, testing, integeration (and production, but this will be
disabled there) and have the contents of the returned HTML altered
based on those configurations. Doing this allows up to have a simple
environment variable set and have the page altered based on that
variable so that developers/testers etc. know which environment they
are working on. This way I don't have to have every page, or what-not,
contain code for this environment aware feature. Hope that explains
it.

If anyone has tackled this issue more easily, let me know. Perhaps I
am making this a bit too complicated, but when it's running, it seems
like it could work nicely. Though, a bit of processing, but that is
only in the non-production environments.

 
Reply With Quote
 
Harry Bosch
Guest
Posts: n/a
 
      12-06-2005
After looking through the API docs I did find a convenient decorator
for wrapping a servlet response:
javax.servlet.http.HttpServletResponseWrapper. This way I only need to
override a couple methods to handle what I needed to do. It's
basically the pattern that you, Abhijat, were pointing me at. I was
going the actual java.lang.reflect.Proxy route but I came across this
and figured I would just "get it over with".

Thanks again.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Authentication via LDAP Using Servlet Filter peeping_t@hotmail.com Java 2 11-09-2005 08:25 PM
Filter output by using a HttpModule Thomas ASP .Net 4 10-23-2004 10:13 AM
Newbie: How do I filter output to the screen and writing the orginal output to a file? Mav Perl Misc 22 07-09-2004 09:56 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
how to get my servlet configuration before the servlet is initialised Andy Fish Java 4 12-17-2003 09:47 PM



Advertisments