Difference between using --> request.getServerName() and new URL(request.getRequestURL().toString()).getHost()

Posted on 2005-04-25
Last Modified: 2010-05-18
Is there any difference or preference or issues in using:



new URL(request.getRequestURL().toString()).getHost()

Since they both return the same result??????
Question by:894359
    LVL 8

    Accepted Solution

            Returns the host name of the server that received the request

    >new URL(request.getRequestURL().toString()).getHost()

      The getHost() function returns the host specified in the url parameter

      So both are same

    Author Comment

    Is one way better then the other??? Could one fail easier then the other etc????
    LVL 14

    Expert Comment

    Well, in this case:


    This function will just return the string value of the server. The other way:

    new URL(request.getRequestURL().toString()).getHost()

    This creates an instance of a URL object which could possibly fail if an invalid URL was passed in to the constructor but the way you have it above ... that should never happen. So, as far as using less resource the first one would be "faster" because it uses the request object which is already available and doesn't create an extra unecessary object.

    Unless there was really a need for making the URL object I wouldn't use it since it appears that the information you need is already easily accessible.
    LVL 8

    Expert Comment

    >Is one way better then the other
             first one involves less typing effort that is the only advantage i can think of.There wont be any performance issues that you can notice as much in this case.
    >Could one fail easier then the other etc????
           In your case NO since you are using request object in both the cases

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now