• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Linking .java classes with the .JSP files

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Saloon Keeper
Posts: 12902
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

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:
 
Saloon Keeper
Posts: 23544
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Andy!

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.
 
Andy Ben
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both, Stephan van Hulst and Tim Holloway !!! Your detailed answers put things in a clearer perspective and I will apply your advice ! Thank you ! <3
 
Marshal
Posts: 72675
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)
I have moved you to our JSP forum because we like to reserve the beginning forum for easier questions.
 
Sheriff
Posts: 67529
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this article for an overview on what JSP is and how it works.

Then you might want to read this article for info on properly structuring Java web apps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic