BannerServlet ver. 2.1


    Java servlet lets you display rotated banners. You can simply set this servlet as an <img> tag attribute in your html-file. A very compact solution for advertising support in your web applications.

How to use it:

a) copy bannerPackage.jar into your WEB-INF/lib directory.

b) define BannerServlet in your web.xml file:
 


    <servlet>
     <servlet-name>BannerServlet</servlet-name>
     <servlet-class>com.jsos.banner.BannerServlet</servlet-class>
    </servlet>

c) define a mapping:
 


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

In order to use servlet you have to prepare a text configuration file. This file describes your pictures (banners), how to show them etc. Configuration file will be passed to servlet as a parameter or as an initial parameter. So there are two basic forms for usage:
 


    a) http://your_host/servlet/BannerServlet?config_file or http://your_host/sevlet/BannerServlet?config=config_file

or you may describe config_file as an initial parameter for servlet (parameter name is config) in your web.xml file:
 


    <servlet>
     <servlet-name>BannerServlet</servlet-name>
     <servlet-class>com.jsos.banner.BannerServlet</servlet-class>
     <init-param>
      <param-name>config</param-name>
      <param-value>path_to_your_configuration_file</param-value>
     </init-param>
    </servlet>

and use servlet in this form:
 


    http://your_host/servlet/BannerServlet

    So in your HTML file you will have some like this:
 


    <img src="http://your_host/servlet/BannerServlet?config_file"  width=125 height=125>

    Such way you can just display different images.

    b) you can set own redirection URL for the each picture. In this case your configuration must have files description (see below) and servlet requires more parameters:
 


    <a href="http://your_host/servlet/BannerServlet?config=your_config_file&mode=2">
    <img src="http://your_host/servlet/BannerServlet?config=your_config_file&mode=1"
                    width=468 height=60></a>

    Such way you can support standard advertising banners

    c) you can have more than one advertising banner on the page. In this case just add to the each entry the parameter id=some_unique_integer_value. These values must be unique within your page. So, for example the first banner could be be described as:
 


 <a href="http://your_host/servlet/BannerServlet?config=your_config_file&mode=2&id=1">
    <img src="http://your_host/servlet/BannerServlet?config=your_config_file&mode=1&id=1"
                    width=468 height=60></a>

and the second is:
 


 <a href="http://your_host/servlet/BannerServlet?config=your_config_file&mode=2&id=2">
    <img src="http://your_host/servlet/BannerServlet?config=your_config_file&mode=1&id=2"
                    width=125 height=125></a>

If you are using an intial parameter, than just remove config= part from URL. E.g.:
 

 <a href="http://your_host/servlet/BannerServlet?mode=2&id=2">
    <img src="http://your_host/servlet/BannerServlet?mode=1&id=2"
                    width=125 height=125></a>

Configuration file is a text file describes servlet's settings. You can save this file anywhere on your server. Configuration file has got free format, but description of any parameter must starts with the appropriate reserved word on the new line. Empty lines and any line starts with # or // are ignored.

    Current version supports the following list of parameters:

# dir. Base directory for your banners. Servlet will output on the round-robin schema
# files from this directory with extensions GIF, JPG, JPEG, PNG.
# This parameter is mandatory.
dir=some_directory_on_your_server

# describe URL's for the redirection (target sites) in the form of:
# filename=appropriate_url. E.g.:
banner1.gif=http://www.acme.com
banner2.jpg=http://www.acme1.com

# how to rotate banners.
# 0 - per session (each user will have own loop)
# 1 - per application (one loop for all visitors)
# Default value is 0.
rotate=0

# Logging

# directory for log files
log=some_existing_directory_on_your_server

or

# file name
log=path_to_some_file_on_your_server

Log files

You can describe an existing directory for log files on your server. In this case servlet will create two log files per day. File names are ddmmyyyyv.txt and ddmmyyyyc.txt Where dd is a day, mm is a month and yyyy is a year. E.g. for Dec 18 2010 files are 18122010v.txt and 18122010c.txt

Alternatively you may set some generic file name. E.g. in your config set:
log=c:\data\adlog
where c:\data\adlog is just a generic name (not a directory). In this case servlet will save all data (for all days) in two files: c:\data\adlogv.txt and c:\data\adlogc.txt
First file (ends with v) keeps records about views and the second (ends with c) keeps records about redirections. Both files are text files with one record per line. Record format is (fields are separated with spaces):
 


IP address
date
picture file
referer field
user agent
redirection (for *c.txt files only)

How to rotate images without page reloading

You can use JavaScript for this. For example, this code rotates banners after 3 seconds:
 


<img src="http://your_host/servlet/BannerServlet?config_file" width=125 height=125 name="pic1">

<script language="JavaScript">
 function swap()
  { document.images['pic1'].src='http://your_host/servlet/BannerServlet?config_file';
   setTimeout('swap()',3000); }
 setTimeout('swap()',3000);
</script>

Notes:

1. Banner's configuration file can be saved anywhere on your server. E.g. if you are using BannerServlet?config line we assume this file is saved under the root (docBase) directory of your servletrunner. But you can of course always use the full path for setting config file location: BannerServlet?/home/users/my_file (or BannerServlet?c:\users\my_file)

2. Base directory is any existing directory (with banner files) on your server.

3. Evaluation version supports up to 3 banners per configuration file
 

for downloading:

servlet: bannerPackage.jar

 © Coldbeans      Comments?

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

Also in Coldtags: