There are countless ways to serve HTML from Java but virtually all of them rely on java servlets and java server pages (JSPs) which are Java's specification for handling web requests.
The absolute bare minimum to get going:
- Install Java EE SDK ensuring to also install Netbeans and Glassfish.
- Launch Netbeans and create a "Java Web" / "Web Application" project
- Enter a project name, e.g. MyWebApp
- In the Server and Settings screen, you need to Add... your server so do so. Point to the file location of your Glassfish server and enter the admin name and password
- Ignore the framework stuff and Finish
- NetBeans will generate a sample app and you can click straightaway on Run Main Project. It will deploy your app to Glassfish and load http://localhost:8080/MyWebApp/ from your default browser
Important things to note:
A file called web.xml tells the host server some basics about your web app. This file can contain a lot of other stuff but the default is some boiler plate. The most interesting part says <welcome-file>index.jsp</welcome-file>
which means when you load http://localhost:8080/MyWebApp/ it will default to load index.jsp.
The index.jsp is what gets loaded if you don't specify a page to the server. If you look at index.jsp it's just HTML with some JSP markup.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Creating new JSPs is as simple as writing HTML. Netbeans has a wizard to create a simple JSP.
You can embed chunks of Java into a .jsp easily and step in and out of Java / HTML with the <% %> notation such as
<%
for (int i = 0; i < 10; i++) {
%>
Hello <%=i%>
<% } %>
Glassfish is just one possible app server. As long as you write compliant code it should functional with only minimal or zero modifications on any other implementation of Java Servlet / JSP spec. e.g. Jetty, Tomcat, oc4j, JBoss, WebSphere etc.
This is just the tip of the iceberg. You can make things as simple or complex as you like.
Once you know the basics then it's up to you how deep you go. More advanced topics would be:
- Taglibraries - these can remove a lot of java clutter and are considered more correct
- Expressions - using expressions inside JSP pages to reduce the need for messy <%= notation
- Custom servlets let you move model / business stuff into a Java class and leave the .jsp to just presentational
- MVC web frameworks like Struts, Spring etc.
- Security & filtering
It's a massive subject but it's fairly easy to do something quick and dirty.