1.service方法重写后,不会执行doget/dopost方法
2.this.getServletContext().getRealPath("/WEB-INF/count.txt");会有平台差异,不建议使用
3.BufferedReader的mark,reset方法结合使用,可重新回到mark下一行重新读文件(好像要在执行reader)
4.OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");设置编码防止乱码
5.代码76行out.write(c.getCount()+"");getCount()返回int型,没加""转为String时一直写入读出错误,这个真的是巨坑,但也是自己基础不扎实罗。
6.文件writer要正确close才能关闭,close有flush的作用。
7. 实际项目部署在workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\WebTest目录下,所以destory方法修改文件后,eclipse里的count.txt未改变
参见:https://blog.csdn.net/u010089444/article/details/53281513 等博客
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.Counter;
/**
* Servlet implementation class CounterServlet
*/
@WebServlet("/CounterServlet")
public class CounterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CounterServlet() {
super();
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
String path = this.getServletContext().getRealPath("/WEB-INF/count.txt");
try {
File file=new File(path);
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
BufferedReader in = new BufferedReader(isr);
in.mark(100000000);
String count=in.readLine();
in.reset();
in.close();
isr.close();
fis.close();
//System.out.println(count);
if(count!=null&&!count.equals("")){
Counter c=new Counter(Integer.parseInt(count));
System.out.println("容器初始化CounterServlet时从count.txt中读入的值:" + c.getCount());
this.getServletContext().setAttribute("count",c.getCount()+"");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
String path = this.getServletContext().getRealPath("/WEB-INF/count.txt");
try {
File file=new File(path);
FileOutputStream fos=new FileOutputStream(file,false);
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
BufferedWriter out = new BufferedWriter(osw);
String count = (String) this.getServletContext().getAttribute("count");
if(count!=null&&!count.equals("")){
Counter c=new Counter(Integer.parseInt(count));
out.write(c.getCount()+"");
System.out.println("执行destroy方法,将count写入count.txt,值为:" + c.getCount());
}
fos.flush();
osw.flush();
out.flush();
out.close();
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String count=(String) this.getServletContext().getAttribute("count");
if(count!=null&&!count.equals("")){
Counter c=new Counter(Integer.parseInt(count)+1);
this.getServletContext().setAttribute("count",c.getCount()+"");
System.out.println("访问一次页面后count值:" + c.getCount());
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}