This article introduces Java to obtain the request URL and obtain the server root path, and gives examples. Interested friends can learn from the following content.
1. Get the URL of this request
String requestUrl = request.getScheme() //The protocol used by the current link +"://" + request.getServerName()//Server address + ":" + request.getServerPort() //Port number + request.getCon TextPath ( ) // Application name, if the application name is + request.getServletpath () // The relative url + "?" + Request.getQuerystring (); // Request parameters
Example:
http://127.0.0.1:8080/world/index.jsp?name=lilei &Sex=1<Context Path = "World" docBase = "/Home/WebApps" defug = "0" UEST. getScheme () = "http"; request.getservername () = "127.0.0.1"; request.getServerport () = "8080"; Request.getContextPath () = "word"; tSERVLETPATH () = "Index.jsp "; Request.getQueryString () =" name = lilei & sex = 1 "; http://127.0.0.0.1: 8080/world/index.jsp? name = lilei & sex = 1 <context path =" "docbase = "/home/webapps "DEBUG =" 0 "Relaadable =" True "/> Request.getscheme () =" http "; Request.getServername () =" 127.0.0.1 "; request.getServerPort () =" 8080 "; Req UEST.GetContextPath () = "" "; Request.getServletpath () =" World/Index.jsp "; Request.getQueryString () =" name = lilei & sex = 1 ";
2. Get the server root path
<% String Path = Request.getContextPath (); String Basepath = Request.getscheme PATH+"/"; %>
Use as follows:
<head> <link Roth = "Stylesheet" Type = "Text/CSS" HREF = "<%= BASEPATH%> Static/CSS/Framework/Themes/EasYui.CSS"> <linK et "Type = "Text/css" href = "<%= Basepath%> Static/CSS/Framework/themes/Icon.css"> <link R = "styleSheet" type = "text/css" href = "<%= BasePath%%%> STATIC/CSS/BASE.CSS "> <script src =" <%= Basepath%> Static/Javascript/Framework/jquery.min.js "> </script> <Script src =" <%= BasePath%> Stati%> STATI C/ javascript/framework/jquery.easyui.min.min.js "> </script> <script src =" <%= Basepath%> Static/JavaScript/Framework/Easei-Lang-zh_cn.js "> </> </> script> <script src = "<%= Basepath%> Static/Javascript/Framework/EasyUI- Util.js"> </Script> </Head>
The above is the method of Java obtained the request URL and server root path. I hope it will be helpful to everyone's learning.