1. Get it in Servlet
import ; import ; import ; import ; public class UrlDemoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // Get a similar ":8080/app/path/to/servlet" StringBuffer url = (); // If you want the query string after the question mark, such as "?a=1&b=2" String queryString = (); // May be null String fullUrl = (queryString == null) ? () : ('?').append(queryString).toString(); ().write("Full URL: " + fullUrl); } }
()
returnStringBuffer
, including protocol, server name, port, context path, and servlet path.()
Return to URL?
The latter part (excluding?
self), if there are no query parameters, it isnull
。
2. Get it in Spring MVC controller
import ; import ; import ; import ; @Controller public class UrlDemoController { @GetMapping("/demo") @ResponseBody public String demo(HttpServletRequest request) { StringBuffer url = (); String qs = (); return (qs == null) ? () : ('?').append(qs).toString(); } }
Spring will automaticallyHttpServletRequest
Injected into method parameters, the usage is the same as that of Servlet.
3. Get it in the filter (Filter)
import .*; import ; import ; public class UrlLoggingFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; StringBuffer url = (); String qs = (); String fullUrl = (qs == null) ? () : ('?').append(qs).toString(); ("Incoming request URL: " + fullUrl); (req, res); } }
Filters are often used for log printing, authentication, and unified processing.
4. Get more detailed parts
()
:protocol(http
orhttps
)()
: Hostname or IP()
: Port number()
: Application context path (depending on deploymentapp
name)()
: Servlet map path()
: Additional path information()
: Query string (excluding?
)
If you need to get the original requested host name and protocol after the reverse proxy (Nginx, F5), you must combine itX-Forwarded-Proto
/ X-Forwarded-Host
Wait for the head to reorganize:
String proto = ("X-Forwarded-Proto"); if (proto == null) proto = (); String host = ("X-Forwarded-Host"); if (host == null) host = (); String port = ("X-Forwarded-Port"); if (port == null) port = (()); String fullUrl = proto + "://" + host + (() ? "" : ":" + port) + () + (qs == null ? "" : "?" + qs);
5. Client (HTTP client library) get request URL
If you are in the client code (such as using Apache HttpClient, OkHttp) you want to get the request URL you sent:
- Apache HttpClient
HttpGet get = new HttpGet("/data?x=1"); (()); // Output URI Object
- OkHttp
Request req = new () .url("/data?x=1") .build(); (()); // Object
The above is the method of "Java: Get Request URL Address" in various common scenarios, just choose as needed.
This is the article about several common methods for obtaining Java request URL address. For more related contents of Java request URL address, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!