Anatomy of a Web App

So far, you’ve run a server using Tomcat or Jetty. You deployed a “hello world” web app to your server, and you’ve visited that web app by navigating to localhost:8080 in your web browser.


This is a companion discussion topic for the original entry at https://happycoding.io/tutorials/java-server/web-app
1 Like

For a webapp on a Tomcat server, if I change the index.html file then I can view the changes immediately after I refresh the page on my browser.
But when I modify the response by the servlet class and compile it (while keeping the server running), then after refreshing the URL on my browser I don’t see any change until and unless I restart the server.

Please shed some light on this topic.

If I had to guess, I’d say that’s because the server loads the Java classes at startup (or the first time they serve a request), and then they keep those classes in memory from then on, without reading from the .class files again. However, I would guess that static files like index.html are loaded from the file system every time they’re requested.

If you want your Java changes to be reflected, you might look into something called “hot reloading” or “hot swapping” which tells your server to reload your classes whenever the files change. I personally tend to restart my server manually, or I use an IDE to handle this stuff for me.

1 Like

okay. thanks.

I was having this error on both Tomcat and the jetty Server.
I was having trouble including the css and javaScript files into my index.html ( I had them all placed together inside the webapps/myWebAppName/ directory).


Ignore the temp folder !!
This worked fine when I opened the index.html directly in the browser. i.e. the css and js worked fine.

But when I placed the css and js files in a separate directory and then included them in the index.html file, everything worked fine even for the Tomcat & Jetty server case.

Does anyone have any idea what causes this bug?
Does anyone else ever faced this?

I’m not sure I understand what your bug is. What’s not working how you expected it to?

I guess this was also a caching related issue. I tried cache-clearing refresh and the bug vanished.

My bug was something like this:



Wheneve I toggled between the above two lines or did something similar (i.e. whenever I changed the src), I used to get the Uncaught ReferenceError: load is not defined at onload.
image

Where, load was a function defined inside the “versus.js” file.
image

Ahh yeah that makes sense, this happens to me all the time. It’s because your browser is caching your HTML file, so it’s still trying to load the JavaScript file from the old location.

I’ve gotten into the habit of always using a cache-clearing refresh automatically whenever I’m doing web dev stuff, because of this exact kind of issue.

1 Like

Guess I’ll have to make Ctrl+F5 a habit, too.:smile:

1 Like