r/javahelp • • Jun 05 '20

How does Java and Javascript work together in building websites?

I'm learning Java right now, but eventually I want to learn Javascript as well to be able to build website and servers. But I'm not sure how they actually come together.

It will be great if someone can eli5 using an example... let's say I want to make a website for a bakery shop. How can I use Java and Javascript to create a website that enables me to receive orders, ship them out to my customers, and store the data somewhere in my computer so I can write algorithms to analyze the data?

And then maybe use that data and feed it back into the website. For example I send data from my warehouse back into the website, so the customers can see which kind of bread is most popular and which sold out.

That kind of communication. I don't know how it happens and I am curious to find out how I can make it happen with Java and Javascript.

It'll be great if you can also enlighten me on the specific things I should learn. This will give me some kind of direction and serve as milestones perhaps?

Also, I heard one can build entirely using Javascript. Is there an advantage/disadvantage to doing so as opposed to using Java for back-end and Javascript for front-end?

Thank you in advance.

32 Upvotes

26 comments sorted by

61

u/snaket2003 Jun 05 '20 edited Jun 06 '20

ELI5 ->

If you think of a website as a bakery, JavaScript is the clerk at the front who is in charge of sorting shelves, cleaning up and making it all look pretty. Java is the full time baker at the back baking the bread, figuring out how to properly use the oven and fighting off the cats at the back.

Technology wise I would recommend ->

Front of the bakery

  • Counters, glass, decorations - HTML, CSS

  • Nice front clerk - JavaScript

  • How front clerk talks to baker - REST, specifically something like Express

  • Where to store account and orders - MongoDB, Mongoose

Back of the bakery

  • Grumpy but amazing baker - Java

  • Oven - Spring Framework

  • How baker talks to front clerk - REST, specifically Spring Web (RestTemplate)

  • Where the baker keeps track of inventory - Postgres, JDBC.

Could the front clerk do the baking? Sure, but I’m sure he may not be as good depending on the size of your bakery. Small neighbourhood one versus national chain. Very different businesses.

Could the baker do the selling? Sure, but it will probably not look very professional and our baker is quite grumpy anyways. He is being doing the same thing for decades!

Best of luck on your journey!

Edit: thank you for the award kind stranger and upvotes.

2

u/[deleted] Jun 06 '20

This is very helpful, thank you :)

8

u/Mimehunter Jun 05 '20

I use sql because I'm basically forced to - is there a significant improvement or advantages with mongodb or mongoose?

Great illustration btw

11

u/morhp Professional Developer Jun 05 '20

Depends on how your data is structured. SQL is for orderly tables that reference each other, mongodb is mostly for storing json like objects. One isn't generally better than the other.

3

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jun 06 '20

In general when in doubt use a relational database. Most data is in fact relational. Document stores have very few use cases (this is true for most NoSQL solutions; they tend to be very specialized) and MongoDB is no different.

3

u/thee_almighty_thor Jun 05 '20

Really good analogy

3

u/AudioManiac Jun 05 '20

How front clerk talks to baker - REST, specifically something like Express

Correct me if I'm wrong, but I thought Express was a backend JS framework? i.e. you'd use it to replace java in this example. In your example, you'd only need REST to talk to the java backend. Am I wrong?

3

u/mtko Jun 05 '20

You're right. Express is for writing a backend API when you're using a node server (or something similar).

You would generally use something like jquery or a built-in http module for whatever frontend framework you're using to do REST requests to the backend.

2

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jun 06 '20

See my comment here

A lot of the information is just plain wrong.

0

u/snaket2003 Jun 06 '20

Yes, you are absolutely correct. I just wanted to provide an analogy and path that could accelerate the learning of OP.

I found using pure http modules of vanilla JS quite difficult when I was learning JS and making REST calls. One day I saw Express and everything just clicked and since OP wants to learn backend anyway, I figure it was a sensible suggestion. Good point though!

5

u/Son_of_Entropy Jun 05 '20

If I had an award to give, I'd give it to this comment. Love the analogy

2

u/dark_manyakulade Jun 06 '20

I doing that job too but really very good explanation!

2

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jun 06 '20 edited Jun 06 '20

I really don't understand why you're using NodeJS (and related tech) when the OP is asking how Java and JavaScript would work together. Why not simply mention Java frameworks there?

In a simple SPA the front-end (Angular for example) JavaScript would just talk to a Java REST back-end. There's no NodeJS or Express there. In addition Mongoose is JavaScript back-end technology, not Java. You would not use Mongoose in a Java back-end.

There's even more mistakes: you don't 'talk' to the front end through RestTemplate at all. That would generally be used for a back-end to talk to other back-ends.

-1

u/snaket2003 Jun 06 '20

I’m obviously not going to engage in a long winded debate about this. For years, I have built Spring Boot back end Java apps that leverage Spring Web and RestTemplate to set up REST APIs which front end and middleware apps use to interact with backend services. (Regardless of being browser or Node based)

I work as a full stack dev and often leverage Node.js (JavaScript last time I checked) to set up middleware services.

Mongoose is just a lightweight framework that eases interaction with MongoDB. Nothing else.

