-1

I have the following jsp where I need to modify a specific part of my current url

String path = request.getRequestURL().toString();
...
response.sendRedirect("https://new.user.SAME.SAME/main.jsp");

I would like to take the XXX part of my current url and put it in the sendRedirect link.

For an example, if I am currently on https://old.user.domain.com/main.jsp, then the new link should be https://new.user.domain.com/main.jsp.

The only thing constantly changes is the user part. I currently use response.sendRedirect("/main.jsp") to redirect on the same site, but I can't seem to figure out a way to get the part between the dots.

Solved

String user = path.substring(path.indexOf("old.") + 4, path.indexOf(".domain"));
response.sendRedirect("https://new." + user + ".domain.com/main.jsp");
A.J
  • 1,140
  • 5
  • 23
  • 58
  • Possible duplicate of [Java HttpServletRequest get URL in browsers URL bar](http://stackoverflow.com/questions/1256562/java-httpservletrequest-get-url-in-browsers-url-bar) – Andremoniy Feb 13 '17 at 19:50
  • What was suggested there returns all `null` – A.J Feb 13 '17 at 20:25

2 Answers2

1

Request URI is a part of URL you're looking for:

request.getRequestURI()
Natalja Olefire
  • 442
  • 5
  • 9
  • That one actually returns anything after `.com` and I don't really need that part. I need to modify the `user` part. – A.J Feb 13 '17 at 20:14
  • 1
    Oh, sorry, misunderstood your question. Then... you can take 'request.getServerName()', tokenize it by "." and replace first (or how many you need) token. – Natalja Olefire Feb 13 '17 at 20:47
0

Solved

String user = path.substring(path.indexOf("old.") + 4, path.indexOf(".domain")); response.sendRedirect("https://new." + user + ".domain.com/main.jsp");

A.J
  • 1,140
  • 5
  • 23
  • 58