1. 程式人生 > >jsp中檔案路徑的幾種形式

jsp中檔案路徑的幾種形式

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

out.println("basePath:"+basePath);
out.println("<br/>");
out.println("getContextPath:"+request.getContextPath());
out.println("<br/>");
out.println("getServletPath:"+request.getServletPath());
out.println("<br/>");
out.println("getRequestURI:"+request.getRequestURI());
out.println("<br/>");
out.println("getRequestURL:"+request.getRequestURL());
out.println("<br/>");
out.println("getRealPath:"+request.getRealPath("/"));
out.println("<br/>");
out.println("getServletContext().getRealPath:"+getServletContext().getRealPath("/"));
%>

demo.jsp顯示:

basePath:http://localhost:8080/demo/

getContextPath:/demo
getServletPath:/demo.jsp
getRequestURI:/demo/demo.jsp
getRequestURL:http://localhost:8080/demo/demo.jsp
getRealPath:D:\Tomcat 6.0\webapps\demo\
getServletContext().getRealPath:G:\Tomcat 7.0\webapps\demo\