?? 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 = "<?xml version=\"1.0\"?>"; private static final String CONTENT_TYPE = "text/vnd.wap.wml"; private static final String DOC_TYPE = "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"\n" + " \"http://www.wapforum.org/DTD/wml_1.1.xml\">"; 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 <code>HttpServletRequest</code> value * @param response a <code>HttpServletResponse</code> 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("<wml>"); out.println("<template>"); out.println("<do type=\"prev\">"); out.println("<prev/>"); out.println("</do>"); out.println("</template>"); out.println("<card id=\"ads\" title=\"Ads\">"); out.println("<p align=\"center\">***<br/>"); out.println(chosenCommercial); out.println("<br/>***</p>"); out.println("</card>"); out.println("</wml>"); } /** * Selects randomly a commercial to be shown according to set priorities * for the commercials * * @return a <code>String</code> 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 ""; } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -