ファイルパス
JavaEEの場合
tomcat
|-webapps
|-myapp
|-WEB-INF
|-jsp
|-login.jsp
|-img
|-logo.gif
>ブラウザから見たパス
login.jsp http://localhost/myapp/loginDisp.do
logo.gif http://localhost/myapp/img/logo.gif
login.jsp
<img src="img/logo.gif" />
>サーバから見たパス
コンテキストルート
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
★Web Path
project
|-dir1
|-c.jsp
|-dir2
|-a.jsp
|-b.jsp
JSP中のパス
相対パス
<a href="b.jsp">xxx</a>或いは<a href="../dir2/b.jsp">xxx</a>
<a href="../dir1/c.jsp">xxx</a>
絶対パス
/の意味は http://localhost:8080
<a href="/project/dir2/b.jsp">xxx</a>
<a href="/project/dir1/c.jsp">xxx</a>
或いは
<%=request.getContextPath()%>/dir1/c.jsp
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<script>var ctx = "${ctx}";</script>
src/main/resources/META-INF/resources/error/main.jsp
<img src="${pageContext.request.contextPath}/error/images/logo.png" />
Servlet中のパス
●Forwardの場合
/の意味は http://localhost:8080/project
RequestDispatcher rd = request.getRequestDispatcher("/dir1/c.jsp");
●Redirectの場合
/の意味は http://localhost:8080
request.getContextPath()の意味は http://localhost:8080/project
response.sendRedirect(request.getContextPath() + "/dir1/c.jsp");
web.xml中のパス
url-mapping
/の意味は http://localhost:8080/project
★リソースファイル
//ファイルはWEB-INF/classesの下に
方法1
String filename = "/WEB-INF/classes/test.txt";
filename = getServletContext().getRealPath(filename);
this.readfile(filename);
方法2
Class c = MyServlet.class;
String filename = c.getResource("/test.txt").getFile();
this.readfile(filename);
//ファイルはWebRootの下に
String filename = "/test.txt";
filename = getServletContext().getRealPath(filename);
this.readfile(filename);
private static void readfile(String filename) throws IOException{
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
while((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
PHPの場合
スクリプトのディレクトリ include_once dirname(__FILE__).'/xxx/yyy.php';
ドキュメントルート include_once $_SERVER["DOCUMENT_ROOT"].'/xxx/yyy.php';
★ページの絶対URLを取得
JavaScript location.href
Java request.getRequestURL() + "?" request.getQueryString()
PHP 'http://'.$_SERVER["SERVER_NAME"].
$_SERVER["SCRIPT_NAME"].'?'.$_SERVER["QUERY_STRING"]