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

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.


Solution 1:

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.

Solution 2:

When you type www.google.com into you web browser it opens a connection on the default port 80 to the Google server (via a DNS lookup to see what IP address www.google.com is) and requests the web page. The Google server responds with a web page which your browser draws on screen (usually by making further calls for images, CSS and JavaScript).

When you go to localhost:8080 it's the exact same thing. Localhost server name always resolves to the machine you are running on and uses the fake IP address of 127.0.0.1 (your computer will have two IP addresses - this fake one that every computer has and the real one). So you must have a Tomcat instance running locally listening for connections on port 8080.

Why port 8080 rather than the default http port 80? Well that's in case you have a webserver in place already.

Typically you have web servers and app servers.

  1. Web servers (like Apache httpd) serve up static pages. In effect it's like a fancy one-way FTP server. You open a TCP connection and ask for a file using the HTTP commands (typically GET). The webserver returns a HTML file and your browser downloads it and parses it, sees it needs other images and requests those. A webserver is very fast but basically lifts files off the local disk and returns them.

  2. An App Server (like Tomcat or JBoss) is similar except it typically runs code to "create" the page you are asking for, rather than lifts it straight from disk. What it does to create that page is up to your application. It could connect to a database, run a program, randomly serve a page... Etc. When you log on to your online banking for example, the app server sets up a session for you, returns that session id in a cookie which your browser resends back each time you make a request until you log out. So if you ask for the "my balances" page then the bank looks up who you are based on your session id, then goes to its database to get your name and bank balance, then creates a page saying "Hi John Smith, your balance is €100." App servers are typically slower, but more versatile than Webservers.

Many places have a WebServer running in default port of 80, and then AppServer running on a secondary port (like 8080). So static pages are served fast and when users click on a link which takes them to a dynamic page, the link either goes to 8080 (which the app server responds to) or web server is setup to forward certain requests to the app server (in which case it still looks like the default port 80 and so looks a little nicer to the user).

Of course this is a very high level overview and nothing is that black and white. Most Webservers can create some dynamic content by running scripts (typically CGI via she'll scripts using perl or PHP) and most app servers can also serve plain files like a webserver. In fact it's possible to just run an app server and change the tomcat port number from 8080 to 80.

Finally many applications are moving away from serving full HTML pages for each request to the app server (which seen as slow and inefficient) and are instead responding with just the data snippets using AJAX to send JSON or XML. Back to the original www.google.com you used to type in your search query, hit Search and get a page of your results. Now instead, as you type, your browser is continually sending AJAX requests to Google which responds with up to date search results based on what you've typed so far and then your browser updates the page. It means no need to wait for user to submit the page so faster and more dynamic to the user (like an old school desktop app would be).

Solution 3:

In this scenario You send a request to resolve localhost which is 127.0.0.1 (loopback interface) and Apache is configured to listen on port 8080, when you access localhost on port 8080 it returns the default VirtualHost.