?? jsq.java
字號:
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class jsq extends HttpServlet
{
/* 這是一個網頁計數器 */
static int count = 0;
static String currentRecord = null; //保存文本的變量
static BufferedReader file;
static String path;
public void doGet(HttpServletRequest rq,HttpServletResponse rp)
{
try
{
rp.setContentType("text/html");
PrintWriter out = rp.getWriter();
int local_count1 = 0;
synchronized(this)
{
/* 協調進程之間的同步關系 */
local_count1 = ++count;
}
String y = ReadFile();
String z = Integer.toString(local_count1);
int local_count2 = Integer.parseInt(y); //將字符串轉換為整型
int d = (local_count1 > local_count2?local_count1:local_count2);
String p = Integer.toString(d); //將整型轉換為字符串
WriteFile(p);
out.println("<html>");
out.println("<body bgcolor=\"#FFFFFF\">");
out.println("<br><br>");
out.println("<center><b><font color=\"#6666FF\" face=\"幼圓\" size=\"5\">歡迎登陸JAVA Servlet計數器測試頁面!</font></b></center>");
out.println("<br><center><b><font color=\"#6666FF\" face=\"幼圓\" size=\"5\">距網頁開通至今,已被訪問了 " + p + " 次</font></b></center>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public static String ReadFile()
{
/* 本方法是將原先的點擊數從文件中讀出來,并且進行累加 */
String returnStr =null;
try
{
path = "E:\\jsq\\jsqfile.txt";
file = new BufferedReader(new FileReader(path)); //實例一個讀文件對象和一個緩存對象
currentRecord = file.readLine();
}
catch (IOException e)
{
System.out.println("讀取數據錯誤.");
}
if (currentRecord == null)
{
returnStr = "沒有任何記錄";
}
else
{
returnStr =currentRecord;
}
return returnStr;
}
public static void WriteFile(String counter)
{
/* 本方法是將當前的點擊數保存到文件中,用以下一次的累加 */
path = "E:\\jsq\\jsqfile.txt";
int Writestr = Integer.parseInt(counter)+1;
try
{
PrintWriter pw = new PrintWriter(new FileOutputStream(path)); //實例化一個文件輸出流對象
String writerstr=Integer.toString(Writestr);
pw.print(writerstr);
pw.close();
}
catch(IOException e)
{
System.out.println("寫入文件錯誤"+e.getMessage());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -