📄 adlinkrotatorservlet.java
字号:
package shopcart.servlets;import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import shopcart.servlets.*;import DebugLog;public class AdLinkRotatorServlet extends GenericServlet implements SingleThreadModel{ protected Vector ads; protected int curIndex; protected String imageURL; protected DebugLog logger; public void init(ServletConfig config) throws ServletException { String imageDirName; String logServer; File imageDir=null; File adSpec=null; super.init(config); curIndex = 0; ads = new Vector(); imageDirName = getInitParameter("imagedir"); imageURL = getInitParameter("imageroot"); logServer = getInitParameter("logserver"); logger = new DebugLog(); logger.logTo(logServer); try { if(imageDirName != null) imageDir = new File(imageDirName); if(imageDir != null) adSpec = new File(imageDir,"ads.txt"); logger.log("Looking for adspec at: "+adSpec); //Try to load the ads from a specification file if((adSpec != null)&& adSpec.exists()) { try { FileReader fileIn = new FileReader(adSpec); BufferedReader bufIn = new BufferedReader(fileIn); String curLine; int index; String link,fileName; AdFileEntry newEntry; while((curLine = bufIn.readLine()) != null) { index = curLine.indexOf(" "); if(index > 0) { link = curLine.substring(0,index); fileName = curLine.substring(index+1); newEntry = new AdFileEntry(link,fileName); ads.addElement(newEntry); } } bufIn.close(); } catch(Exception exp) { logger.log("Invalid ad spec. file."); ads.removeAllElements(); } } //If no spec file, or a bad one, just show ads if((ads.size()==0) && (imageDir!=null) && imageDir.exists() && imageDir.isDirectory()) { String[] files; int i,max; files = imageDir.list(); max = files.length; for(i=0;i<max;i++) { ads.addElement(new AdFileEntry(null,files[i])); } } else if(ads.size()==0) { logger.log("Cannot find image dir: "+imageDirName); } } catch(Exception exp) { logger.log(exp); } } public void service(ServletRequest request, ServletResponse response) throws IOException,ServletException { AdFileEntry curEntry; PrintWriter out=null; int len; try { out = response.getWriter(); len = ads.size(); if(len>0) { curEntry = (AdFileEntry) ads.elementAt(curIndex); if(curEntry.link != null) { out.print("<A HREF=\""); out.print(curEntry.link); out.println("\">"); } out.print("<IMG SRC="); out.print(imageURL); out.print("/"); out.print(curEntry.fileName); out.println(">"); if(curEntry.link != null) { out.println("</A>"); } curIndex= (curIndex+1)%len; } else { //Print a comment to HTML for testing out.println("<!--No ads.-->"); } } catch(Exception exp) { logger.log(exp); } }}class AdFileEntry{ public String link; public String fileName; public AdFileEntry(String l,String file) { link = l; fileName = file; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -