Windows – What exactly is going on when I go to localhost:8080 in the web browser? (Apache Tomcat)

localhostporttomcatwindows

I've spent my career doing all local application programming (C++ applications and whatnot). I'm trying to dig my feet into the web world now, however. I'm using Eclipse (Mars) and Apache Tomcat 8.0.23 to try and set up an envoirnment in which I can experiment with and learn about server side programming (JSP, PHP, etc). I have it all set up such that when I type

localhost:8080

in my browser it directs to the correct tomcat page. I know this is probably very simple, but it is kind of blowing my mind here.

What exactly is going on when I type localhost?

Where is the tomcat page coming from if not the internet? I'm pretty sure the data comes from a server, which thanks to Tomcat, is on my PC, but how did my browser know where to find the information just from localhost:8080?

I assume 8080 is a port or something, but I'm really not sure about that either and if it is a port I'm not really sure what that means.

Basically all I know (I think…) about server side web development is that in pure html/css web development all work is done locally once everything is downloaded, but with servers some of the work (inside some delimiters) goes off to the server and comes back injected into the html in a different form (much like sending data to a function in C++ and different data being returned), which then is drawn to the page locally again.

Best Answer

What exactly is going on when I direct my web browser to go to localhost:8080?

  1. You are causing your web browser to ask your operating system to resolve the hostname localhost. Operating systems will normally resolve the hostname localhost to 127.0.0.1, your loop back interface.

  2. Any hostname or IP address followed by a : and a port number like :8080 tells the browser to connect to that TCP port instead of the default web-server port 80.

    Just as http://localhost:80/, http://localhost/, http://127.0.0.1/:80, and http://127.0.0.1/ each connect to the same server and port, so does http://localhost:8080/ and http://127.0.0.1:8080/ also connect to the same ip address but on TCP port 8080

Additional Note: In HTTP/1.1, even though the web browser connects to the same IP address and port, to many web-servers, there is a slight difference between localhost and 127.0.0.1. Depending what is in the address bar, your browser will send a request header field with either Host: localhost or Host: 127.0.0.1 in it. When a web server is properly configured, the browser's Host header field allows a single web-server to listen on a single IP address port and serve different webpages for many different domains that resolve to the same IP address.

How does the operating system typically resolve hostnames like localhost?

  1. On Unix systems or Unix like OS such as Linux or Freebsd, the file is /etc/hosts, and is likely to have lines like:

    127.0.0.1   localhost
    ::1     localhost ip6-localhost ip6-loopback
    
  2. On windows, the file is c:\windows\system32\drivers\etc\hosts and will usually have a similar line:

    127.0.0.1   localhost
    

Additional note: If you like, you can add lines to your hosts file like:

127.0.0.1     localhost
127.0.0.1     developer.yourdomain.com
# Deny Browser Request For These Sites
127.0.0.2     www.spam.advertisements.com
127.0.0.2     super.ads.com
# Block These Sites
127.0.0.3     www.dont.go.here.com
127.0.0.3     nsfw.stuff.com 
  • The Uniform resource locator (URL) http://developer.yourdomain.com:8080/ in your browser's address bar, directs the web browser to make a TCP connection to port 8080 of your local loopback address 127.0.0.1.

  • Furthermore, acording to rfc1700 page 4 any address in the 127.0.0.0/8 range is also a loopback address. Thus, a properly configured webserver running on your computer could deny all request on port 127.0.0.2 while giving a generic "You should not go here. The site is blocked" message for connections on 127.0.0.3.

Where is the tomcat page coming from?

Apache Tomcat is a server that listens on a port and runs java programs that generate content to send to your browser.