jsp获取相对路径网址的方法 request.getContextPath()

1. 在jsp页面中取得网站的首页的路径

<%
 String appContext = request.getContextPath();
 Integer prot=request.getServerPort();
 String basePath ="";
 if(prot==80)   //80为默认端口
  basePath = request.getScheme()+"://"+request.getServerName()+ appContext;
 else
  basePath = request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + appContext;
%>

2. 在超链接中的写法

<a title="首  页" href="<%=basePath %>" target="_self">首  页</a>

<a title="美体" href="<%=basePath %>/fat"   target="_self">美体</a>

3. 在浏览器html中显示的为:

<a title="首  页" href="http://www.txw100.com" target="_self">首  页</a>

<a title="美体" href="http://www.txw100.com/fat"   target="_self">美体</a>

回复   TOP
posted @ 2012-11-08 01:46  wj-conquer  阅读(460)  评论(0编辑  收藏  举报