web工程中和路径资源相关的方法及测试

httpreservlet中和路径相关的方法
request.getContextPath();
返回请求使用的实际上下文路径
request.getServletPath();
request.getRequestURL();
request.getRequestURI();
ServletContext中和路径相关的方法
servletContext servletContext = getServletContext();

servletContext.getContextPath();
servletContext.getRealPath("/");
servletContext.getREsourcePaths("/");

补充1:测试代码

package GQ.work;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServletPath extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public TestServletPath() {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        System.out.println("request.getContextPath()"+request.getContextPath());
        System.out.println("request.getServletPath()"+request.getServletPath());
        System.out.println("request.getRequestURL()"+request.getRequestURL());
        System.out.println("request.getRequestURI()"+request.getRequestURI());
        ServletContext context = getServletContext();
        System.out.println("getServletContext()"+context);
        System.out.println("context.getContextPath()"+context.getContextPath());
        System.out.println("context.getRealPath('/')"+context.getRealPath("/"));
        System.out.println("context.getResourcePaths('/')"+context.getResourcePaths("/"));
    }
}

补充2: 测试结果及说明
①request.getContextPath()
/JD1706_Servlet
获得上下文路径
②request.getServletPath()
/TestServletPath
返回该请求使用的实际上下文路径
③request.getRequestURL()
http://localhost:8088/JD1706_Servlet/TestServletPath
获得浏览器地址
④request.getRequestURI()
/JD1706_Servlet/TestServletPath
项目中的路径
⑤getServletContext()
org.apache.catalina.core.ApplicationContextFacade@608061bd
上下文对象
⑥context.getContextPath()
/JD1706_Servlet
上下文路径
⑦context.getRealPath(‘/’)
E:\Study\javaee\apache-tomcat-6.0.18\webapps\JD1706_Servlet\
获得一个绝对路径/代表项目名后的/
⑧context.getResourcePaths(‘/’)
[/META-INF/, /download/, /WEB-INF/, /dabang.jpg, /zhaohongli.jpg, /form/, /upload/]
获得一个路径下的所有资源

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值