3

I am trying to get the absolute URL in my managed bean's action listener. I have used:

HttpServletRequest#getRequestURL() // returning http://localhost:7101/POSM/pages/catalog-edit
HttpServetRequest#getQueryString() // returning _adf.ctrl-state=gfjk46nd7_9

But the actual URL is: http://localhost:7101/POSM/pages/catalog-edit?_adf.ctrl-state=gfjk46nd7_9&articleReference=HEN00067&_afrLoop=343543687406787. I don't know why the parameter artcileReference get omitted.

Is there any method which can give me the whole URL at once? How can I get the whole URL with all query string?

1
  • Complicated answer, some monkey coding :) Commented May 31, 2013 at 6:03

2 Answers 2

5

You can reconstruct your URL manually by using ServletRequest#getParameterNames() and ServletRequest#getParameter() both available with the HttpServletRequest instance.

Here is a sample code I've used in the past for this exact purpose :

private String getURL()
{
    Enumeration<String> lParameters;
    String sParameter;
    StringBuilder sbURL = new StringBuilder();
    Object oRequest = FacesContext.getCurrentInstance().getExternalContext().getRequest();

    try
    {
        if(oRequest instanceof HttpServletRequest)
        {
            sbURL.append(((HttpServletRequest)oRequest).getRequestURL().toString());

            lParameters = ((HttpServletRequest)oRequest).getParameterNames();

            if(lParameters.hasMoreElements())
            {
                if(!sbURL.toString().contains("?"))
                {
                    sbURL.append("?");
                }
                else
                {
                    sbURL.append("&");
                }
            }

            while(lParameters.hasMoreElements())
            {
                sParameter = lParameters.nextElement();

                sbURL.append(sParameter);
                sbURL.append("=");
                sbURL.append(URLEncoder.encode(((HttpServletRequest)oRequest).getParameter(sParameter),"UTF-8"));

                if(lParameters.hasMoreElements())
                {
                    sbURL.append("&");
                }
            }
        }
    }
    catch(Exception e)
    {
        // Do nothing
    }

    return sbURL.toString();
}
Sign up to request clarification or add additional context in comments.

2 Comments

@TapasBose ortherwise you won't have a valid URL if you have special characters example : parameter including one &
+1 and obviously acceptable answer. Thank you very much. I have posted similar solution, if you can take a look. Actually I was getting NPE from the URLEncoder#encode so I have handled that also I have considered the HttpServletRequest#getParameterValues() as there can be param=value1&param=value2&param=value3.
1

Here I came up with my solution, taking idea of the answer given by Alexandre, considering that HttpServletRequest#getParameterValues() method:

protected String getCurrentURL() throws UnsupportedEncodingException {
    Enumeration parameters = getServletRequest().getParameterNames();
    StringBuffer urlBuffer = new StringBuffer();
    urlBuffer.append(getServletRequest().getRequestURL().toString());

    if(parameters.hasMoreElements()) {
        if(!urlBuffer.toString().contains("?")) {
            urlBuffer.append("?");
        } else {
            urlBuffer.append("&");
        }
    }

    while(parameters.hasMoreElements()) {
        String parameter = (String)parameters.nextElement();
        String[] parameterValues = getServletRequest().getParameterValues(parameter);

        if(!CollectionUtils.sizeIsEmpty(parameterValues)) {
            for(int i = 0; i < parameterValues.length; i++) {
                String value = parameterValues[i];

                if(StringUtils.isNotBlank(value)) {
                    urlBuffer.append(parameter);
                    urlBuffer.append("=");                            
                    urlBuffer.append(URLEncoder.encode(value, "UTF-8"));

                    if((i + 1) != parameterValues.length) {
                        urlBuffer.append("&");
                    }
                }                                                                        
            }
        }                                    

        if(parameters.hasMoreElements()) {
            urlBuffer.append("&");
        }
    }

    return urlBuffer.toString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.