1.在WebRoot 下面创建文件夹adc,在创建文件adc.txt
2.编写servlet类,实现doGet/doPost 方法
3.重写getLastModified方法,阻止页面刷新是每次都访问服务器,减轻服务器压力
-------补充知识------getLastModified = -1 : 都会访问服务器资源 (200);-getLastModified != -1 :找浏览器缓存资源(304)
public class Demo44 extends HttpServlet {
@Overrideprotected long getLastModified(HttpServletRequest req) {
System.out.println("getLastModified( )");
String path = this.getServletContext().getRealPath("/adc/adc.txtt");
File file = new File(path);
return file.lastModified();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet()");
ServletContext context = this.getServletContext();
InputStream is = context.getResourceAsStream("/adc/adc.txt");
OutputStream os = response.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=is.read(buf))>0){
os.write(buf,0,len);
}
//os.flush();
is.close();
os.close();
}
}