• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2261
  • Last Modified:

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

Is there any difference or preference or issues in using:

request.getServerName()

vrs.

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

Since they both return the same result??????
0
894359
Asked:
894359
  • 2
1 Solution
 
koppchaCommented:
>request.getServerName()
        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
0
 
894359Author Commented:
Is one way better then the other??? Could one fail easier then the other etc????
0
 
dfu23Commented:
Well, in this case:

request.getServerName()

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.
0
 
koppchaCommented:
>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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now