JSP

  • JSP (JavaServer Pages)
  • Allows Java within HTML pages
  • JSP tags are processed on a web server; the resulting HTML is sent to the client
  • The usage of JSP tags is comparable to the usage of PHP or ASP tags
  • An extension of Java Servlets
  • Tomcat supports JSP and Java Servlets

JSP tags

<%  java-expressions %>                          Scriptlet
<%= single-java-expression-to-output %> Expression
<%! java-declaration-expressions %> Declaration

<%@ [page, include, taglib] jsp-directive %> Directive

<%@ page import="java.sql.*" %>
<%@ include file="filename.jsp" %>

<%@ page language="java" %>

Input / output

  • out.println sends output to the browser
  • System.out.println sends output to the server log

Comments

<%-- Comment --%>

Hello, world

<%= "Hello, World!" %>

JSP tag libraries

  • JSP Tag Extension API
  • Allows developers to define and implement a set of custom JSP tags
  • A Java class that implements one of the Tag interfaces is written for each custom tag
  • An XML file (.tld) defines the custom tags and the Java classes that implement the tags
<%@ taglib uri="taglib.tld" prefix="prefix" %>
<prefix:tag> ... </prefix:tag>
public class MyTag extends TagSupport {
// Constructor
public MyTag() { ... }

// Release instance variables
public void release() { ... }

// Called for the start tag
public int doStartTag() { ... }

// Called at the end tag
public int doEndTag() { ... }
}