Web programming

Units WEB1P and WEB2P

Web programming in Java 2: Java Server Pages

Java Server Pages (JSP)

Read chapters 7 and 8 of Basham.

JSP is an extension of servlet technology. In fact, given a JSP, a servlet container simply translates it into a servlet that is then executed like any other.

A servlet might be described as a Java program with HTML in it; a JSP might be described as an HTML page with Java in it.

The great advantage of a JSP over a servlet is the same as that of a PHP page over a Perl script - the program looks like an HTML page rather than a computer program. This means that web page editors (e.g. Dreamweaver) can be used to get the look and feel of the page right. Dreamweaver also provides some support for the inclusion of Java code in the page.

JSPs can be located anywhere in your web application (except in the WEB-INF directory).

To make a JSP dynamic, you embed Java code in amongst the HTML. The scripting components are:

  Purpose Example Explanation of example
Scriptlets A block of Java code to be executed at that point in the page
<%

String name = request.getParameter("name");
%>
Create a new variable "name" of type "String"and assign to it the "name" parameter from the input form.
Expressions A Java expression whose value is incorporated into the page
<%= name %>
Include the value of variable "name" in the output
Declarations A Java declaration (variable or method)
<%! int counter = 0; %>
Declare and initialise a variable called "counter"
Actions Java bean interaction and page manipulation
<jsp:include page="header.jsp"/>
Include the contents of the page "header.jsp" at runtime
Directives Specify page settings
<%@include file="header.jsp" %>
Include the contents of the page "header.jsp" at translation time
Comments Obvious!
<%--
This is a JSP comment
--%>

Contrast this with

<--
This is an HTML comment
-->

Consider this simple example of a JSP (PDF format) which does almost exactly the same as the servlet considered above.

It is generally agreed that using scriplets, declarations and expressions is a bad thing. Their use leads to cluttered unreadable code, where Java and HTML get hopelessly mixed up, but you need to know about them in case you see a page that includes them.

Most modern JSP pages use only directives and an occasional action, and do the rest of their task using custom tags (see below).

Java tag libraries

A further refinement of JSP is the use of tag libraries. You can either use the Java Standard Tag Libraries (JSTL) or define your own custom tag libraries.

Tag libraries try to obviate the need to include any Java code in the page, thereby making the page XHTML compatible and therefore more likely to be processable with XML tools.

JSP 2.0 also introduced the Expression Language, a means of accessing Java Beans and other objects directly in the HTML page. This reduces the number of tags that need to appear.

  Purpose Example Explanation of example
JSTL tag Represent standard action by a page component
<c:if test="${status.totalVisits == 1000000}">
You are the millionth visitor to our site!
Congratulations!
</c:if>
Specialised message based on property of a bean.
Custom tag Represent custom action by a page component
<jim:display value="${record.lastUpdated}" format="large"/>
Call customised display routine (with attribute).
Expression language element Incorporate bean properties in page
Hello ${me.name}
Include bean property in page without tag
 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The web programming units include some material that was formerly part of the WPRMP, WECPP, WPSSM and WEMAM units.