對於一個商務網站來說
我們一般在網頁上放上一些圖片
下面
用法有三種是
例如
<IMG height=
用這種方法你就可以顯示不同的圖片了
<IMG height=
這樣你就可以支持標准的旗幟廣告了
<IMG height=
第二個為
<IMG height=
有的朋友會問了
解釋
解釋
banner
banner
解釋
附錄
Banner 系統每天會自動產生兩個日志文件
IP地址 日期 圖片文件 用戶代理 重定向記錄 (只用於 *c
附錄
import java
import java
import javax
import javax
public class Banner extends HttpServlet
{
public Banner(){ }
//讀取配置文件內容
private boolean readConfig(String sConfig
{
try
{
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(new FileInputStream(sConfig)));// 設置緩沖區讀入一個配置文件
String sLineInformation
while((sLineInformation
{
sLineInformation
if(sLineInformation
int i = sLineInformation
if(i >
//配置文件的每一行參數必須以不為#或//開頭的字符串
hashtable
}
}
bufferedreader
File file = new File(sConfig);//創建一個配置文件
hashtable
}
catch(Exception _ex)
{
return false;
}
String sDirInfo
if(sDirInfo
{
if(!sDirInfo
{
sDirInfo
hashtable
hashtable
}
File file
String as[] = file
if(as == null) {
hashtable
}
sDirInfo
if(sDirInfo
{
if(!sDirInfo
{
sDirInfo
hashtable
hashtable
}
File file
String as
if(as
hashtable
}
return true;
}
private Hashtable getConfig(String s)//取得配置
{
Hashtable hashtable = (Hashtable)cfgs
if(hashtable != null)//如果配置不為空
try
{
String s
File file = new File(s);
if(!s
//如果s
hashtable = null;
}
catch(Exception _ex)//捕獲Exception _ex錯誤
{
hashtable = null;
}
if(hashtable != null)
return hashtable;
hashtable = new Hashtable();
if(!readConfig(s
{
return null;
}
else
{
cfgs
return hashtable;
}
}
public void init(ServletConfig servletconfig)//初始化配置參數
throws ServletException {
//如果出錯
super
separator = System
cfgs = new Hashtable();//設置配置變量
logs = new Hashtable();//設置日志變量
System
}
public void destroy() { }
public void doPost(HttpServletRequest request
throws ServletException
{ doGet(request
public void doGet(HttpServletRequest httpservletrequest
throws ServletException
{
String strConfig = httpservletrequest
//讀取請求字符串
if(strConfig == null)//如果字符串為空
strConfig =
if(strConfig
//如果strConfig長度為零
{
errorMessage(
return;
}
String strConfig
if(strConfig
strConfig
Hashtable hashtable = getConfig(strConfig
if(hashtable == null)
{
errorMessage(
return;
}
if(hashtable
//如果哈希表中dir為空值
{
errorMessage(
return;
}
String strConfigMode
if(strConfigMode
strConfigMode
String strConfigId
if(strConfigId
strConfigId
HttpSession httpsession = httpservletrequest
if(strConfigMode
file://如果strConfigMode
{
showBanner(hashtable
return;
}
else //否則轉向banner所指的站點
{
goToSite(hashtable
return;
}
}
private void goToSite(Hashtable hashtable
throws IOException //如果有任何錯誤
{
String sitename
if(httpsession == null)//如果httpsession為空
{
sitename
}
else //否則
{
Hashtable hashtable
if(hashtable
sitename
else
sitename
}
if(sitename
//如果站點名為空值則站點名為默認值http://www
sitename
String s
if(hashtable
writeLog(hashtable
httpservletresponse
}
private void showBanner(Hashtable hashtable
throws IOException
{
String s
File file = new File(s
Vector vector;
if(file == null)
{
vector = new Vector();
}
else
{
String as[] = file
vector = getGraphFiles(as);
}
if(vector
{
httpservletresponse
PrintWriter out = httpservletresponse
out
out
out
return;
}
int i;
if(httpsession != null)
synchronized(hashtable
{
Integer integer;
int j;
if((integer = (Integer)httpsession
j =
else
j = integer
if(j >= vector
j =
i = j;
if(++j >=
j =
httpsession
}
else
i =
String s
String s
if(httpsession != null && (s
{
Hashtable hashtable
if((hashtable
hashtable
hashtable
httpsession
}
if(hashtable
writeLog(hashtable
outputBanner(s
vector = null;
}
private void writeLog(Hashtable logHashtable
{
String logString
String logString
GregorianCalendar gregoriancalendar = new GregorianCalendar();
//獲取當前的時間
gregoriancalendar
String logString
saveLog(hashtable
}
private void writeLog(Hashtable logHashtable
{
String logString
String logString
GregorianCalendar gregoriancalendar = new GregorianCalendar();
gregoriancalendar
String logString
saveLog(logHashtable
}
private void saveLog(Hashtable hashtable
{
synchronized(hashtable
{
try
{
FileWriter filewriter = new FileWriter(s
PrintWriter printwriter = new PrintWriter(filewriter);
printwriter
printwriter
printwriter
filewriter
}
catch(Exception _ex) { }
}
}
private String getLogString(String s
{
String s
//取得遠程的訪問者的IP地址
String s
String s
String s
if(s
s
if(s
s
else
s
if(s
s
else
s
return s
}
private String stringDate(Calendar calendar) //取得時間戳
{
String s = String
String s
if(s
s
s = s + s
s
if(s
s
return s + s
}
private String getFileByUrl(Hashtable hashtable
{
for(Enumeration enumeration = hashtable
//hashtable的keys()方法返回了哈希表關鍵字的枚舉
{
String s
//讓s
if(s
return s
}
return null;
}
private String getFirstSite(Hashtable hashtable)//取得第一個站點的名字
{
String s = (String)hashtable
File file = new File(s);
if(file == null)
return null;
String as[] = file
Vector vector = getGraphFiles(as);
//設置Vector向量變量來獲取圖形文件
if(vector
return null;
else
return getUrl(hashtable
}
private String getUrl(Hashtable hashtable
{
String s
for(Enumeration enumeration = hashtable
{
String s
if(s
return (String)hashtable
}
return null;
}
private void outputBanner(String s
throws IOException//如果有錯則拋出IOException錯誤
{
String s
httpservletresponse
httpservletresponse
httpservletresponse
httpservletresponse
javax
dumpFile(s
servletoutputstream
servletoutputstream
}
private boolean dumpFile(String s
{
byte abyte
boolean flag = true;
try
{
FileInputStream fileinputstream = new FileInputStream(s);
int i;
while((i = fileinputstream
outputstream
fileinputstream
}
catch(Exception _ex)
{
flag = false;
}
return flag;
}
private Vector getGraphFiles(String as[])//獲得圖片文件
{
Vector vector = new Vector();
if(as == null)//如果as為空值
return vector;
for(int i =
{
String s = as[i]
if(isGraphFile(s))//如果為圖片格式
vector
}
return vector;
}
private boolean isGraphFile(String stringFileName) //判斷文件是否為圖形格式
{
int i = stringFileName
if(i <=
return false;
//判斷文件是否以GIF
String stringExtendFileName
return stringExtendFileName
}
private void errorMessage(String s
throws IOException
{
httpservletresponse
PrintWriter out = httpservletresponse
out
out
out
out
out
out
out
out
out
out
}
private String getFromQuery(String strQuery
{
if(strQuery == null)
return
int i;
if((i = strQuery
return
String strQuery
if((i = strQuery
return strQuery
else
return strQuery
}
public String getServletInfo()
{
return
}
private static final String CPR =
private static final String DEFAULT_SITE =
private static final String BANNER_SESSION =
private static final String DIR =
private static final String LOG =
private static final String BANNERINDEX =
private static final String EDITED =
private static final String VIEW_POSTFIX =
private static final String CLICK_POSTFIX =
private static final String CONFIG =
private static final String MODE =
private static final String ID =
private static final int BUFFER_SIZE =
static String separator =
private static Hashtable cfgs;
private static Hashtable logs;
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27253.html