This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Getting the port dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting the port dynamically" Watch "Getting the port dynamically" New topic
Author

Getting the port dynamically

Neil Gold
Greenhorn

Joined: Nov 12, 2009
Posts: 5
I have a servlet (parent) that needs to invoke another servlet (child) that resides on the same appserver (Could be Tomcat, Websphere or Weblogic). The way we build the URL now to reach the child servlet is to build the URL up by pulling the protocol, host and port out of the request object when the parent servlet executes. Since the child is on the same appserver, we use the same host/port and protocol. The host is always going to be local, so we could just use localhost instead of a getHost() call.



This code works fine, but some clients have started to use a load balancer and if the LB is not configured to pass through the client's address in the request object, what happens is we wind up getting back the IP/port of the LB instead of the app server and our servlet request will fail.

As a solution, we could change to use "localhost" instead of calling srcURL.getHost(), however how would we resolve the port? For instance, the port Tomcat is listening on is not guaranteed to be 8080 and can be changed by the user (same with other appservers). Is there a way to dynamically determine what port the app server is using without requiring the user to have to tell the servlet or by getting it out of the request object?

thank you

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

As they are on the same sever, can you not just use a server-relative path (one beginning with the context path)?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Neil Gold
Greenhorn

Joined: Nov 12, 2009
Posts: 5
I'm not sure how to create a URL with just a relative path. I need to use HTTP and open the connection, but not finding a way to do that using a relative path in the java docs.

thank you
Paul Okeke
Ranch Hand

Joined: May 16, 2009
Posts: 58
Using relative is far easier,

If only you know you context root (mostly the URL that does not specify any particular page)
eg http://localhost:8080/yourApplication/. Here "yourApplication" is the context root.

All you need to do is reference all other pages assuming that the context root is your working directory.

eg if you want to reference http://localhost:8080/yourApplication/forms/registrationForm.jsp, you can do it this way
forms/registrationForm.jsp


OCJP 1.6
Neil Gold
Greenhorn

Joined: Nov 12, 2009
Posts: 5
Paul,

My problem is the opposite, I don't know the context root.

My Java servlet needs to invoke another Java servlet that resides on the same app server. I can use localhost, but how would I know what port to use? Our customers using our servlets aren't always using 8080 and may be using Websphere or Weblogic and are free to change the port to whatever they want.

http://localhost:???/mySecondServlet/...

What we are doing now is looking at the Request object that was passed to the first servlet, then extracting the host, port and protocol from that to create the URL to find the 2nd servlet. The problem is some customers are now using Load Balancers which pass in the host, port and protocol relative to the load balancer and not relative to our app server which causes us problems. Since I know we are local, I know localhost will work, but I don't know if the port has changed and I can't rely on the Request object to give me the port.

Bear recommended using a relative path, but I can't seem to make that work when creating the URL I need to invoke the 2nd servlet.
Paul Okeke
Ranch Hand

Joined: May 16, 2009
Posts: 58
I am sure that the HttpServletRequest have some methods retrieve the URL and the URI from a particular request object.

But if you talk about the second servlet, then you MUST know the port where it was deployed.

DNS server could have being the solution, to resolve this inconsistency/change in the address (port no).

But since it seems there is no DNS server to resolve this, your have no alternative than to create a mechanism to conform that
the URI is valid before processing a request.

A change in this port no is as good as saying the http://www.yahoo.com was changed to http://www.yahoo.org.
I dont see any alternative to this...

Will equally appreciate any other person will have a better idea
T. Huy Nguyen
Ranch Hand

Joined: Nov 02, 2010
Posts: 57
Because you're creating a HTTP connection to the 2nd servlet, you need the port number. But if that servlet resides in the same application (or even same domain) as your servlet, using RequestDispatcher could be handier, no port number needed (but you still need to know the relative path from the context path to that 2nd servlet)

And context root is available at ServletContext.getContextPath()

And if all solutions fail, you can always ask your client to input the correct address of the 2nd servlet in some database table.


My material for SCJP (2008), SCWCD (2010), SCBCD (2010). About me
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the port dynamically
 
Similar Threads
How to set Alias name for URL or change URL
JNDI in Servlets
How to get the port dynamically
Security Contraints setup..
tomcat.startup..inquiry