Java obtains this request URL as well as the server root path method _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.