JSP获取当前路径
null | 阅5902 | 来源:好空间网络 [打印] [关闭] |
很多用户会问怎么知道我在服务器上的绝对路径呢,大家把现在这个文件下回去保存为jsp的后缀后上传到空间里浏览下他就会告诉你你在我们服务器上的绝对路径了
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Untitled Document</title>
</head>
<body>
当前WEB应用的物理路径:<%=application.getRealPath("/")%>
当前你求请的JSP文件的物理路径:<%=application.getRealPath(request.getRequestURI())%>
<%String path=application.getRealPath(request.getRequestURI());
String dir=new File(path).getParent();
out.println("当前JSP文件所在目录的物理路径:"+dir);
String realPath1 = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);
out.println("web URL 路径:"+realPath1);
%>
</body>
</html>