Can someone please explain the relationship between Java code and HTML/JS files in a Java servlet?

I have an SQL database that’s managed with a Java application (created in Eclipse). As of now I’m just providing user input through the IDE console, however, I’d now like to transition to a web interface.

I’ve watched a couple YouTube videos and have gone through some tutorials on how to make an extremely simple dynamic web project through Eclipse and TomCat (basically just helloworld), however, I’m still fairly uncertain how I can take the data retrieved from the database via Java code and insert it into an HTML/JS page.

What I would like to do is have an already created HTML files that are located in my project repo and then just change the values in those HTML/JS pages. However, all I’ve been able to find are approaches that use Java’s PrinterReader.

Can anyone explain how this can be achieved and/or provide some resources that explain this? I guess I’m just having a hard time understanding how to connect all the dots…

Thanks!

First off I want to say its been a while since I used Java, and even then I had only limited experience with Java for web development.

Lets get a few things out of the way, just to make sure:

  • Java !== Javascript (have to clear it up just in case haha) Java and Javascript are totally unrelated languages.
  • Javascript is usually a client-side language. This means it runs in your browser, after the browser downloads it, and then “runs” the code. Same way HTML and CSS are downloaded, and used to create a nice looking web-page.
  • Java is a server-side language. This means it runs on your “back-end” or server. no Java code can run in the browser.
  • Tomcat is a project that basically allows Java to be used as a web server. (my definition might be totally off, again its been a while since I touched this technology haha)

Now onto your question:

Can someone please explain the relationship between Java code and HTML/JS files in a Java servlet?

The Java servlet handles the “back-end” part of a web application/ web page. Where the browser goes to the url: localhost:8080 (when running locally) or mywebsite.com (when on a server somewhere on the web) and “requests” whats there. The Java servlet code will be called, where it can return stuff. this stuff could be:

  • an html file
  • some JSON
  • some XML
  • a byestream or something

Think of this as “downloading” whatever is at that page. Your asking for specifically html/JS code, and so if you went to localhost:8080 and your java servlet code is setup to return some static files it will return those files. Alternatively the servlet could return something like just a string like “hello world”, or a JSON object like:

{
  "word": "Hello World!"
}

or something else.

Now going back to the browser, the browser will get this information and “display it” to its best of its ability. If its an HTML page it will load all the assets/css/images/whatever and “build” the website. If this page has some script tags:

<script src="myjsfile.js"></script>

Then the browser will go get that information again from the server, this could hit the same servlet (that is returning static files) or another servlet. Regardless it will return the JS file which will be parsed and “ran” by the browser.

So long-story short, the html/css/javascript code/files are returned via the server. That server uses Java code.


Now if you want to change the HTML code before it is returned, you will have to “change” the html files a bit,so you can make them dynamic. I can’t provide much information on this, since I totally forgot how to setup Java to do that haha, but the documentation on this approach is out there :slight_smile: (i’d check the tomcat docs)