90 likes | 221 Views
Service Computing. Prof. Dr. Ramin Yahyapour IT & Medien Centrum 3 . Novem ber 2009. JSP Scripting Elements – Java Expressions. Current time: <%= new java.util.Date () %>.
E N D
Service Computing Prof. Dr. Ramin YahyapourIT & Medien Centrum3. November 2009
JSP Scripting Elements –Java Expressions Current time: <%= new java.util.Date() %> • A JSP expression is used to insert Java values directly into the output. It has the following form: <%= Java Expression %> • The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run-time (when the page is requested), and thus has full access to information about the request. • Special variables: • request, the HttpServletRequest; • response, the HttpServletResponse; • session, the HttpSession associated with the request (if any); and • out, the PrintWriter used to send output to the client. Your hostname: <%= request.getRemoteHost() %>
JSP Scripting Elements –Java Scriptlets • JSP scriptlets let you insert arbitrary code into the servlet method that will be built to generate the page. • Scriptlets have the following form: <% Java Code %> <% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData); %>
JSP Example <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Using JavaServer Pages</TITLE> </HEAD> <H1>Using JavaServer Pages</H1> <P> Some dynamic content created using various JSP mechanisms: <UL> <LI><B>Expression.</B><BR> Your hostname: <%= request.getRemoteHost() %>. <LI><B>Scriptlet.</B><BR> <% out.println("Attached GET data: " + request.getQueryString()); %> <LI><B>Declaration (plus expression).</B><BR> <%! private intaccessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %> <LI><B>Directive (plus expression).</B><BR> <%@ page import = "java.util.*" %> Current date: <%= new Date() %> </UL> </BODY> </HTML>
JSP Directives <%@ directive attribute1="value1" attribute2="value2" ... attributeN="valueN" %> • JSP Directive: pagelets you do things like import classes, customize the servletsuperclass, and the like;<%@ page import="java.util.*" %> • JSP Directive: include This directive lets you include files at the time the JSP page is translated into a servlet. The directive looks like: <%@ include file="relative url" %>
JSP Actions • JSP actions use constructs in XML syntax to control the behavior of the servlet engine. • Available actions include: • jsp:include - Include a file at the time the page is requested. • jsp:useBean - Find or instantiate a JavaBean. • jsp:setProperty - Set the property of a JavaBean. • jsp:getProperty - Insert the property of a JavaBean into the output. • jsp:forward - Forward the requester to a new page. • jsp:plugin - Generate browser-specific code that makes an OBJECT or EMBED tag for the Java plugin. • Most actions utilize JavaBeans which are covered later in the lecture. <jsp:useBean id="name" class="package.class" />
Bean Example import java.io.Serializable; public class BasicBean implements Serializable { boolean property; public BasicBean() { } public booleangetProperty() { return property; } public booleanisProperty() { return property; } public void setProperty(booleannewValue) { property = newValue; } }
Book Bean import java.io.Serializable; public class BookBean implements Serializable { string author; public BookBean() { author = “Unnamed”; } // Property “author” public string getAuthor () { return author; } public void setAuthor(string newAuthor) { author=newAuthor; } // more properties }