Use filter to change a JSP's HTML?

T

Todd

Is it possible to use a filter to change a JSP's HTML code?

As an example, suppose I wanted a way to ensure that ALL input elements
had the attribute "autocomplete='false'" in them. Could I create a
filter that runs after the servlet, that could grab the outgoing HTML
and insert the autocomplete attribute in it?

I guess the main question is this: at what point of a servlet's
lifecycle is the resultant HTML available? And, what object, if any,
gives me access to that HTML?

Thanks,
Todd
 
J

jan V

As an example, suppose I wanted a way to ensure that ALL input elements
had the attribute "autocomplete='false'" in them. Could I create a
filter that runs after the servlet, that could grab the outgoing HTML
and insert the autocomplete attribute in it?

What an absolutely horrible idea...

Wouldn't it be better to enhance the HTML generation code to add the
attribute?
 
T

Todd

Jan,
Thanks for the reply.

Why is this a horrible idea (besides the obvious - "what you see isn't
what you get")?

I assume when you say "enhance the HTML generation code" you are
referring to changes to the IDE to automatically insert autocomplete to
every new input tag? If so, that would be a good suggestion for new
pages.

But what about all the existing pages? We currently have quite a
number of pages in a number of web apps up and running. Either every
page would have to be changed OR a filter could address the everything
in one go.

Also, autocomplete is just one of several modifications I'd like to do.
 
B

Bryce

Is it possible to use a filter to change a JSP's HTML code?

As an example, suppose I wanted a way to ensure that ALL input elements
had the attribute "autocomplete='false'" in them. Could I create a
filter that runs after the servlet, that could grab the outgoing HTML
and insert the autocomplete attribute in it?

I guess the main question is this: at what point of a servlet's
lifecycle is the resultant HTML available? And, what object, if any,
gives me access to that HTML?

One idea would be to make sure your HTML is valid XML, then use an
XSLT stylesheet to transform it. We do something similar, where our
servlet outputs XML, and we use a filter to transform it to HTML. As
long as the HTML is valid XML, I don't see why that wouldn't work.

And the object would probably be the response. You might need to wrap
the response in order to get access to the body, not sure, and too
lazy at the moment to look it up.

Good reference here:
http://java.sun.com/products/servlet/Filters.html
 
T

Todd

Thanks for the reply and reference Bryce!

Unfortunately our HTML is not even close to being valid XML (we're an
old school shop, struggling to even get close to being current) :-(.


I've had a look at what the response exposes and the leading candidate
is the output string. Unfortunately, I don't see anthing exposed that
gives me access to its contents.
 
B

Bryce

I've had a look at what the response exposes and the leading candidate
is the output string. Unfortunately, I don't see anthing exposed that
gives me access to its contents.

Ahh.. You are giving me an excuse to fire up Eclipse again... Been
doing C# work for the past month...

You need to create an HttpServletResponseWrapper.

Here are the relative portions of mine (won't compile, just an
extract. That's left as an exercise for the user):

public class ResponseWrapper extends HttpServletResponseWrapper {

private CharArrayWriter output;
private String m_contentType = "";
private int errorCode = 0;

public ResponseWrapper(HttpServletResponse response) {
super(response);
output = new CharArrayWriter();
}

public String toString() {
return output.toString();
}

public PrintWriter getWriter() {
return new PrintWriter(output);
}

....
}

To use, you need to wrap the Response into your wrapper like this in
your Filter:

ResponseWrapper wrappedResponse = new ResponseWrapper(
(HttpServletResponse) response);

//doBeforeProcessing(wrappedRequest, wrappedResponse);

Throwable problem = null;

try {
chain.doFilter(wrappedRequest, wrappedResponse);
....

Then you can access your response by just using the toString() method
above:

response.toString();

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top