?
Solved

Filter: update site name without hardcode

Posted on 2005-04-16
5
Medium Priority
?
257 Views
Last Modified: 2010-08-05
Hi!

I am modifying a filter that already written for changing site name, but I can’t hardcode the site names into the filter itself. My question is how can I update site name without hardcode? Can I get parameter?

Here is the original code.
+++++++
package moreservlets.filters;

public class ReplaceSiteNameFilter extends ReplaceFilter {
  public String getTargetString() {
    return("filtersRus.com");
  }

  public String getReplacementString() {
    return("weBefilters.com");
  }
}
+++++++
package moreservlets.filters;

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

public abstract class ReplaceFilter implements Filter {
  private FilterConfig config;

  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain)
      throws ServletException, IOException {
    CharArrayWrapper responseWrapper =
      new CharArrayWrapper((HttpServletResponse)response);
    // Invoke resource, accumulating output in the wrapper.
    chain.doFilter(request,responseWrapper);
    // Turn entire output into one big String.
    String responseString = responseWrapper.toString();
    // In output, replace all occurrences of target string
    // with replacement string.
    responseString =
      FilterUtils.replace(responseString,
                          getTargetString(),
                          getReplacementString());
    PrintWriter out = response.getWriter();
    out.write(responseString);
  }

  /** Store the FilterConfig object in case subclasses
   *  want it.
   */

  public void init(FilterConfig config)
      throws ServletException {
    this.config = config;
  }

  protected FilterConfig getFilterConfig() {
    return(config);
  }

  public void destroy() {}

  /** The string that needs replacement.
   *  Override this method in your subclass.
   */

  public abstract String getTargetString();

  /** The string that replaces the target.
   *  Override this method in your subclass.
   */

  public abstract String getReplacementString();
}
+++++++++++++
package moreservlets.filters;

public class FilterUtils {

  /** Change all occurrences of orig in mainString to
   *  replacement.
   */

  public static String replace(String mainString,
                               String orig,
                               String replacement) {
    String result = "";
    int oldIndex = 0;
    int index = 0;
    int origLength = orig.length();
    while((index = mainString.indexOf(orig, oldIndex))
          != -1) {
      result = result +
               mainString.substring(oldIndex, index) +
               replacement;
      oldIndex = index + origLength;
    }
    result = result + mainString.substring(oldIndex);
    return(result);
  }
}
+++++++++++++++
package moreservlets.filters;

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


/** A response wrapper that takes everything the client
 *  would normally output and saves it in one big
 *  character array.
 */

public class CharArrayWrapper
             extends HttpServletResponseWrapper {
  private CharArrayWriter charWriter;

  /** Initializes wrapper.
   *  <P>
   *  First, this constructor calls the parent
   *  constructor. That call is crucial so that the response
   *  is stored and thus setHeader, setStatus, addCookie,
   *  and so forth work normally.
   *  <P>
   *  Second, this constructor creates a CharArrayWriter
   *  that will be used to accumulate the response.
   */
 
  public CharArrayWrapper(HttpServletResponse response) {
    super(response);
    charWriter = new CharArrayWriter();
  }

  /** When servlets or JSP pages ask for the Writer,
   *  don't give them the real one. Instead, give them
   *  a version that writes into the character array.
   *  The filter needs to send the contents of the
   *  array to the client (perhaps after modifying it).
   */
 
  public PrintWriter getWriter() {
    return(new PrintWriter(charWriter));
  }

  /** Get a String representation of the entire buffer.
   *  <P>
   *  Be sure <B>not</B> to call this method multiple times
   *  on the same wrapper. The API for CharArrayWriter
   *  does not guarantee that it "remembers" the previous
   *  value, so the call is likely to make a new String
   *  every time.
   */
 
  public String toString() {
    return(charWriter.toString());
  }

  /** Get the underlying character array. */
 
  public char[] toCharArray() {
    return(charWriter.toCharArray());
  }
}
++++++++
thanks,

0
Comment
Question by:dkim18
  • 2
  • 2
5 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13798715
If you want to find out the name of the url that this webapp is in you can call  "request.getServerName()" which will return the url of the server.

e.g.
http://localhost:8080/brs/index.jsp
would return:localhost

and request.getContextPath() returns the name of the webapp: in this case "/brs"
0
 
LVL 28

Accepted Solution

by:
rrz earned 400 total points
ID: 13799567
>Can I get parameter?
You could edit your context's web.xml to include something like the following.
           <filter>
                   <filter-name>replaceFilter</filter-name>
                   <filter-class>moreservlets.filters.ReplaceSiteNameFilter </filter-class>
                   <init-param>
                         <param-name>targetString</param-name>
                         <param-value>filtersRus.com</param-value>
                   </init-param>
            </filter>  

then you could use  
 
package moreservlets.filters;
public class ReplaceSiteNameFilter extends ReplaceFilter {
  String targetString = "default";
  public void init(FilterConfig config) throws ServletException{
                                      targetString =  config.getInitParameter("targetString");
  }
  public String getTargetString() {
                                                 return targetString;
  }
...
}
0
 

Author Comment

by:dkim18
ID: 13803846
This is what I did, but when I requested the jsp page, it was just taking forever.(didn't show any error..)

package moreservlets.filters;

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

public class ReplaceCEONameFilter extends ReplaceFilter {
      private FilterConfig config;
   private static final String DEFAULT_TARGET_NAME =
    "MISSING-TARGET-NAME";

    private static final String DEFAULT_REPLACEMENT_NAME =
    "MISSING-REPLACEMENT-NAME";
    String targetCEOName="";
    String replacementCEOName="";
  public void init(FilterConfig config)
        throws ServletException{          
            String targetCEOName =  config.getInitParameter("targetCEOName");
            String replacementCEOName =  config.getInitParameter("replacementCEOName");
  }
  public String getTargetString() {
      if (targetCEOName == null) {
      targetCEOName = DEFAULT_TARGET_NAME;
    }
    return(targetCEOName);
  }
   
  public String getReplacementString() {
      if (replacementCEOName == null) {
      replacementCEOName = DEFAULT_REPLACEMENT_NAME;
    }
    return replacementCEOName;
  }
}
++++++++++++
  <filter>
     <filter-name>ReplaceCEONameFilter</filter-name>
     <filter-class>moreservlets.filters.ReplaceCEONameFilter </filter-class>
        <init-param>
           <param-name>targetCEOName</param-name>
           <param-value>Bill Gate</param-value>
        </init-param>
        <init-param>
            <param-name>replacementCEOName</param-name>
            <param-value>Marty Hall</param-value>
        </init-param>
  </filter>  
...
...
 <filter-mapping>
    <filter-name>ReplaceCEONameFilter</filter-name>
    <url-pattern>/plugSite/CEONamePage1.jsp</url-pattern>
 </filter-mapping>
0
 
LVL 28

Expert Comment

by:rrz
ID: 13811315
>it was just taking forever.(didn't show any error..)
Is CEONamePage1.jsp  very biig ?
Does the filter work ?
0
 

Author Comment

by:dkim18
ID: 13829013
I shouldn't declare these inside of init().
String targetCEOName
 String replacementCEOName

thanks,
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question