ServletContext.getRealPath()方法的功能说明
下文笔者讲述ServletContext.getRealPath()方法的功能简介说明,如下所示
ServletContext.getRealPath()方法的示例
getRealPath()方法简介
ServletContext.getRealPath()方法的功能: 返回虚拟路径对应的磁盘物理路径 ServletContext.getRealPath()方法的语法: String javax.servlet.ServletContext.getRealPath(String path) path:表示虚拟路径 String:返回虚拟路径对应的物理路径例:
ServletContext.getRealPath()方法的示例
package com.java265; 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 TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = request.getServletContext(); String path1 = servletContext.getRealPath(""); System.out.println(path1); String path2 = servletContext.getRealPath("/imgs"); System.out.println(path2); String path3 = servletContext.getRealPath("/WEB-INF"); System.out.println(path3); } } -----运行以上代码,将输出以下信息----- C:\Program Files\Tomcat 9.0\webapps\RealPath\ C:\Program Files\Tomcat 9.0\webapps\RealPath\imgs C:\Program Files\Tomcat 9.0\webapps\RealPath\WEB-INF
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。