This is a minimal JSP. It includes a minimal scriplet in which a String variable, message, is instantiated. It then embeds this variable within an HTML level two header.
Scriplets are sections of Java code embedded in JSP pages. They are marked by <% and %> tags. The declaration of the Java String variable is conventional Java code. The reference to the variable is again signaled by a scriptlet, but with an equal sign and the name of the variable indicating that its value should be included in that place.
In the discussion below, a Hello World JSP is shown followed by the servlet generated by t he Tomcat server when the original file is parsed.
Hello World
<HTML> <HEAD> <TITLE>hello jsp</TITLE> <% String message = "Hello, World, from JSP"; %> </HEAD> <BODY> <h2><font color="#AA0000"><%= message%></font></h2> </BODY> </HTML>Run the JSP
Hello World Generated Servlet
When the server processes a .jsp file, it generates a servlet and stores it in a section of the directory structure that it manages. Specifically, Tomcat stores it in a subdirectory called work/Standalone/localhost. Additional subdirectories are created matching the directory structure where the original .jsp file was stored. The name of the generated file will be the same as the original file with a dollar sign appended (e.g., the generated servlet for hello.jsp will be hello$.jsp).
The servlet generated for the above Hello, World example is shown below:
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import org.apache.jasper.runtime.*; public class hello$jsp extends HttpJspBase { static { } public hello$jsp( ) { } private static boolean _jspx_inited = false; public final void _jspx_init() throws org.apache.jasper.runtime.JspException { } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; String _value = null; try { if (_jspx_inited == false) { synchronized (this) { if (_jspx_inited == false) { _jspx_init(); _jspx_inited = true; } } } _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html;ISO-8859-1"); pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true); application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); // HTML // begin [file="/programs/hello.jsp";from=(0,0);to=(4,0)] out.write("<HTML>\r\n\r\n<HEAD>\r\n<TITLE>hello jsp</TITLE>\r\n"); // end // begin [file="/programs/hello.jsp";from=(4,2);to=(6,0)] String message = "Hello, World, from JSP"; // end // HTML // begin [file="/programs/hello.jsp";from=(6,2);to=(11,26)] out.write("\r\n</HEAD>\r\n\r\n<BODY>\r\n\r\n<h2><font color=\"#AA0000\">"); // end // begin [file="/programs/hello.jsp";from=(11,29);to=(11,37)] out.print( message); // end // HTML // begin [file="/programs/hello.jsp";from=(11,39);to=(15,7)] out.write("</font></h2>\r\n\r\n</BODY>\r\n\r\n</HTML>"); // end } catch (Throwable t) { if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (pageContext != null) pageContext.handlePageException(t); } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext); } } }Notice how the generated servlet differentiates between the HTML text and the Java code. Were the code more extensive, it would generate more complex Java expressions than the simple print statement shown here.