Hello World JSP

This is a minimal JSP.  It includes a minimal scriplet in which a String variable, called message, is declared and 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.  More specialized java constructs may include other markers.  For example, a declaration is marked by <%! and %> tags, and an expression is marked by <%= and %> tags.

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>
<!-- the variable, message, is declared and initialized -->
<%!
String message = "Hello, World, from JSP";
%>
</HEAD>

<BODY>

<!-- the value of the variable, message, is inserted between h2 tags -->
<h2><font color="#AA0000"><%= message%></font></h2>

<h3><font color="#AA0000">
<!-- the java.util.Date method is executed and the result inserted between h3 tags -->
<%= new java.util.Date() %> 
</font></h3>

</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 under a top-level work directory.  Additional subdirectories are created matching the directory structure where the original .jsp file was stored.  The name of the generated file will be some variation of the original file (e.g., hello_jsp).  (Different version of Tomcat and other Servlet containers use different conventions for directory structure and file names.)

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 {

String message = "Hello, World, from JSP";

private static java.util.Vector _jspx_includes;

public java.util.List getIncludes() {
return _jspx_includes;
}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

	JspFactory _jspxFactory = null;
	javax.servlet.jsp.PageContext pageContext = null;
	HttpSession session = null;
	ServletContext application = null;
	ServletConfig config = null;
	JspWriter out = null;
	Object page = this;
	JspWriter _jspx_out = null;


	try {
		_jspxFactory = JspFactory.getDefaultFactory();
		response.setContentType("text/html;charset=ISO-8859-1");
		pageContext = _jspxFactory.getPageContext(this, request, response,
			null, true, 8192, true);
		application = pageContext.getServletContext();
		config = pageContext.getServletConfig();
		session = pageContext.getSession();
		out = pageContext.getOut();
		_jspx_out = out;

		out.write("<HTML>\r\n\r\n");
		out.write("<HEAD>\r\n");
		out.write("<TITLE>hello jsp");
		out.write("</TITLE>\r\n");
		out.write("\r\n");
		out.write("</HEAD>\r\n\r\n");
		out.write("<BODY>\r\n\r\n");
		out.write("<h2>");
		out.write("<font color=\"#AA0000\">");
		out.print( message);
		out.write("</font>");
		out.write("</h2>\r\n\r\n");
		out.write("<h3>");
		out.write("<font color=\"#AA0000\">\r\n");
		out.print( new java.util.Date() );
		out.write("\r\n");
		out.write("</font>");
		out.write("</h3>\r\n\r\n");
		out.write("</BODY>\r\n\r\n");
		out.write("</HTML>");
	} catch (Throwable t) {
		out = _jspx_out;
		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 statements shown here.