Can I get the port number from the URL ?


Web tier: servlets, JSP, Web frameworks: Can I get the port number from the URL ?

  1. Can I get the port number from the URL ? (8 messages)

    Hi !
      Is there any way to get the port number from the URL when my application is first started..
    eg: if my application URL is, then how do I get the port 8000 in my application when the URL is first typed in the browser ?

    Any help is greatly appreciated.
  2. request.getServerPort()
  3. Hello anecss !
      Thanks .. is there any other way, other than request.getServerPort() ? My application is being launched by another application and hence request.getServerPort() returns the requestors port number and not that of the requested applications port number.

  4. how is your application launched? what is the nature of the other application -- is it a simple http redirect or a forward or something different?

    Aren't the methods in the following classes good enough for your case?
  5. Hi !
      Here is the situation...

    Appliction X - http://www.somethingX.xom:7001/someXApplication
    Application Y -

    Both applications work fine when running independently. But application X has a button which references Application Y. When a button is clicked in Application X, a new window is opened & it opens the window with the URL http://www.somethingY.xom:7003/someYApplication?login=abc&pwd=abc which is correct.
    In appliction Y, I see if login = abc & pwd = abc, then I build a new URL, and then do a request.sendredirect().
    When I build, this URL, I get the port as 7001 instead of 7003.

    I hope I am clear !
  6. When building the URL, I do a request.getServerPort() to get the port !
  7. i tried exactly the same thing with two tomcats one running in 7001 and another running in 7003 and it works fine.. and i dont see the problem... btw... i understand you have to build the url and do a sendRedirect because you have to redirect to{$PORT} isn't it? otherwise a simple reference to /somthing?action=1234 will do what you need as you are already in the context of application Y. ok.. this may be a bug... as i recall.. in tomcat's earlier versions with https they had one similar to this.. what is the web/app server you are using? what's the OS? j2ee version?
  8. browser versions too!
  9. Hi !
      I am using Weblogic 6.1 on a Sun Solaris. My browser is IE version 5.5