Servert context的常用方法

Servert context的常用方法

getInitparameter(String Name) 根据名称范围全局配置参数
getContextpath() 获取应用的虚拟目录
getRealpath()根据虚拟目录获取绝对路径
获取路径根据位置不同也可以用字节码的方式以下就列举根据虚拟目录获取绝对路径的代码有不同的方式可以参考,自己写的仅供参考

public class ServitContext1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = this.getClass().getClassLoader().getResource("a.txt").getPath();
        System.out.println(path);
        ServletContext servletContext = getServletContext();
        String realPath3 = servletContext.getRealPath("/WEB-INF/classes/a.txt");
        System.out.println(realPath3);
        System.out.println("---------------------------------------");
        String path1 = this.getClass().getResource("b.txt").getPath();
        System.out.println(path1);
        String realPath = servletContext.getRealPath("WEB-INF/classes/com.wo.code/b.txt");
        System.out.println(realPath);
        System.out.println("--------------------------------------------------");
        String realPath1 = servletContext.getRealPath("c.txt");
        System.out.println(realPath1);
        System.out.println("-----------------------------------------");
        String realPath2 = servletContext.getRealPath("/WEB-INF/d.txt");
        System.out.println(realPath2);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值