www.pudn.com > WAPpush.zip > Commercials.java
package commercials;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
/**
* Commercials class randomizes a commercial from a given list.
* Initialization-parameters:
* "commercialsfile" is where you store the commercials
*
* The number before the commercial is the "priority number", the higher,
* the bigger the chance is that this commercial appears.
*
* @version 1.0
* @since 1.0
* @see HttpServlet
*/
public final class Commercials extends HttpServlet {
private static final String XML_VERSION = "";
private static final String CONTENT_TYPE = "text/vnd.wap.wml";
private static final String DOC_TYPE =
"";
private static final Vector commercials = new Vector();
private static int[] priorities = null;
private static int prioritiesTogether=0;
/**
* This is the initialization code that runs only once.
* The servlet is being placed into service.
*
* @exception ServletException if an error occurs
*/
public void init() throws ServletException {
String commercialsFile = getInitParameter ("commercialsfile");
commercialsFile = getServletContext().getRealPath(commercialsFile);
try {
BufferedReader reader = new BufferedReader(new FileReader(commercialsFile));
String line;
while((line = reader.readLine()) != null) {
if(line.trim().equals("")) break;
commercials.add(line);
}
}
catch (java.io.IOException e) {
System.err.println(e);
throw new ServletException(e);
}
priorities = new int[commercials.size()];
for (int i = 0; i < commercials.size(); ++i){
String commercial = (String)commercials.get(i);
StringTokenizer st = new StringTokenizer(commercial, ";");
int number = Integer.parseInt(st.nextToken());
priorities[i] = number;
prioritiesTogether += number;
}
}
/**
* "doGet" is called when we want a commercial to appear.
*
* @param request a HttpServletRequest value
* @param response a HttpServletResponse value
* @exception ServletException if an error occurs
* @exception IOException if an error occurs
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
boolean cookieCompatible = false;
String chosenCommercial = getRandomCommercial();
response.setContentType(CONTENT_TYPE);
// prevent caching of the response
response.setHeader("cache-control", "no-cache");
PrintWriter out = response.getWriter();
out.println(XML_VERSION);
out.println(DOC_TYPE);
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println(" ");
out.println("");
out.println("");
out.println("***
");
out.println(chosenCommercial);
out.println("
***
");
out.println(" ");
out.println(" ");
}
/**
* Selects randomly a commercial to be shown according to set priorities
* for the commercials
*
* @return a String value
*/
public static String getRandomCommercial() {
try {
if(priorities == null || priorities.length == 0) return "";
int rnd = (int)(Math.random() * prioritiesTogether);
int commercialToBeChosen = 0;
int x = priorities[commercialToBeChosen];
while(x<=rnd)
x += priorities[++commercialToBeChosen];
StringTokenizer st =
new StringTokenizer((String)commercials.get(commercialToBeChosen), ";");
System.out.println("commercial: " + (String)commercials.get(commercialToBeChosen));
st.nextToken();
String chosenCommercial = st.nextToken();
return chosenCommercial == null ? "" : chosenCommercial;
}
catch (Exception e) { System.err.println(e); return ""; }
}
}