Sub-domain filter ver. 1.2

This is a Java servlet filter (as per Servlet API 2.3). DomainFilter lets work with sub-domains on your host. Filter forwards all the subdomain's incoming requests to some sub-directory on your site. So it is like a mapping: map some sub-domain on your host to some directory on your server.

How to use it:

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

b) describe this filter in web.xml file. You must provide two initial parameters: domain for your subdomain description and dir for setting the redirection. E.g.:
 


<filter>
  <filter-name>DomainFilter</filter-name>
  <filter-class>com.cj.domain.DomainFilter</filter-class>
  <init-param>
    <param-name>domain</param-name>
    <param-value>http://test.acme.com</param-value>
  </init-param>
  <init-param>
    <param-name>dir</param-name>
    <param-value>/sub/test</param-value>
  </init-param>
</filter>

here filter prepares to forward all the requests to http://test.acme.com (parameter domain) to the directory /sub/test/ (parameter dir).

c) describe a mapping for this filter in web.xml
 


<filter-mapping>
  <filter-name>DomainFilter</filter-name>
  <url-pattern>*</url-pattern>
</filter-mapping>

and now all the requests to the http://test.acme.com will be forwarded to the directory /sub/test/.

You can use regular expressions in the initial parameters as well use this filter as simply redirection tool. For example:
 


<filter>
  <filter-name>DomainFilter</filter-name>
  <filter-class>com.cj.domain.DomainFilter</filter-class>
  <init-param>
    <param-name>domain</param-name>
    <param-value>http://mydomain.com/user/(.*)</param-value>
  </init-param>
  <init-param>
    <param-name>dir</param-name>
    <param-value>/users/$1</param-value>
  </init-param>
</filter>

If the target URL starts with http than request will be redirected. Otherwise filter assumes a local resource and forwards request.
 

    For downloading:   domainPackage.jar
 

 ©  Coldbeans     Comments?

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

Also in JSOS: