JakartaJEEWebDevelopment

Jakarta JEE8 Enterprise Web Development

View on GitHub

Demonstrating that JSP is HTML in a JEE 8 Web App Module

Jakarta Server Pages

JEE 8 JSP is HTMl Start Branch

1. Consider the following section of Print writer code

	 PrintWriter writer = response.getWriter();
    writer.append("<!DOCTYPE html>\r\n")
          .append("<html>\r\n")
          .append("    <head>\r\n")
          .append("        <title>Hello Associate Application</title>\r\n")
          .append("    </head>\r\n")
          .append("    <body>\r\n")
          .append("        Hello, ").append(Associate).append("!<br/><br/>\r\n")
          .append("        <form action=\"greeting\" method=\"POST\">\r\n")
          .append("            Enter your name:<br/>\r\n")
          .append("            <input type=\"text\" name=\"Associate\"/><br/>\r\n")
          .append("            <input type=\"submit\" value=\"Submit\"/>\r\n")
          .append("        </form>\r\n")
          .append("    </body>\r\n")
          .append("</html>\r\n");

This is an extremely tedious and error prone way to output an HTML form, it would be much easier to present this form as a JSP

2. Enter the JSP

	<%@ page contentType="text/html;charset=UTF-8" language="java" %>
	<%!
	    private static final String DEFAULT_Associate = "Guest";
	%>
	<%
	    String Associate = request.getParameter("Associate");
	    if(Associate == null)
	        Associate = DEFAULT_Associate;
	%>
	<!DOCTYPE html>
	<html>
	    <head>
	        <title>Hello Associate Application</title>
	    </head>
	    <body>
	        Hello, <%= Associate %>!<br /><br />
	        <form action="greeting" method="POST">
	            Enter your name:<br />
	            <input type="text" name="Associate" /><br />
	            <input type="submit" value="Submit" />
	        </form>
	    </body>
	</html>
3. JSP’s are a combo of Java Code and HTML that are easier to maintain and comes with pre-built tags, JSTL and Expression Language EL, we can also create our own custom tags
4. We will now replace the writer.out() code in our JEE8 Web Archetype Example with JSP’s and through debugging and logging we will learn about the lifecycle of a JSP
5. At this point we switch to code in Eclipse and start with updating the Maven Dependencies so that

we can make use of JSP’s

Add Maven Dependencies
	<!-- https://mvnrepository.com/artifact/jakarta.servlet.jsp/jakarta.servlet.jsp-api -->
	<dependency>
	    <groupId>jakarta.servlet.jsp</groupId>
	    <artifactId>jakarta.servlet.jsp-api</artifactId>
	    <version>3.0.0</version>
	    <scope>provided</scope>
	</dependency>

	<!-- https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api -->
	<dependency>
	    <groupId>jakarta.servlet.jsp.jstl</groupId>
	    <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
	    <version>3.0.0</version>
	</dependency>
		
	<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
	<dependency>
	    <groupId>jakarta.servlet</groupId>
	    <artifactId>jakarta.servlet-api</artifactId>
	    <version>4.0.4</version>
	    <scope>provided</scope>
	</dependency>
Then we create a new greeting.jsp (above) page that will emulate the HelloWorld Servlet and test it

6. We demonstrate that we can debug the greeting.jsp in Eclipse (and learn about pre-compile deployment)

We will learn that JSP’s are compiled back into servlets under the covers

In Eclipse we can find the location below the file path of our current workspace The Deploy directory “Eclipse_Workspace”.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps The compile directory “Eclipse_Workspace”.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\jee8webarchetype\org\apache\jsp\greetingjsp.jva Compare and inspect the _jspService method

		/*
		 * Generated by the Jasper component of Apache Tomcat
		 * Version: Apache Tomcat/9.0.56
		 * Generated at: 2022-02-18 10:28:30 UTC
		 * Note: The last modified time of this file was set to
		 *       the last modified time of the source file after
		 *       generation to assist with modification tracking.
		 */

Check in the end git branch of this slide show

JEE 8 JSP is HTML Finish Branch