Get the current project name

Java side

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
	// 获取当前工程名,返回 /工程名
	String projectName = req.getContextPath();
	System.out.println(projectName);
}

Web

JSP syntax

<% out.println(request.getContextPath()); %><br>
<%=request.getContextPath() %><br>

  Another built-in objects JSP is pageContext object can get the request object with getRequest () method, so I wanted to try to use pageContext.getRequest().getContextPath()this method to get the project name, but then try to find the error, the error is not getContextPath in ServletRequest class ( ) this way.
  The reason is that pageContext.getRequest()the returned object type ServletRequest, and getContextPath () method is defined in the HttpServletRequest class, so getContextPath not found () method.
  Mention it, pageContext.getRequest().equals(request)it returns true, because the pageContext.getRequest()acquisition of ServletRequest HttpServletRequest object is actually request this transformation come up, so although they are of different types, but it is the same object, it calls equals () method returns true results .

EL expression

${pageContext.request.contextPath}<br>

Guess you like

Origin blog.csdn.net/qq_40395874/article/details/114466350