FileDocCategorySizeDatePackage
AdServlet.javaAPI DocExample2084Sat Dec 02 19:43:52 GMT 2000jabadot

AdServlet

public class AdServlet extends HttpServlet
Banner Ad Rotator, similar to the M$ Ad Rotator provided in ASP. Converted to a servlet for efficiency. Picks ad at random. Input file format is like this: URL-to-image:alt-desc:ImageName e.g. http://woo.trbleclick.com/images/Gurfle.gif:Georgeous Grufles:GGBanner.gif

Fields Summary
protected ArrayList
list
The list of ads
protected File
file
The File object for statting the adlist file
Random
r
A Random Number generator for picking the next ad.
protected long
load_time
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

		doPost(req, resp);
	
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
Print the HTML for the next advertisement for a user.


		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();

		// If the list-of-ads file has changed, reload it.
		// Synchronized so we only reload once, to avoid having
		// multiple threads calling load().
		synchronized(this) {
			if (load_time < file.lastModified()) {
				out.println("<!-- reloaded adrotator -->");
				list = AdAccessor.load(file);
				load_time = file.lastModified();
			}
		}

		// Pick an ad at random from the list
		int n = r.nextInt(list.size());
		Ad ad = (Ad)list.get(n);

		// Convert it to HTML
		out.println("<!-- Ad link made by AdServlet -->");
		out.println(ad.html);
	
public voidinit(javax.servlet.ServletConfig cfg)
initialize the AdServlet -- load the adlist


	        
	      
		try {
			String adsList = JDConstants.getProperty("jabadot.dir.ads") +
					JDConstants.getProperty("jabadot.ads_def_list");
			System.err.println("AdServlet: Opening " + adsList);
			file = new File(adsList);
			list = AdAccessor.load(file);
			load_time = file.lastModified();
		} catch (IOException e) {
			throw new ServletException(e.toString());
		}