GetURI shows JSP name not browser url?

I am trying to pull the URL being viewed in the address bar of a browser into a Google Analytics tag. I used

<% String getURI = request.getRequestURI (); %>

but it shows me the name / path of the JSP file, not what is in the browser.

Am I doing this wrong or is this what is expected, since I am doing this in the Developer ToolKit in a virtual machine? I looked through the forums and looked at Google, but I can not find the answer to my question, nothing seems to be considering the possibility of it working differently in the local environment, so I think I did something wrong.

+4
source share
2 answers

If the JSP was redirected by the front controller, then HttpServletRequest#getRequestURI() will actually return the JSP URI instead of the original URI as the initial client request (as seen in the address bar of the browser).

In the case of forwarding, the original request URI is available as a request attribute with the key identified by RequestDispatcher#FORWARD_REQUEST_URI , which is javax.servlet.forward.request_uri .

So this should do:

 String getURI = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); 

Or, when you're still on Servlet 2.5 or older (this constant was introduced in Servlet 3.0):

 String getURI = request.getAttribute("javax.servlet.forward.request_uri"); 

By the way, it is available in JSP EL as follows:

 ${requestScope['javax.servlet.forward.request_uri']} 
+1
source

Yes, this is what he should do. Use request.getRequestUrl (), this will give you the full one (http: //www...../xyz.jsp)

Change See description for two methods http://docs.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletRequest.html

Edit 2 therefore, in short, your request is that you need the file name of the tge file in which you are located.

Try something like

 request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/")) 

This will give you the string welcome.html.if if you are inside welcome.html you can add '/'. Not sure if there is a direct search method for what you are looking for

0
source

Source: https://habr.com/ru/post/1437427/


All Articles