Load balance for servlets ver. 2.9

It is a Java servlet allows you to redirect your requests to different servers. During the loading this servlet reads the list of sites from some text file and after that redirects requests to described sites on the cyclical basic (round robin). You have to provide that list of files as an initial parameter for servlet. Parameter's name is SitesPool and this parameter defines a name of text file with your sites. An initial parameter StickSession lets you forward the subsequent requests from the same visitor to one site in your pool.

How to install servlet

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

b) describe Redirect servlet in your web.xml file. E.g.:
 


    <servlet>
     <servlet-name>Redirect</servlet-name>
     <servlet-class>com.jsos.redirect.Redirect</servlet-class>
     <init-param>
      <param-name>SitesPool</param-name>
      <param-value>path_to_your_configuration_file</param-value>
     </init-param>
    </servlet>

c) define a mapping:
 


    <servlet-mapping>
     <servlet-name>Redirect</servlet-name>
     <url-pattern>/servlet/Redirect</url-pattern>
    </servlet-mapping>

Text file described as an initial parameter just contains the list of URL's for sites (one per line). Empty lines and any line starts with # or // are ignored.

E.g.:
 


http://host1/servlet/ControllerServlet
http://host2/servlet/ControllerServlet

In your HTML or JSP pages for all forms and hyperlinks where action points to ControllerServlet you can just change action to /servlet/Redirect. So in this case the first request will be redirected to ControllerServlet on host1, the second to ControllerServlet on host2, the third to host1 etc.

Of course, you can use different sets (pools) for redirection. Just describe Redirect servlet with different names and configuration files.

If your URL in the configuration file has not query part than Redirect will use query from the request. For example suppose our source code is:
 


<a href="/servlet/Redirect?a=b">index</a>

a) configuration file defines redirections by this way:
 


http://host1/index.html
http://host2/index.html

in this case request will be redirected to http://host1/index.html?a=b

b) configuration file defines redirections by this way:
 


http://host1/index.html?c=d
http://host2/index.html?c=d

in this case request will be redirected to http://host1/index.html?c=d

You can set a value true to the optional initial parameter StickSession. In this case servlet will forward all the requests within the same session to one host:
 


    <servlet>
     <servlet-name>Redirect</servlet-name>
     <servlet-class>com.jsos.redirect.Redirect</servlet-class>
     <init-param>
      <param-name>SitesPool</param-name>
      <param-value>path_to_your_configuration_file</param-value>
     </init-param>
     <init-param>
      <param-name>StickSession</param-name>
      <param-value>true</param-value>
     </init-param>
    </servlet>

default value for this parameter is false

Note: for JSP based solutions check out Load balance taglib

    For downloading:   redirectPackage.jar
 

 © Coldbeans    Comments?

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

Also in JSOS: