This article describes Java to obtain this request URL and get the server root path method, and for example, interested friends can learn from the content below.
First, get the URL of this request
String Requesturl = Request.getscheme ()//The current link uses the protocol
+ "://" + request.getservername ()//server address
+ ":" + Request.getserverport ()//port number
+ Request.getcontextpath ()//Application name, if the application name is
+ request.getservletpath ()// Requested 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" 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";
Second, get the server root path
<%
String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
Use the following:
The above is the Java to obtain this request URL and server root path method, hope for everyone's learning help.