码迷,mamicode.com
首页 > 编程语言 > 详细

【Java】获取此次请求URL,获取服务器根路径

时间:2015-08-27 23:15:03      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:java   request   获取跟路径   获取请求url   tomcat   

一、 获取此次请求的URL

**String requestUrl = request.getScheme() //当前链接使用的协议
        +"://" + request.getServerName()//服务器地址  
        + ":" + request.getServerPort() //端口号  
        + request.getContextPath() //应用名称,如果应用名称为
        + request.getServletPath() //请求的相对url  
        + "?" + request.getQueryString(); //请求参数**

举例:

http://127.0.0.1:8080/world/index.jsp?name=lilei&sex=1
<Context path="world" docBase="/home/webapps" debug="0" reloadable="true"/>

request.getScheme() = "http";
request.getServerName() = "127.0.0.1";
request.getServerPort() = "8080";
request.getContextPath() = "world";
request.getServletPath() = "index.jsp";
request.getQueryString() = "name=lilei&sex=1";

http://127.0.0.1:8080/world/index.jsp?name=lilei&sex=1
<Context path="" docBase="/home/webapps" debug="0" reloadable="true"/>

request.getScheme() = "http";
request.getServerName() = "127.0.0.1";
request.getServerPort() = "8080";
request.getContextPath() = "";
request.getServletPath() = "world/index.jsp";
request.getQueryString() = "name=lilei&sex=1";

二、获取服务器根路径

<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>

使用如下:

<head>
<link rel="stylesheet" type="text/css" href="<%=basePath%>static/css/framework/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>static/css/framework/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>static/css/base.css">
<script src="<%=basePath%>static/javascript/framework/jquery.min.js"></script>
<script src="<%=basePath%>static/javascript/framework/jquery.easyui.min.js"></script>
<script src="<%=basePath%>static/javascript/framework/easyui-lang-zh_CN.js"></script>
<script src="<%=basePath%>static/javascript/framework/easyui-util.js"></script>
</head>

版权声明:本文为博主原创文章,未经博主允许不得转载。

【Java】获取此次请求URL,获取服务器根路径

标签:java   request   获取跟路径   获取请求url   tomcat   

原文地址:http://blog.csdn.net/claram/article/details/48035207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!