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

一、 获取此次请求的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>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值