Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only part of URL from HttpServletRequest?

Tags:

From the following URL I need to get (http://localhost:9090/dts) alone.
That is I need to remove (documents/savedoc) (OR)
need to get only - (http://localhost:9090/dts)

http://localhost:9090/dts/documents/savedoc   

Is there any method available in request to get the above?

I tried the following and got the result. But still trying.

System.out.println("URL****************"+request.getRequestURL().toString());   System.out.println("URI****************"+request.getRequestURI().toString()); System.out.println("ContextPath****************"+request.getContextPath().toString());  URL****************http://localhost:9090/dts/documents/savedoc   URI****************/dts/documents/savedoc   ContextPath****************/dts 

Can anyone please help me in fixing this?

like image 565
user1732653 Avatar asked Dec 28 '12 05:12

user1732653


People also ask

What is request getRequestURI ()?

The function getRequestURI() returns the complete requested URI. This includes the deployment folder and servlet-mapping string. It will also return all extra path information.

What is request getContextPath ()?

String getContextPath() Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "".

What is the difference between HttpServletRequest and ServletRequest?

ServletRequest provides basic setter and getter methods for requesting a Servlet, but it doesn't specify how to communicate. HttpServletRequest extends the Interface with getters for HTTP-communication (which is of course the most common way for communicating since Servlets mostly generate HTML).


1 Answers

You say you want to get exactly:

http://localhost:9090/dts 

In your case, the above string consist of:

  1. scheme: http
  2. server host name: localhost
  3. server port: 9090
  4. context path: dts

(More info about the elements of a request path can be found in the official Oracle Java EE Tutorial: Getting Information from Requests)
##First variant:###

String scheme = request.getScheme(); String serverName = request.getServerName(); int serverPort = request.getServerPort(); String contextPath = request.getContextPath();  // includes leading forward slash  String resultPath = scheme + "://" + serverName + ":" + serverPort + contextPath; System.out.println("Result path: " + resultPath); 

##Second variant:##
String scheme = request.getScheme(); String host = request.getHeader("Host");        // includes server name and server port String contextPath = request.getContextPath();  // includes leading forward slash  String resultPath = scheme + "://" + host + contextPath; System.out.println("Result path: " + resultPath); 

Both variants will give you what you wanted: http://localhost:9090/dts

Of course there are others variants, like others already wrote ...

It's just in your original question you asked about how to get http://localhost:9090/dts, i.e. you want your path to include scheme.

In case you still doesn't need a scheme, the quick way is:

String resultPath = request.getHeader("Host") + request.getContextPath(); 

And you'll get (in your case): localhost:9090/dts

like image 65
informatik01 Avatar answered Oct 27 '22 00:10

informatik01