文章分类

相关文章

Java JSP中获取各种路径的方法

分类:Java     发布:2018-12-19     来源:本站     浏览:461 次
测试源码:
找一个jsp页面输入以下代码:
带样式:
<%

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("/"));
out.println("<br/>");
out.println("getQueryString:"+request.getQueryString());
%>
输出结果:
basePath:http://localhost:8080/Interface/ 
getContextPath:/Interface 
getServletPath:/test.jsp 
getRequestURI:/Interface/test.jsp 
getRequestURL:http://localhost:8080/Interface/test.jsp 
getRealPath:F:\15_Java_web\05_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\Interface\ 
getServletContext().getRealPath:F:\15_Java_web\05_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\Interface\ 

带样式:
<%
out.println("<style type='text/css'>");
out.println(".tag{color:gray;}");
out.println(".tag-val{color: brown; font-weight: bold;display: inline-block; margin-left: 5px;}");
out.println("</style>");
out.println("<span class='tag'>request.getContextPath():</span><span class='tag-val'>"+ request.getContextPath() + "</span><br/>");
out.println("<span class='tag'>request.getScheme():</span><span class='tag-val'>" + request.getScheme() + "</span><br/>");
out.println("<span class='tag'>request.getServerName():</span><span class='tag-val'>" + request.getServerName() + "</span><br/>");
out.println("<span class='tag'>request.getServerPort():</span><span class='tag-val'>" + request.getServerPort() + "</span><br/>");
out.println("<span class='tag'>拼接绝对路径:</span><span class='tag-val'>" + request.getScheme() + "://" + request.getServerName()	+ ":" + request.getServerPort() + request.getContextPath() + "/" + "</span><br/>");
out.println("<span class='tag'>request.getContextPath():</span><span class='tag-val'>" + request.getContextPath() + "</span><br/>");
out.println("<span class='tag'>request.getServletPath():</span><span class='tag-val'>" + request.getServletPath() + "</span><br/>");
out.println("<span class='tag'>request.getRequestURI():</span><span class='tag-val'>" + request.getRequestURI() + "</span><br/>");
out.println("<span class='tag'>request.getRequestURL():</span><span class='tag-val'>" + request.getRequestURL() + "</span><br/>");
out.println("<span class='tag'>request.getRealPath(\"/\"):</span><span class='tag-val'>" + request.getRealPath("/") + "</span><br/>");
out.println("<span class='tag'>request.getServletContext().getRealPath(\"/\"):</span><span class='tag-val'>"	+ getServletContext().getRealPath("/") + "</span><br/>");
out.println("<span class='tag'>request.getQueryString():</span><span class='tag-val'>" + request.getQueryString() + "</span><br/>");
%>
运行结果:



上一篇:搞懂字符集和字符编码

下一篇:Java 文件读写辅助类