首页 > 其他分享 >pageContext.request.contextpath、getServletContext.getRealPath(““)、request.getContextPath()详解以及区别

pageContext.request.contextpath、getServletContext.getRealPath(““)、request.getContextPath()详解以及区别

时间:2024-06-08 23:29:49浏览次数:14  
标签:Web 路径 getContextPath contextPath contextpath request JSP pageContext

在Java EE(Java Enterprise Edition)中开发Web应用时,开发者常常需要获取Web应用的上下文路径和服务器上文件的真实路径。以下是对pageContext.request.contextPathgetServletContext().getRealPath("")request.getContextPath()的详细解释及它们之间的区别:

1. pageContext.request.contextPath

pageContext是JSP内置对象,它提供了对整个JSP页面上下文的访问。通过pageContext,可以获取request对象。

  • 使用场景: 在JSP页面中获取当前Web应用的上下文路径。

  • 语法:

    String contextPath = pageContext.getRequest().getContextPath();
    
  • 返回值: 返回当前Web应用的上下文路径。上下文路径是指应用在服务器上的相对路径,通常是应用在URL中的第一个部分。如果应用部署在根路径下,则返回一个空字符串。

2. getServletContext().getRealPath("")

ServletContext提供了对Web应用的运行时环境的访问。

  • 使用场景: 在Servlet中获取服务器上Web应用的真实路径。

  • 语法:

    String realPath = getServletContext().getRealPath("");
    
  • 返回值: 返回Web应用在服务器文件系统上的绝对路径。getRealPath("")返回的是Web应用根目录在服务器上的绝对路径。注意,如果应用部署在WAR文件中,某些服务器(如Tomcat)可能返回null,因为文件路径并不直接存在于文件系统中。

3. request.getContextPath()

HttpServletRequest接口提供了对客户端请求信息的访问。

  • 使用场景: 在Servlet或JSP中获取当前Web应用的上下文路径。

  • 语法:

    String contextPath = request.getContextPath();
    
  • 返回值: 返回当前Web应用的上下文路径。与pageContext.request.contextPath相同,都是获取应用在URL中的第一个部分。

区别总结

  1. 对象来源不同

    • pageContext.request.contextPath 是通过JSP的pageContext对象获取的,适用于JSP页面。
    • getServletContext().getRealPath("") 是通过Servlet的ServletContext对象获取的,适用于Servlet。
    • request.getContextPath() 是通过HttpServletRequest对象获取的,可以在Servlet或JSP中使用。
  2. 返回值用途不同

    • pageContext.request.contextPathrequest.getContextPath() 返回Web应用的上下文路径,用于生成相对URL或其他与Web应用相关的路径操作。
    • getServletContext().getRealPath("") 返回Web应用在服务器文件系统上的真实路径,用于访问服务器上的文件系统中的文件。
  3. 使用环境

    • pageContext.request.contextPath 主要在JSP页面中使用。
    • getServletContext().getRealPath("") 主要在Servlet中使用,但也可以在JSP的脚本中使用。
    • request.getContextPath() 在Servlet和JSP中均可使用。

示例

JSP 页面中获取上下文路径
<% 
String contextPath = pageContext.getRequest().getContextPath();
out.println("Context Path: " + contextPath);
%>
Servlet 中获取上下文路径和真实路径
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String contextPath = request.getContextPath();
    String realPath = getServletContext().getRealPath("");

    response.getWriter().append("Context Path: ").append(contextPath)
                        .append("\nReal Path: ").append(realPath);
}

通过上述解释和示例,可以更好地理解pageContext.request.contextPathgetServletContext().getRealPath("")request.getContextPath()的作用和区别。

标签:Web,路径,getContextPath,contextPath,contextpath,request,JSP,pageContext
From: https://blog.csdn.net/2301_79858914/article/details/139552908

相关文章

  • docker拉取镜像报错Bad Request
    问题现象docker拉取镜像报错BadRequest。问题分析cat/etc/systemd/system/docker.service.d/http-proxy.conf1.1.1.1:80模拟错误的docker代理配置,错误的代理导致镜像拉取失败。解决问题注释代理配置。vim/etc/systemd/system/docker.service.d/http-proxy.conf重启d......
  • HTTP Status 400 – Bad Request
    1.问题2.原因org.apache.juli.logging.DirectJDKLog:log|ErrorparsingHTTPrequestheaderNote:furtheroccurrencesofHTTPheaderparsingerrorswillbeloggedatDEBUGlevel.java.lang.IllegalArgumentException:Requestheaderistoolargeat......
  • HttpContext探究之RequestServices
    HttpContext探究之RequestServices在一篇随笔中提到了中间件的构造方式,主要有两种,第一种是直接从容器里面获取,第二种是构造函数的参数从容器里面获取,这两者都离不开容器,也就是serviceprovide,而RequestService则是里面重要的内容RequestServices是什么HttpContext.RequestServi......
  • 爬虫中关于SSL证书的处理(requests库)
    SSL证书是方法一:暴力verify=Falseresponse=requests.get('https://example.com',verify=False)#强烈建议不要在生产环境中使用verify=False,#因为它会使你的请求容易受到中间人攻击(Man-in-the-Middle,MITM)。#当SSL证书验证被绕过时,任何位于你和目标服务器之间......
  • finishActivity (int requestCode)
    publicvoidfinishActivity(intrequestCode)Since:APILevel1ForcefinishanotheractivitythatyouhadpreviouslystartedwithstartActivityForResult(Intent,int).ParametersrequestCodeTherequestcodeoftheactivitythatyouhadgiventostartActivit......
  • Python从0到100(二十九):requests模块处理cookie
    1爬虫中使用cookie为了能够通过爬虫获取到登录后的页面,或者是解决通过cookie的反扒,需要使用request来处理cookie相关的请求1.1爬虫中使用cookie的利弊带上cookie的好处能够访问登录后的页面能够实现部分反反爬带上cookie的坏处一套cookie往往对应的是一个用户......
  • requestAnimationFrame使用介绍
    概述requestAnimationFrame是根据帧数来执行回调函数的,就是屏幕一帧,那requestAnimationFrame就会执行一次。一般屏幕是60帧,也就是一秒执行60次回调函数.性能相对定时器settimeout好,因为定时器执行权限在同步任务微任务之后,会受到其他任务影响。requestAnimationFrame......
  • Python从0到100(三十):requests模块的其他方法
    1requests中cookirJar的处理方法使用request获取的resposne对象,具有cookies属性,能够获取对方服务器设置在本地的cookie,但是如何使用这些cookie呢?1.1方法介绍response.cookies是CookieJar类型使用requests.utils.dict_from_cookiejar,能够实现把cookiejar对象转化为字典......
  • @RequestMapping注解有哪些属性?
    在SpringFramework中,@RequestMapping注解用于将HTTP请求映射到MVC和REST控制器的处理方法上。它是SpringMVC中最基本的注解之一,可以应用在类级别或方法级别。@RequestMapping注解拥有多个属性,以下是其中的一些常用属性及其解释:value/path:类型:String[]描......
  • 06.爬虫---urllib与requests请求实战(POST)
    06.urllib与requests请求实战POST1.Urllib模块2.Requests模块3.实战(Requests)POST请求Python中的POST请求是HTTP协议中的一种请求方法,用于向服务器提交数据。与GET请求不同,POST请求将数据封装在请求体中,而不是在URL中传递。通常情况下,POST请求用于向服务器提交表单......