Results 1 to 2 of 2
  1. #1
    saint_jorjo is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

    Default servlet Filter problem

    i've made a program with servlet Filter
    and the programme have to put in every html page banner but the programme can't run
    i've mett class cast exception can someone help me

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;

    public class AdvertismentFilter implements Filter {
    private FilterConfig filterConfig;

    public void init(FilterConfig filter) throws ServletException{}
    // //Handle the passed-in FilterConfig
    // public void init(FilterConfig filterConfig) throws ServletException {
    // this.filterConfig = filterConfig;
    // }

    //Process the request/response pair
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException{
    PrintWriter out = response.getWriter();
    // HttpServletResponse httpRes = (HttpServletResponse)response;
    // MemoryResponseWrapper resWrapper = new MemoryResponseWrapper(httpRes);
    //
    // chain.doFilter(request,resWrapper);
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    MemoryResponseWrapper responseWrapper = new MemoryResponseWrapper(httpResponse);
    chain.doFilter(request, response);

    String contentType = request.getContentType();
    String originalRes = responseWrapper.toString();
    if((contentType != null)&&(contentType.toLowerCase().startsWith("text/html"))){
    String newResponse = addAdvertisment(originalRes);
    out.println(newResponse);
    }else{ out.println(originalRes);}
    out.close();
    }
    public String addAdvertisment(String aHtml){
    int endOfBodyIndex = aHtml.toLowerCase().indexOf("</body>");
    String htmlBefore;
    String htmlAfter;
    if(endOfBodyIndex!=-1){
    htmlBefore = aHtml.substring(0,endOfBodyIndex-1);
    htmlAfter = aHtml.substring(endOfBodyIndex);
    }else{
    htmlBefore = aHtml;
    htmlAfter = "";
    }
    String result = htmlBefore + "\n\n<br><p align='center'>" +"<a href='http://www.devbg.org'><img border='0' "
    +"src='http://www.devbg.org/ads/basd-logo.png'>" +"</a></p>\n\n"+htmlAfter;
    return result;

    }

    //Clean up resources
    public void destroy() {
    }
    }

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class MemoryResponseWrapper extends HttpServletResponseWrapper{
    private CharArrayWriter output;

    public MemoryResponseWrapper(HttpServletResponse aResponse){
    super(aResponse);
    output = new CharArrayWriter();
    }
    public String toString(){
    String result = output.toString();
    return result;
    }
    public PrintWriter getPrintWriter(){
    PrintWriter printW = new PrintWriter(output);
    return printW;
    }
    }

  2. #2
    saint_jorjo is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

    Default

    evrything is ok my servlet bibl. not contains method response.getContentType i will get a new one sorry i'm donn

Similar Threads

  1. Problem in Servlet with JDBC
    By *New Programer* in forum Java Servlet
    Replies: 2
    Last Post: 12-20-2007, 10:15 AM
  2. Object filter
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 08-08-2007, 08:40 PM
  3. Need a filter
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 08:37 PM
  4. Servlet Problem after install ^^Urgent^^
    By black_box in forum Java Servlet
    Replies: 0
    Last Post: 08-05-2007, 02:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •