java server pages cs-422. what are jsps a logical evolution of java servlets –most servlets...

16
Java Server Pages CS-422

Upload: gabriella-kelly

Post on 25-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Java Server Pages

CS-422

Page 2: 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

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

Page 3: 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

An Example...<!-- JSP that processes a "get" request containing data. --><html xmlns = "http://www.w3.org/1999/xhtml"> <!-- head section of document --> <head> <title>Processing "get" requests with data</title> </head> <!-- body section of document --> <body> <% // begin scriptlet String name = request.getParameter( "firstName" ); if ( name != null ) { %> <%-- end scriptlet to insert fixed template data --%> <h1> Hello <%= name %>, <br /> Welcome to JavaServer Pages! </h1> <% // continue scriptlet } // end if else { %> <%-- end scriptlet to insert fixed template data --%> <form action = "welcome.jsp" method = "get"> <p> Type your first name and press Submit</p>

<p><input type = "text" name = "firstName" /> <input type = "submit" value = "Submit" /> </p> </form> <% // continue scriptlet} // end else %> <%-- end scriptlet --%> </body></html> <!-- end XHTML document -->

Page 4: 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

The rendered screen(s)...

After entering Dick...

Page 5: 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

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

Page 6: 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

JSP Directives

• page

• taglib

• include

Page 7: 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

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”

Page 8: 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

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” %>

Page 9: 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

include directive

• Allows the inclusion of another file into the defining page

• Happens at translation time.

• <%@ include file=“URL” %>

Page 10: 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

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>

Page 11: 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

<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

Page 12: 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

<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:forward>

Page 13: 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

<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

Page 14: 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

<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

Page 15: 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

<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

Page 16: 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

<jsp:getProperty>

• retrieve the value of a Bean property

• attributes:– name - the name of the Bean

– property - the name of the property to retrieve