0
点赞
收藏
分享

微信扫一扫

ServletContext功能 获取文件真实路径

林塬 2022-03-16 阅读 54

配置文件的位置

  1. src目录下
  2. Web目录下
  3. WEB-INF目录下
    在这里插入图片描述
@WebServlet("/servletContextDemo5")
public class ServletContextDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取httpServlet对象
        ServletContext context = this.getServletContext();

        //获取文件的服务器路径
        String realPath = context.getRealPath(".b.txt");
        System.out.println(realPath);
        File file=new File(realPath);
    }

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

在这里插入图片描述

举报

相关推荐

0 条评论