Hi, everyone ! I am a Java noobie and this community is a safe place for me to ask a very silly question. I am struggling to understand the process by which I could connect / link together the java classes in a project with the front-end HTML or .jsp files.
To be specific : say I work on a sample on-line shop. Let's say I have the following classes : a Product.java class, a Smartphone.java class, which extends the Product class, a Database.java class in which I use the JDBC connector to manipulate a mySQL db. I also have an index.html file and I could make a products.html file. My struggle is:how do I use the Product.java class in connection to the products.html file ? ! I know a little about the Java Server Pages and servlets technology and although I did find some tutorials on making a web app which deploys to Glassfish or Tomcat, none of those tutorials explained how the actual Java source code is used in the final result ! I mean, will the design-programming of the .java classes be reflected in the .html or .jsp file ? Why should I even make a Product.java class if it doesn't connect with the product.html file ? Please, help me understand it. <3 TIA !
If the user makes a request that should display the outcome on an HTML page, the request should first enter a non-JSP servlet. This servlet will retrieve the necessary data from the persistence layer (and maybe make some changes in case of a POST/PUT/DELETE request) and convert the data to a 'view model' of all the things you want to display back to the user. Then it forwards this view model to a JSP that can access the view model using JSP tags.
If you're using bare servlets, you can forward data to the JSP like this:
From the JSP, you could access the list of products like this:
Instead of using bare servlets though, I recommend using an MVC framework, such as MVC 1.0 or Spring MVC. They allow you to do stuff like this:
First, let me mention that the only HTML pages that Java "talks" to are HTML that is generated dynamically from JSP or servlet code (or code called from JSPs or servlets). If you just include an HTML file in a web application, there will be no Java interaction and the webapp server simply copies the HTML page to the client.
All Java web applications have to have a particular architecture and that is a Web Archive (WAR) file. A WAR file is an enhanced form of Java JAR file, which itself is an enhanced type of ZIP file. It has "directories" that define where the compiled Java code (.class) files go, where application library (JAR) files go, and where application configuration files go. You'll often see a WAR deployed in UNZIPPED form in a web application server, and this is known as an "exploded" WAR, but the format is exactly the same. For information on how to create WARs, a good book on Java (JEE/J2EE) web application is recommended. For information on how to deploy and manage webapps, consult the documentation for whatever web application server you are going to use, be it Tomcat, Wilddfly, IBM WebSphere or whatever.
JSPs are converted by the webapp server into Java servlets, which are then compiled to produced Java classes. Neither the intermediate servlet source code nor the compiled JSP servlet class may be visible to you, but the webapp server knows where to find it, and that's all that's important. The JSP's Java source can be deleted, since only resulting JSP class is actually needed.
Any Java code references from a JSP to non-JSP code will be done the same way that Java code talks to other Java code within the webapp. That is, classes within the WAR's /WEB-INF/classes folder or within one of the JARs within the /WEB-INF/lib folder.
Loudly announcing something is true and finding out you're wrong makes you feel foolish.
Finding out you're wrong and refusing to admit it makes you LOOK foolish.