1 Java Server Pages • A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted • When the page is first requested, a JSP is parsed into a java source file which is then compiled into a servlet class and run under the container JVM • Initially, there is a performance hit • However, there is a “build” mechanism to avoid unneeded parsing and compilation based on the date of the JSP page relative to the class file
Java Server Pages. A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is first requested, a JSP is parsed into a java source file which is then compiled into a servlet class and run under the container JVM - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Java Server Pages
• A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted
• When the page is first requested, a JSP is parsed into a java source file which is then compiled into a servlet class and run under the container JVM
• Initially, there is a performance hit• However, there is a “build” mechanism to avoid
unneeded parsing and compilation based on the date of the JSP page relative to the class file
2
Java Server Page Tags
• Directives
• Hidden Comments
• Declarations
• Expressions
• Scriptlets
• There are also a number of XML-type tags that begin with “<jsp:’ called “actions”
3
Directives
• Directives are compiler instructions that are processed when the page is compiled
• The format is <%@ and end with %>
• Examples:<%@ page %>
<%@ include %>
4
Directives
• <%@ page %>
• At the top of each JSP is a page declaration<%@ page
language=“java” language (see below)
import=“. . .” list of packages to import
errorPage=“. . .” substitute error page
contentType=“. . .” usually “text/html”
%>
• At present only Java can be used in a JSP
5
Directives
• <%@ include %>• This directive inserts the “static” contents
of a file into the JSP at compile time• The text becomes part of the JSP page• It is useful for including code that is
common to a number of pages, but for which you do not want to use a bean
• Example:<%@ include file="relativeURL" %>
6
Hidden Comments
• These are used to document the page and are not sent to the client
• The format is <%-- comment --%>• Example:
<%-- This is a comment about the code --%>
• Any characters may be used in the body of a comment except the closing "--%>" marker which should be escaped as "--%\>".
7
Declarations
• These are used to declare variables or methods that have scope throughout the page
• The format is <%! declaration or method %>• Examples:
<%! String myString = "hello";
private String getData() {
// Java code for the method goes here
}
%>
8
Declarations
• Every JSP contains implicit references to API objects that do not need to be declared:– request– response– pageContext– session– application– out– config– page– exception
9
Expressions
• The result of an expression is converted to a String and inserted into the output stream at the place where it occurs
• The format is <%= expression %> • Examples:
<%= myString %><%= getData() %>
• Note that the expression is not terminated with a semicolon
• The parser simply wraps an out.print() or similar method around the expression as its argument
10
Scriptlets
• Scriptlets contain fragments of Java code• The format is <% Java code here %>• Scriptlet code can access implicit references and
declared variables or methods• Use a scriptlet to wrap a conditional or a loop
• The <jsp:param> tag is used to pass additional parameters to the target
14
Action: Java Beans
• JavaBeans are the component model for Java - the equivalent of all those fancy components you can obtain and use in the .NET world
• In a component model / market, developers go to the marketplace, pay for the component library they need, and wire it all together in a JSP using their IDE
• This has worked for other languages (e.g. Visual Basic), but has never developed for the Java market
15
Action: Java Beans
• A Java Bean is a class with the following– A default constructor (with no argument list)– A Set and Get method for each attribute (called
a property in Java Bean terms)
• A JSP can use an action to instantiate and access an instance of the Java Bean class
• A JSP can use actions to access the other methods of an instance of the Java Bean
• A JavaBean can be monitored by listeners for changes to its parameters from the JSP
Java Bean
public class MyJavaBean
{
private String propertyName;
public MyJavaBean() { … } // Default Constructor
public void setPropertyName(String theValue)
{ this.propertyName = theValue; }
public String getPropertyName()
{ return this.propertyName; }
}16
17
Action: Java Beans
• <jsp:useBean> and <jsp:setProperty>• Example of how to set up a bean instance