When it comes to Express, as I mentioned before, is just an easy way of creating web servers, APIs, etc useful to someone trying to understand and learn the basics without having to use vanilla JS to make API calls right away. Plus OP wants to get into backend so I don’t know how Node is something I shouldn’t make reference to.

Unfortunately, I feel comments like this overly mystify development for newcomers and if we don’t use easy to understand metaphors, how could newcomers ever get started? If OP wanted the technical documentation response, he wouldn’t be asking in this subreddit for an ELI5... SMH

2

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jun 06 '20

I’m obviously not going to engage in a long winded debate about this.

If that is how you respond to people just pointing out some mistakes in your post, you should not be posting on a beginner sub.

Unfortunately, I feel comments like this overly mystify development for newcomers and if we don’t use easy to understand metaphors, how could newcomers ever get started?

That's not the problem with your post at all. The 'bakery' analogy is fine. The problem is that you are making some rather specific plain mistakes. And not only that; when someone points out the mistakes you downvote their reply and act as if I personally attack you.

He was asking how JavaScript and Java interact. The JavaScript would be the front-end 'clerk', the Java the 'baker'. Again; that analogy is fine. The problem is that in your analogy you're for no appearant reason are also using JavaScript in the back-end, which is not at all what the OP is asking about, making the answer more confusing than needed.

1

u/snaket2003 Jun 06 '20

Fair enough. However, I ask you that you formulate a full analogy that can help and not confuse OP like I did where I am just plain wrong

3

u/Northeastpaw Jun 05 '20

I don't know how ELI5 I can get since this is a complicated topic. Maybe ELI15.

You want to likely use a REST architecture. REST stands for Representational state transfer. With REST your backend server exposes a set of HTTP endpoints and takes action based on the HTTP request method that was used. For example, a GET request will return some data. A POST request with form data will store that data somehow on the server. A DELETE request will, unsurprisingly, delete some data on the server.

With REST the server can be done in any language. Java has a few different frameworks like JavaEE and Spring that help you get the backend in place. There's also libraries and frameworks for Node (think server side JavaScript), Python, Ruby, C#, and almost any other popular language. You can even have different parts of your backend written in different languages.

The frontend code is JavaScript. There are lots of frameworks like React and Angular to help you construct not just the UI but also how your site communicates with your REST backend.

Side note: Java and JavaScript have similar names due to an unfortunate marketing decision in the 90s. The naming doesn't mean they have to be used together or that they depend on each other. They're about as similar as ham and hamster. While the frontend code is almost always JavaScript the backend could be Java or something else entirely. The nice thing about REST is that it abstracts away the implementation details of the backend.

1

u/[deleted] Jun 05 '20

This helps, thank you for the explanation and examples provided :)

1

u/PrinceValkyr Jun 05 '20

In a nutshell,

Javascript is front end logic, it's responsible for determining what happens on front end based events. Like pressing a button, filling out a textbox.

Java is for backend logic, it's responsible for retrieving information to display on the front end, like if a user enters in a username/password, java needs to check against database to see if those credentials are correct.

So, using the username/password as an example. Javascript can take what the user enters into the textbox, and call some backend code (Java) to check and validate the data.

1

u/Yithar Intermediate Brewer Jun 05 '20

let's say I want to make a website for a bakery shop. How can I use Java and Javascript to create a website that enables me to receive orders, ship them out to my customers, and store the data somewhere in my computer so I can write algorithms to analyze the data?

Well you'd have a backend server in Java that actually talks to the database and serves REST endpoints that can be accessed via HTTP. See tutorial on REST APIs.

The actual user interface would be implemented in Javascript. Basically the web app the user interacts with. When the user wants to order some bread or something, it would contact a REST endpoint that the backend server is hosting, and that would be forwarded to the database, and then a response would be sent back to the webapp. Once an order is persisted to the database, actions would be taken such to ensure that the bread gets to the person who ordered it. Perhaps the shipping is manually done by an employee, in which case it would notify an employee.

1

u/coding_boy_ Jun 06 '20

You can use fronted like a angular or react and backed java - spring boot and JDBC to store data.

0

u/little_bobby_tables1 Jun 05 '20

java - being JSP or Servlets can be the back end of the application

JavaScript/jquery - can be the front end of the application.

Eli5: java is the server "controller" and its in charge of handling the user web requests. HTML & CSS are what the customer sees and JavaScript makes that content more user friendly by adding draggable components, forms that can submit data and update a part of the page with out having to reload the full page, graphs suck as gauges, bar graphs and so on.

HTML content doesn't change, you can use JSP or a language like it to get information from a database and populate the website. ( Ajax is a "subset" of JavaScript and it can be also used for this )

--Start by learning HTML & CSS then choose where you want to focus your attention front end ( what the customer sees) or back-end ( what happens in the back)

1

u/[deleted] Jun 05 '20 edited Jun 05 '20

How does Java handle user web requests? Does it have anything to do with the Spring framework? (if possible pls eli5 haha)

edit: another kind redditor has already touched on this. thanks for your explanation above :)

12

u/[deleted] Jun 05 '20

[removed] — view removed comment

3

u/[deleted] Jun 06 '20

Ah I actually didn't know there's so much flexibility on server side. Thanks for enlightening me :)

15

u/[deleted] Jun 05 '20

Java is to JavaScript like Car is to Carpet

It's just the most accurate (and funny) description I ever ear