Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot append string to response with Servlet Filter

Status
Not open for further replies.

bcreeve

Programmer
Nov 1, 2007
4
US
I simply want to use a servlet filter to take the response from JSPs and append a string before sending it back to the client. My servlet filter is in the chain and executing, but I'm receiving the error "OutputStream already retrieved"

Below is my doFilter method. Please let me know what I'm doing wrong, or if there is a simpler way of doing this. Thank you.

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
if (request.getAttribute(FILTER_APPLIED) == null)
{
request.setAttribute(FILTER_APPLIED, Boolean.TRUE);

String closingTags = "</body></html>";

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

chain.doFilter(request, wrappedResponse);

wrappedResponse.getWriter().append(closingTags);
}
else {
chain.doFilter(request, response);
}
}
 
Ok, after being tabled for awhile, I'm back on this issue.

I'm trying to create a servlet filter to append closing body and html tags (like "</body></html>")to the end of every HTML and JSP before sent to the browser.

I'm still stuck and running in circles. Any ideas to help me out would be greatly appreciated.
 
Hi,

Did you get any answer to this query ?
I am in a same need.

thanks for your response.

AK
 
Yes I did actually. I adapted a servlet filter to allow prepending text and appending text. I checked the content type before appending the closing tags to make sure it was "text/html", and applied some explicit mappings in web.xml so the servlet filter would not try to append HTML on .CSS, .JS, images, etc.

My servlet filter class ended up being like this:

Code:
package my.package;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
 
public class PrePostFilter extends GenericFilter { 
 
  public void doFilter(final ServletRequest request,
                       final ServletResponse response,
                       FilterChain chain)
       throws IOException, ServletException { 
  OutputStream out = response.getOutputStream();
  
  //prepend string
  out.write(new String("").getBytes());
  
  GenericResponseWrapper wrapper = 
         new GenericResponseWrapper((HttpServletResponse) response); 
  chain.doFilter(request,wrapper);
  out.write(wrapper.getData());
  
  //append string
  if (wrapper.getContentType() != null && wrapper.getContentType().equals("text/html")) {
	  out.write(new String("</body></html>").getBytes());
  }
  
  out.close(); 
  } 
}

...and the related excerpt from web.xml...

Code:
  <filter>
    <filter-name>PrePostFilter</filter-name>
    <filter-class>my.package.PrePostFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>PrePostFilter</filter-name>
    <url-pattern>/*.jsp</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>PrePostFilter</filter-name>
    <url-pattern>/*.htm</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>PrePostFilter</filter-name>
    <url-pattern>/*.html</url-pattern>
  </filter-mapping>

Hope this helps!
 
My servlet has doGet as follows

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try{
response.setContentType(CONTENT_TYPE);
//PrintWriter out = response.getWriter();
System.out.println("From Servlet");
RequestDispatcher rd = request.getRequestDispatcher("logs.jsp");
rd.include(request,response);
out.println("<table><tr><td>Hello !!</td></tr></table>");
out.close();
}catch(Exception e){
System.out.println("Error in servlet "+e);
}
}


then i have a filter on this with Wrapper as this...
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
try {

CharReponseWrapper wrapper = new CharReponseWrapper((HttpServletResponse)response);
chain.doFilter(request, wrapper);
System.out.println("In Filter "+wrapper.toString());
}catch(Exception e){
System.out.println("Error in filter "+e);
}
}


While the wrapper is this...

public class CharReponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter output;
public String toString() {
return output.toString();
}
public CharReponseWrapper(HttpServletResponse response){
super(response);
output = new CharArrayWriter();
}
public PrintWriter getWriter(){
return new PrintWriter(output);
}
}


in Servlet, i am inclugding a very simple JSP. What i see on webpage is what i am epxecting. But if you see the filter code, i have a System.out.println that does not print the output from JSP, it prints on the content added from servlet.

that is - In Filter <table><tr><td>Hello !!</td></tr></table>

Can you tell me why its not including the JSP output to the wrapper toString?

It appears on the browser though.

your help is much appreciated.

THANKS
AK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top