jsp網頁計數器實現樣本_JSP編程

來源:互聯網
上載者:User
複製代碼 代碼如下:

//過濾器類
public class EcondingFilter implements Filter {
private String charset = null;
private ServletContext context = null;
private String path = "";
/**
* 在銷毀前將資料存入本地檔案中
*/
public void destroy() {
//擷取servleContext中的屬性的那個值
String nums = (String) context.getAttribute("nums");
//建立寫入流
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(path);
bw = new BufferedWriter(fw);
bw.write(nums);
} catch (Exception e) {
e.printStackTrace();
} finally {

try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
System.out.println("filter銷毀");
}

複製代碼 代碼如下:

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("doFilter前");
String path = ((HttpServletRequest)request).getServletPath();//擷取每次訪問的action的相對路徑
<img alt="" src="http://img.blog.csdn.net/20130728233435953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2tyZ3diag==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"><img alt="" src="http://img.blog.csdn.net/20130728233445625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2tyZ3diag==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"> //判斷路徑,如果是登陸的那個action,就讓儲存的context裡面的那個屬性加1
if(path.endsWith("/login.action")){
context.setAttribute("nums",Integer.parseInt(context.getAttribute("nums").toString())+1+"");
}
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
chain.doFilter(request, response);
System.out.println("doFilter後");

}

複製代碼 代碼如下:

public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("filter初始化");
//擷取編碼格式
charset = filterConfig.getInitParameter("encoding");
//擷取servletContext
context = filterConfig.getServletContext();
System.out.println(charset);

path = context.getRealPath("");
File file = new File("D:\\text.txt");
if (!file.exists()) {//判斷檔案是否存在
// 如果檔案不存在,就建立一個檔案,儲存在D盤中
file = new File("d:\\text.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(0 + "");// 寫入初始化資料0
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}
}
//當每次tomcat啟動服務時,進行讀取建立的那個檔案
path = "d:\\text.txt";
// 從本地讀取存取的人數的檔案
FileReader fr = null;
BufferedReader bf = null;
String nums = "";
try {
fr = new FileReader(path);
bf = new BufferedReader(fr);
nums = bf.readLine();
System.out.println(nums);
} catch (Exception e) {
e.printStackTrace();
} finally {

try {
if (bf != null) {
bf.close();
}
if (fr != null) {
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//將獲得到的資料儲存在servletContext中
context.setAttribute("nums", nums);
}

}

用過濾器方便的一點,不需要我們每次手動去調用,當web服務啟動時候,自動會引用。首先說下,我寫到init方法的依據是,每次web服務啟動會調用一次init方法,當關閉服務的時候會調用一次destory方法,將計數的那個資料檔案,這個方法寫到init方法和destory方法,這樣可以減少每次的不斷的讀取伺服器和讀取寫入檔案的次數,當我們每登陸一次,就讓servletContext中的那個attr加1,從而實現當關閉服務的時候,把檔案儲存在磁碟中。下次從磁碟中讀取。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.