0

In my application, I am receiving one URL with different parameters. I need to check if “locale” parameter is present in the parameter or not. If the parameter is present in the URL then I need to modify the value and if the parameter is not present then I need to add the URL.

For this requirement how I can create new URL which will be having existing URL with “locale” parameter?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user2625662
  • 21
  • 1
  • 3
  • Can you share your code to help you better. – Juned Ahsan Jul 29 '13 at 07:43
  • You might want to use a [Filter](http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html) – rocketboy Jul 29 '13 at 07:55
  • Use a Filter for this. You can check an example from [StackOverflow Servlet-Filter wiki](http://stackoverflow.com/tags/servlet-filters/info) – Luiggi Mendoza Jul 29 '13 at 07:55
  • @user2625662 you must edit your question and provide the code **there** instead of posting code in comments. – Luiggi Mendoza Jul 29 '13 at 07:55
  • With the below code i am getting the current URL. I need to generate new URL with the "locale" parameter. FacesContext facesContext = FacesContext.getCurrentInstance(); String viewId = facesContext.getViewRoot().getViewId(); String path = facesContext.getExternalContext().getRequestContextPath(); String URL=path+viewId – user2625662 Jul 29 '13 at 08:08
  • Filter? OP just needs or . He just asked the question the wrong way. He faced a problem, thought about a "possible" solution and asked a question about how to achieve *that* solution. However, this solution is after all completely wrong. OP should initially have asked about the original problem, so that right solution can be proposed in an answer. See also http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem and OP's follow-up question http://stackoverflow.com/questions/17925415/httpservletrequest-is-not-working-for-retrieving-parameter-from-url – BalusC Jul 29 '13 at 15:24
  • I am not passing the parameter to JSF page. so i dont want to use or . I am trying to get the existing URL with the parameter name and values – user2625662 Jul 30 '13 at 09:17

1 Answers1

-3
  1. Fetch the query part of the urL
  2. Use regex to replace the locale param
  3. Create a new URL with the query from #2

    URL url = new URL("http://www.blablabla.com?a=3&locale=us&b=4"); String newQuery = url.getQuery().replaceAll("locale=.+?&", "locale=bla");

DSR
  • 185
  • 1
  • 2