Redirect filter ver. 1.5


This is a Java servlet filter (as per Servlet API 2.3). This filter lets you redirect incoming requests according to some mapping table. So, you may use it for example as an access restriction tool, for load balancing etc.

How to use it:

a) download redirectflt.jar and save it in WEB-INF/lib

b) create a data file with you mapping info. It is a text file, each line describes an action for one source URI. Empty lines and any line starts with // or # are ignored. Path to this file should be provided as a parameter for RedirectFilter. Parameter name is config. Each action looks like:
 


uri=some_url

For example:
 


/a/test.jsp=http://www.acme.com
/a/test1.jsp=/mypage.jsp

here the request to /a/test.jsp will be redirected to http://www.acme.com and request to /a/test1.jsp will be forwarded to /mypage.jsp.

c) describe this filter in web.xml.
 


<filter>
  <filter-name>RedirectFilter</filter-name>
  <filter-class>com.cj.redirect.RedirectFilter</filter-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>your_data_file</param-value>
  </init-param>
</filter>

d) describe a mapping for this filter in web.xml. E.g.:
 


<filter-mapping>
  <filter-name>RedirectFilter</filter-name>
  <url-pattern>/a/*.jsp</url-pattern>
</filter-mapping>

in this example filter will be on for the each .jsp file in /a subdirectory

And now when some .jsp page (covered by RedirectFilter mapping) is requested RedirectFilter may redirect (or forward) request to the specified URL. If target URL starts with http than request will be redirected. Otherwise filter assumes a local resource and forwards request. The query string (if any) will be passed to the new site as is.

Note: your data file could be updated in the dynamic, you do not heed to restart this filter.

Also, you can update your redirection info right from the server side application (e.g. Struts action). You may use two methods:
 


public void com.cj.redirect.addRedirection(String from, String to)

public String com.cj.redirect.getRedirection(String from)

public void com.cj.redirect.removeRedirection(String from)

For dealing with your redirection data right from JSP pages you can use Redirection taglib.

   For downloading:

    Redirect package:  redirectflt.jar
 

 ©  Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.

Also in JSOS: