JSP Syntax

This lesson provides a brief introduction to JSP syntax and to the various features it supports. While comprehensive, it is sketchy, discussing and illustrating selected parts of its various components.  See the numerous tutorials available on-line for a more thorough discussion.

JSP includes three main categories of elements: 

Each will be introduced, below, and some of their more useful features discussed.


Scripting Elements

Scripting elements allow Java code to be written directly into the JSP page.  It is compiled and available within the page.  There are three kinds of scripting elements:

Scriplets

Scriplets are sections of Java code that are executed in place.  They can be as simple as a declaration of a variable that is treated as an instance variable, but they can also include loops that mix Java and HTML.

The basic form is:

<% Java code %>

Example:

<% String message = "Hello, World, from JSP"; %>

Expressions

Expressions are references to variables, methods, or composable structures of such.  they are evaluated a run time and the results interested into the output stream at the location of the expression.

The basic form is:

<%= Java expression %>

Example:

<h2><font color="#AA0000"><%= message%></font></h2>

Implicit Objects

Several local variables are available to scriptlets and expressions through the ServletContext.  They include the following:

All are self-explanatory except perhaps Application, which is the ServletContext of the JSP.  Recall the following lines from the View JSP:

<%
java.util.Hashtable interestValues = (java.util.Hashtable) 
   request.getAttribute("interestValues");

String jspAmount = (String)interestValues.get("amount"); 
String jspRate = (String)interestValues.get("rate"); 
String jspYears = (String)interestValues.get("years"); 
String jspResult = (String)interestValues.get("result"); 
%>

Declarations

Declarations are used to define variables and methods -- both instance and static -- that are likely to be repeated.  thus, they correspond to writing code at the Java Class level. They can also be used to overwrite jspInit or jspDestroy methods that are created when the JSP is compiled.

The basic form is:

<%! Java variable or method %>

Directives

Directives are instructions to the JSP compiler.  The three types of directives are:

Only the include directive will be discussed here.

The include directive instructs the JSP compiler to fetch the file referenced in the directive and insert it into the page at that point.  Thus, it can be used to include standard boilerplate, such as headers and footers.

The basic form is:

<%@ directive %>

Example:

<%@ include file="header.html" %>

If the included file is HTML, it should not repeat any HTML HEAD or BODY tags that may already be defined in the output stream.

Run Include JSP


Actions

Actions are JSP tags that transfer control to other server objects or perform operations on other objects.  They may also generate output.  They are another means of building view objects within an m - v - c architecture.  

There are some half-dozen action tags. The three that will be discussed here are:

All are marked by tags that have the following form:

<jsp:action  attributes />

Include

The include tag is very similar to the include directive, discussed above.  It allows content to be included in place.  The content may be either static or dynamic. 

Here is an example:

<jsp:include page="header.html" flush="true"/>

Run the JSP

Forward

The forward tag transfer control to a static or dynamic resource, usually referenced by a URL, that exists on the same server.

The basic form is:

<jsp:forward page="forward_page.html"/>

Example:

<jsp:include page="forward_page.html" flush="true"/>

Run the JSP

UseBean

The useBean action is by far the most powerful and the most complex of the JSP actions.  It allows a JSP to create an instance or receive an instance of a Java Bean. 

The JSP could provide typical servlet controller functions by passing the request object to the bean and allowing it to extract user parameters and call back-end functions, just like a servlet.  However, supporting those functions in a servlet is generally considered a better practice.  However, rather than transferring result values to another type, such as a Hashtable, and passing that object to a JSP, if a back-end process returned a result bean back to a controller servlet, it could simply pass this bean to the relevant JSP.

In fact, so long as the Java server supports it and is properly configured, it apermits beans to be passed from servlet to JSP, to another JSP, to another servlet, etc.  This is called chaining and is an architecture used to provide incrementally processing using filtering principles.

In the example code, below, the overall servlet and JSP components are very similar to that shown in the JSP View portion of this lesson.  Consequently, only the servlet's viewIB method is shown along with the JSP.

bean.InterestBean

Because of the way Tomcat constructs the servlet that implements a JSP, unqualified class names are given a default package qualification.  This causes them to be unrecognizable.  Consequently, beans that are to be created and/or passed should be included within a package, as illustrated in the code, below.

package bean;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;

public class InterestBean
{
}  // end InterstBean
 

viewIB Method from Servlet

Note the fully qualified bean passed to the method and the subsequent use of that bean by the RequestDispatcher.

private void viewIB(HttpServletRequest req, HttpServletResponse resp, bean.InterestBean ib)
	throws ServletException, IOException
	{
	    
		RequestDispatcher disp;
		
		disp = getServletContext().getRequestDispatcher("/WEB-INF/classes/jsp/view_useBean.jsp");
		req.setAttribute("interestBean", interestBean);
		disp.forward(req, resp);
		
	}

View JSP

<html>

<title>Comp 117 view jsp</title>

<jsp:useBean id="interestBean" class="bean.InterestBean" scope="request"/>
<%
String jspAmount = interestBean.getAmount();
String jspRate   = interestBean.getRate();
String jspYears  = interestBean.getYears(); 
String jspResult = interestBean.getResult();
%> 
</jsp:useBean>
</head>

<body>
<h2 align="center"><font color="#aa0000">JSP View:<br>
<br>
<i>Interest Servlet UseBean</i></font></h2>
<hr>

<form method="POST" action="http://jbs.cs.unc.edu:8888/jsp/servlet/ServletControllerUseBean">
<!--<form method="POST" action="http://localhost:8888/jsp/servlet/ServletController">-->

<table border="0" width="40%">
<tr>
<td width="50%" align="right">Amount:&nbsp; </td>
<td width="50%"><input type="text" name="amount" value="<jsp:getProperty name='interestBean' property='amount'/>" size="20"></td>
</tr>
<tr>
<td width="50%" align="right">Rate:&nbsp; </td>
<td width="50%"><input type="text" name="rate" value="<%= jspRate%>" size="20"></td>
</tr>
<tr>
<td width="50%" align="right">Years:&nbsp; </td>
<td width="50%"><input type="text" name="years" value="<%= jspYears%>" size="20"></td>
</tr>
<tr>
<td width="50%" align="right">Result:&nbsp; </td>
<td width="50%"><input type="text" name="result" value="<%= jspResult%>" size="20"></td>
</tr>
<tr>
<td width="50%" align="right"></td>
<td width="50%"><input type="submit" value="Compute Results" name="userAction"></td>
</tr>
<tr>
<td width="50%" align="right"></td>
<td width="50%"><input type="submit" value="Error" name="userAction"></td>
</tr>
</table>
</form>

</body>

</html>

Run the UseBean Servlet/JSP