${pageContext.request.contextPath}不能识别的问题

${pageContext.request.contextPath} 是通过 get方法去取的,先 pageContext.getRequest() 得到 HttpServletRequest 对象,再调用 HttpServletRequest 的 getContextPath方法

作用是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的。

 

El表达式的写法:

${pageContext.request.contextPath}

 

jsp的写法:

<%=request.getContextPath()%>

--------------------------------------------------------------------------------------

${pageContext.request.contextPath}不能识别的问题

原因:

idea生成的web.xml 的web-app版本居然是2.3,2.3之后的版本才支持el表达式

 

解决方法:

如果运行这一段代码的时候报错,请检查项目 web.xml 文件的开头是这样

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
</web-app> 
改完记得重启


REF
https://blog.csdn.net/u012527870/article/details/71330754
posted @ 2021-12-24 19:59  emanlee  阅读(385)  评论(0编辑  收藏  举报