0

I am trying to get absolute URL in java class, but getting an error even after type casting it to Object.

Any suggestions

   String file = ((Object) request).getRequestURI();
     if (((Object) request).getQueryString() != null) {
        file += '?' + ((Object) request).getQueryString();
     }
     URL reconstructedURL = new URL(request.getScheme(),
                                    request.getServerName(),
                                    request.getServerPort(),
                                    file);
     System.out.println(URL.toString());
2
  • What is the actual type of request? By default, request is already an Object (no need to cast), and it most certainly does not contain a getRequestURI method. I presume you're getting a method not found error. Is request supposed to be an HttpServletRequest? download.oracle.com/javaee/6/api/javax/servlet/http/… Commented Jan 31, 2011 at 6:08
  • @Daniel: Yes it should be HttpServletRequest. Commented Jan 31, 2011 at 6:28

2 Answers 2

4

Addition to what @Daniel said URL class has not static method like .toString(). You should use reconstructedURL instead

I don't test it but please try this

String file = (request).getRequestURI();
if ((request).getQueryString() != null) {
    file += '?' + (request).getQueryString();
}

URL reconstructedURL = new URL(request.getScheme(),
                               request.getServerName(),
                               request.getServerPort(),
                               file);
System.out.println(reconstructedURL.toString());
Sign up to request clarification or add additional context in comments.

Comments

2

A cast to Object is never nessecary, because everything is an object. I suggest posting the stacktrace of your error.

((Object) request).getRequestURI() doesn't work, because Object does not have a getRequestURI() function. Just leave the cast away.

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.