在阅读有关 servlet 的这本书时,我遇到了一个示例,它使用 FileWriter 类在 servlet 被销毁之前保存它的持久状态。测试了示例并且工作正常,这是代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InitDestroyCounter extends HttpServlet {
int count;
public void init(ServletConfig config) throws ServletException {
// Always call super.init(config) first (servlet mantra #1)
super.init(config);
// Try to load the initial count from our saved persistent state
try {
FileReader fileReader = new FileReader("InitDestroyCounter.initial");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String initial = bufferedReader.readLine();
count = Integer.parseInt(initial);
return;
}
catch (FileNotFoundException ignored) { } // no saved state
catch (IOException ignored) { } // problem during read
catch (NumberFormatException ignored) { } // corrupt saved state
// No luck with the saved state, check for an init parameter
String initial = getInitParameter("initial");
try {
count = Integer.parseInt(initial);
return;
}
catch (NumberFormatException ignored) { } // null or non-integer value
// Default to an initial count of "0"
count = 0;
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
count++;
out.println("Since the beginning, this servlet has been accessed " +
count + " times.");
}
public void destroy() {
saveState();
}
public void saveState() {
// Try to save the accumulated count
try {
FileWriter fileWriter = new FileWriter("InitDestroyCounter.initial");
String initial = Integer.toString(count);
fileWriter.write(initial, 0, initial.length());
fileWriter.close();
return;
}
catch (IOException e) { // problem during write
// Log the exception.
}
}
}
停止 glassfish 服务器后,我从部署文件夹复制/粘贴 war 文件,解压缩并查找“InitDestroyCounter.initial”文件,但在其中找不到,所以我想知道 glassfish 将这个创建的文件保存在哪里?