Can I get the port number from the URL ?

Discussions

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 http://something.com:8000/myapplication, 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.
    Thanks,
    -Paresh
  2. request.getServerPort()

    http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#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.

    -Paresh
  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?

    http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html

    http://java.sun.com/j2se/1.3/docs/api/java/net/URL.html

    http://java.sun.com/j2se/1.3/docs/api/java/net/InetAddress.html
  5. Hi !
      Here is the situation...

    Appliction X - http://www.somethingX.xom:7001/someXApplication
    Application Y -
    http://www.somethingY.xom:7003/someYApplication?login=abc&pwd=abc

    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, http://www.somethingNew.com:7003/something?action=1234 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 !
    -Paresh
  6. When building the URL, I do a request.getServerPort() to get the port !
    -Paresh
  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 somethingNEW.com{$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

    Thanks,
    -Paresh