Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get full URL and query string in Servlet for both HTTP and HTTPS requests

I am writing a code which task is to retrieve a requested URL or full path. I've written this code:

HttpServletRequest request;//obtained from other functions String uri = request.getRequestURI(); if (request.getQueryString() != null)     uri += "?" + request.getQueryString(); 

So, when I browse http://google.com?q=abc it is OK (correct). But there is problem when I browse https://google.com. The value of uri is http://google.com:443google.com:443, So the program doesn't only when HTTPS is used.

And the output is same for request.getRequestURL().toString().

What is the solution?

like image 260
progrrammer Avatar asked May 21 '13 16:05

progrrammer


People also ask

How to get URL in servlet?

Method 1: String url = request. getRequestURL(). toString();

How servlets can receive parameters through servletrequest object?

Other types of servlets can receive their parameters in other ways. getParameter() returns the value of the named parameter as a String or null if the parameter was not specified. The value is guaranteed to be in its normal, decoded form. If the parameter has multiple values, the value returned is server-dependent.

What is getQueryString?

The getQueryString() method is defined in the HttpServletRequest interface, which is used to retrieve the query string of the HTTP request. A query string is the string on the URL to the right of the path to the servlet. Using this a programmer can know the data which is sent from the client(when a form is submitted)


2 Answers

By design, getRequestURL() gives you the full URL, missing only the query string.

In HttpServletRequest, you can get individual parts of the URI using the methods below:

// Example: http://myhost:8080/people?lastname=Fox&age=30  String uri = request.getScheme() + "://" +   // "http" + "://              request.getServerName() +       // "myhost"              ":" +                           // ":"              request.getServerPort() +       // "8080"              request.getRequestURI() +       // "/people"              "?" +                           // "?"              request.getQueryString();       // "lastname=Fox&age=30" 
  • .getScheme() will give you "https" if it was a https://domain request.
  • .getServerName() gives domain on http(s)://domain.
  • .getServerPort() will give you the port.

Use the snippet below:

String uri = request.getScheme() + "://" +              request.getServerName() +               ("http".equals(request.getScheme()) && request.getServerPort() == 80 || "https".equals(request.getScheme()) && request.getServerPort() == 443 ? "" : ":" + request.getServerPort() ) +              request.getRequestURI() +             (request.getQueryString() != null ? "?" + request.getQueryString() : ""); 

This snippet above will get the full URI, hiding the port if the default one was used, and not adding the "?" and the query string if the latter was not provided.


Proxied requests

Note, that if your request passes through a proxy, you need to look at the X-Forwarded-Proto header since the scheme might be altered:

request.getHeader("X-Forwarded-Proto") 

Also, a common header is X-Forwarded-For, which show the original request IP instead of the proxys IP.

request.getHeader("X-Forwarded-For") 

If you are responsible for the configuration of the proxy/load balancer yourself, you need to ensure that these headers are set upon forwarding.

like image 199
acdcjunior Avatar answered Sep 29 '22 10:09

acdcjunior


Simply Use:

String Uri = request.getRequestURL()+"?"+request.getQueryString(); 
like image 42
sumitkanoje Avatar answered Sep 29 '22 09:09

sumitkanoje