• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting the port dynamically

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As they are on the same sever, can you not just use a server-relative path (one beginning with the context path)?
 
Neil Gold
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Neil Gold
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic