一个很奇怪的问题,关于servletContext()调用getRealPath()方法

weixin_39939623 2018-05-19 02:36:23
很简单的一个网页浏览计数器

一共2个文件
一个testBug.java的servlet文件,
一个record.txt文件

结构如下图


1.启动tomcat后浏览器访问,报错500 空指针错误代码为第12行
2.经过测试得出第12行的record文件没有读取到,返回值为null
3.把record.txt文件里的内容(原本是一个0)改为一串数字,比如123456,再次测试,正常了!返回值为123456
4.再把record.txt内容改回0,测试,正常!!!!
5.多次刷新网页后record在浏览器页面显示了正确的浏览次数,但是关闭浏览器,停止tomcat后,从eclipse打开record文件其内容仍然为0.
6.再次启动tomcat,打开浏览器,访问次数仍然在继续累加,而record文件内容依然是0!!

提问:1.在初始值为0--->改为123456--->再改为0,这个过程中发生了什么,导致浏览器不再报500 空指针,而是能够正确读取record.txt的内容?
2.为什么无论计数器怎么累加,原始的record文件内容依然为0?

record.txt的内容就一个0
testBug.java内容如下:
@WebServlet("/testBug")
public class testBug extends HttpServlet {
private static final long serialVersionUID = 1L;

public testBug() {
super();
// TODO Auto-generated constructor stub
}

public void init(ServletConfig config) throws ServletException {
super.init(config);
String filePath = this.getServletContext().getRealPath("record.txt");
FileReader fr = null;
BufferedReader br = null;
try{
fr = new FileReader(filePath);
br = new BufferedReader(fr);
String num = br.readLine();
this.getServletContext().setAttribute("num", num);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void destroy() {
String filePath = this.getServletContext().getRealPath("record.txt");
FileWriter fw= null;
BufferedWriter bw= null;

try{
fw = new FileWriter(filePath);
bw = new BufferedWriter(fw);
String num = (String)this.getServletContext().getAttribute("num");
bw.write(num);
}catch(IOException e){
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();

String num = (String)this.getServletContext().getAttribute("num");
this.getServletContext().setAttribute("num",(Integer.parseInt(num)+1)+"");
String nums = (String)this.getServletContext().getAttribute("num");
out.println("该网页被访问了"+nums+"次");

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}

}

...全文
768 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_39939623 2018-05-20
  • 打赏
  • 举报
回复
引用 1 楼 yjsl__ 的回复:
record.txt是自动创建的,还是手动新建的? 只是改变了内存中的num,txt中没写回去 关闭的时候是强制关闭的,还是用shutdown.bat关的?
record是手动创建的,关闭肯定是shutdown了
yjsl__ 2018-05-19
  • 打赏
  • 举报
回复
record.txt是自动创建的,还是手动新建的? 只是改变了内存中的num,txt中没写回去 关闭的时候是强制关闭的,还是用shutdown.bat关的?
JSF实现文件的下载功能 public static void downloadFile(String path,String fileName) { try { // 获得JSF上下文环境 FacesContext context = FacesContext.getCurrentInstance(); // 获得ServletContext对象 ServletContext servletContext = (ServletContext) context .getExternalContext().getContext(); // 取得文件的绝对路径 String realName = servletContext.getRealPath(path) + "/" + fileName; HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext .getCurrentInstance().getExternalContext().getResponse(); downloadFile(httpServletResponse,realName,fileName); } catch (IOException e) { e.printStackTrace(); } FacesContext.getCurrentInstance().responseComplete(); } public static void downloadFile(HttpServletResponse response,String realName,String fileName) throws IOException { response.setHeader("Content-disposition", "attachment; filename=" + fileName); response.setContentType("application/x-download"); //File exportFile = new File(realName); //response.setContentLength((int) exportFile.length()); ServletOutputStream servletOutputStream = response.getOutputStream(); byte[] b = new byte[1024]; int i = 0; FileInputStream fis = new java.io.FileInputStream(realName); while ((i = fis.read(b)) > 0) { servletOutputStream.write(b, 0, i); } } 使用方法 1、在backing bean的方法调用函数1即可。如Abean中download方法调用了该方法,前台可以这样调用: 或者 2、jsp页面可以这样调用: <% String filename = ""; if (request.getParameter("filename") != null) { filename = request.getParameter("filename"); } try { framewo

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