web工程部署路径与getServletConfig().getRealPath()

我们都知道,通过ServletContext类可以获取工程路径(虚拟路径)以及工程部署在服务器硬盘上的绝对路径

那我们首先应该了解一个工程是怎样部署的,以idea为例

不知道大家有没有发现每当我们启动服务器时,都会有这样一个地址

在这里插入图片描述

这是一个什么地址呢,我们打开发现它的目录内容如下
在这里插入图片描述
有没有熟悉感?我们对比一下Tomcat的目录

在这里插入图片描述

发现了吗,这个路径其实就是IDEA整合Tomcat之后,Tomcat被拷贝的一些副本内容。

进入这个地址的conf/Catalina/localhost目录,我们发现了这样一个文件

在这里插入图片描述

回想一下conf/Catalina/localhost这个路径,我们使用tomcat部署项目的第三种方式是不是也是在这个路径下创建一个.xml的配置文件

显而易见idea部署项目时使用的其实就是第三种tomcat部署项目的方式

因为我们不可能每次创建工程在tomcat的webapps目录下,所以idea会根据我们设置的工程部署路径以及虚拟路径生成出一个配置文件

下图分别为我们设置的虚拟路径以及工程部署路径

在这里插入图片描述
在这里插入图片描述

然后每当我们部署项目时,就会创建我们所设置的工程部署路径,随后我们在网站访问的其实都是该路径下的内容而不是开发环境文件夹下的内容,但两者内容其实一样,工程部署路径的内容是完全拷贝开发环境中web文件夹下的内容

这样就解释了为什么context.getRealPath("/")获得的路径为什么和我们项目创建的路径不一样(因为获取的是工程的部署路径)

public class ContextServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   		//获取配置文件中的上下文参数
        ServletContext context = getServletConfig().getServletContext();
        System.out.println(context.getContextPath());
        System.out.println("当前工程部署的路径" + context.getRealPath("/"));
        System.out.println(context.getRealPath("/css"));
        System.out.println(context.getRealPath("/jpg"));
    }

}

上述程序执行后返回结果如下

/myfirst
当前工程部署的路径E:\eclipse-workspace\Javaweb-servlet\out\artifacts\Javaweb_servlet_war_exploded
E:\eclipse-workspace\Javaweb-servlet\out\artifacts\Javaweb_servlet_war_exploded\css
E:\eclipse-workspace\Javaweb-servlet\out\artifacts\Javaweb_servlet_war_exploded\jpg

context.getContextPath()获得工程路径,故输出/myfirst

context.getRealPath("/")获得参数所对应虚拟路径的真实路径

其中“/”表示根目录,项目工程的根目录,即http://ip:port/工程路径

那么context.getRealPath("/")也就是获得工程的真实路径,即工程部署的物理位置

自然也就是上文配置文件中的docBase值

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
基于JAV的网上商城项目完整源码. import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestContextParamServlet extends HttpServlet { /** * Constructor of the object. */ public TestContextParamServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass() + this.getServletConfig().getServletContext().getInitParameter("a")); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServl

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

得过且过的勇者y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值