Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prob. modify the servlet response using filter

 
Mohan raj gaddam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code wht i am using, this is an example give in the oronserver.com and some other sites too..
import javax.servlet.*;
public class GenericFilter implements javax.servlet.Filter {
public FilterConfig filterConfig;
public void doFilter(final ServletRequest request,
final ServletResponse response,
FilterChain chain)
throws java.io.IOException, javax.servlet.ServletException {
chain.doFilter(request,response);
}
public void init(final FilterConfig filterConfig) { this.filterConfig = filterConfig;
}
public void destroy() {
}
}
=============================================================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FilterServletOutputStream extends ServletOutputStream {
private DataOutputStream stream;
public FilterServletOutputStream(OutputStream output) {
stream = new DataOutputStream(output);
}
public void write(int b) throws IOException {
stream.write(b);
}
public void write(byte[] b) throws IOException {
stream.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
stream.write(b,off,len);
}
}
===================================================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class GenericResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream output;
private int contentLength;
private String contentType;
public GenericResponseWrapper(HttpServletResponse response) {
super(response);
output=new ByteArrayOutputStream();
}
public byte[] getData() {
return output.toByteArray();
}
public ServletOutputStream getOutputStream() {
return new FilterServletOutputStream(output);
}
public PrintWriter getWriter() {
return new PrintWriter(getOutputStream(),true);
}
public void setContentLength(int length) {
this.contentLength = length;
super.setContentLength(length);
}
public int getContentLength() {
return contentLength;
}
public void setContentType(String type) {
this.contentType = type;
super.setContentType(type);
}
public String getContentType() {
return contentType;
}
}
===========================================================
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();
out.write("<HR>PRE<HR>".getBytes());
GenericResponseWrapper wrapper = new
GenericResponseWrapper((HttpServletResponse) response);
chain.doFilter(request,wrapper);
out.write(wrapper.getData());
out.write("<HR>POST<HR>".getBytes());
out.close();
}
}
===========================================================
filter3.jsp
<HTML>
<HEAD>
<TITLE>Filter Example 3</TITLE>
</HEAD>
<BODY>
This is a testpage. You should see<br>
this text when you invoke filter3.jsp, <br>
as well as the additional material added<br>
by the PrePostFilter.
<br>
</BODY>
</HTML
When i run the above code i am not getting data which is there in the jsp page, i am getting data which i am writing it in the prepostFilter.
the output is
PRE
Post
Is the above is overwriting the actual response ?
If so can anyone tell me how to modify the response of the servlet instead of overwriting.............i will so thankful to you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic