Java Server Pages CS-422
What are JSPs •
A logical evolution of java servlets – most servlets dynamically create HTML and integrate it with some computational and database output
•
Servlets on their own had some pretty stiff competition form PHP, ASP and ColdFusion – these technologies for the most part (especially ColdFusion) don’t need a programmer; ASP and PHP definitely have a programming language in their background but scripting in VBScript or PHP is still easier than Java
•
•
some one at Sun recognized that a servlet was just some Java with HTML embedded in it, and thought: why not embed some Java in regular HTML; then create a class that will scan through it and make the source code for a servlet, then compile it and deploy it The Java Server Page is born
An Example...
Processing "get" requests with data <% // begin scriptlet String name = request.getParameter( "firstName" ); if ( name != null ) { %> <%-- end scriptlet to insert fixed template data --%>
Hello <%= name %>,
Welcome to JavaServer Pages!
<% // continue scriptlet } // end if else { %> <%-- end scriptlet to insert fixed template data --%>
<% // continue scriptlet } // end else %> <%-- end scriptlet --%>
The rendered screen(s)... After entering Dick...
Embedding Java in HTML •
•
Embedding Java in HTML is pretty simple; the lead is taken from PHP and ASP; we’ll make a special escape character “<%”, “<%=“ and “%>” the tag pair <% …%> are used for defining scriptlets; a sequences of Java statements – scriptlets are used to glue the parts of your jsp together (see prev. example)
•
the tag pair <%=…%> is used for Java expression evaluation – ex <%= new java.util.Date( ) %> will be replaced with the current Date/Time
JSP Directives •
page
•
taglib
•
include
page directive • • • • • • • • • • • •
Language – “scriptingLanguage” Extends – “classname” Import – “importList” – java.lang.*, javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.* are imported implicitily Session “true|false” Buffer – “none|sizekb” autoFlush- “true|false” isThreadSafe – “true|false” Info – “info_text” errorPage – “error_URL” isErrorPage – “true|false” contentType – “contentInfo” pageEncoding – “pageEncodingInfo”
taglib directive •
Import a taglib into the page
•
Associate taglib with a URI
•
Map the URI to,a prefix for use within the page
•
<%@ taglib uri=“taglib URI” prefix=“prefix” %>
include directive • •
Allows the inclusion of another file into the defining page Happens at translation time.
•
<%@ include file=“URL” %>
JSP Actions (Tags) •
JSP provides a set of standard actions to help you with commonly done tasks: – – – – – – –
<jsp:include> <jsp:forward> <jsp:plugin> <jsp:param> <jsp:useBean> <jsp:setProperty> <jsp:getProperty>
<jsp:include> •
Attributes: – page - specifies the relative URI path to the resource to include – flush - must be “true” ; specifies whether the buffer should be flushed after the include is performed – <%jsp:include file=“myfile.html” flush=true> – use this action to include dynamic content; it will be included each time it is included
•
there is also an include directive – <%@ include file = relative URI> – use this for including static content, is included only once
<jsp:forward> •
•
Allows you to forward processing to another JSP, as soon as processing is forwarded the processing of the curreent JSP is terminated can be used in conjunction with <jsp:param> to forward parameter information to the forwarded JSP <jsp:forward page=“nextjsp.jsp”> <jsp:param name = date value=“<%=java.util.Date() %>” />
<jsp:plugin> •
•
Adds an applet or JavaBean to a Web page in the form of a browser=specific object or embed XHTML element. It also allows the client to download and install the java Plug-in if it is not already present. Attributes: – – – – – – – – – – – – – –
type - bean or applet code - Class that represents the component codebase- location of the class specified in the code attribute and the archives specified in the archives attribute align - Alignment od the component (left,right,center) archive - space separated list of archives (JARS) containing resources height - component height in pixels hspace - number of pixels to right and left of component jreversion - version of the JRE required name - name of component vspace - number of pixels above and below the component title - Text describing the component width - component width in pixels or percent nspluginurl - location of plugin for Netscape Navigator iepluginurl - location of plugin for Internet Explorer
<jsp:useBean> • •
Enables a JSP to manipulate or use a JavaBean. The action creates or locates an existing object for use in the JSP. Attributes: – – – –
id - the name used to manipulate the Java object (case sensitive) scope - page, request,session,or application (default is page) class - fully qualified class name of the Java object beanName - The name that can be used with method instantiate of class java.beans.Beab to load a JavaBean into memory. – Type - type of JavaBean, can be same as class attribute , a superclass of that type or an interface implemented by that type
<jsp:setProperty> • •
Used to set Bean property values Attributes: – – – –
name - Id of the Bean for which a property is to be set property - name of the property to set param - specify which request parameter is to be used value - the value to assign to a Bean proberty
<jsp:getProperty> • •
retrieve the value of a Bean property attributes: – name - the name of the Bean – property - the name of the property to retrieve