JSP Java Server Pages
<% String visitor = request.getParameter( “name” ); if ( visitor == null ) visitor = “World”; %> Hello, !
Elements of JSP ● directives ● scripts ● components ● all created through tags or special notations
Directives or page, include
include Directive ● ● textual inclusion ● semantics same as #include in C++ ● file attribute can be a relative or absolute path ● supports unlimited nesting
page Directive ● ● attributes: – info – description – session – does the page support sessions or not – import – external classes used by scriplets on this page
session Attribute ● session=”true” – this is the default – page supports sessions ● session=”false” – must be set explicitly – can improve performance for pages that don't need sessions
import Attribute ● works like the import statement in a Java program ● can specify multiple classes/packages by separating with commas ● import=”abc.class1” – imports a single class ● import=”xyz.*” – imports an entire package ● java.lang, javax.servlet, javax.servlet.http, and javax.servlet.jsp are imported by default
Scripts ● declarations –... – ● expressions –... – ● scriptlets –... –
Declartions ● variable declartions become instance variables of the servlet class into which the JSP is translated ● method declarations become methods of the same servlet class ● both are accessible throughout the remainder of the page
Special Methods ● public void jspInit() - initialization method called after the servlet is constructed but before the first request processed ● public void jspDestroy() - called after the last request is processed but before the servlet is destroyed
Expression ● the value of the expression is converted to a string and inserted into the output stream where it appears in the JSP
Scriptlet ● arbitrary Java code inserted into the doGet/Post method at the point where it appears in the JSP ● does not generate any output in the output stream unless explicitly told to do so
Implicit Objects ● available to all scriptlets ● JspWriter out – output stream for the response ● HttpServletRequest request – request object (same as servlet request object) ● HttpServletResponse response – response object (same as servlet respons object) ● HttpSession session – container for session objects
Java Beans
Java Beans Fundamentals ● a constructor with no arguments ● matching get*, set* public methods
Using Beans in a JSP ●