180 likes | 344 Views
Java Server Pages. Java Server Pages. Servlets are nice, but… It’s a pain to put in all those out.println stmts JSPs mix Java and HTML Within the same document Nice for team based projects Web page designers don’t need to know Java Programmers don’t need to know design. My First JSP.
E N D
Java Server Pages • Servlets are nice, but… • It’s a pain to put in all those out.println stmts • JSPs mix Java and HTML • Within the same document • Nice for team based projects • Web page designers don’t need to know Java • Programmers don’t need to know design
My First JSP <html> <head> <title>Greetings</title> </head> <body> <% for(int i=0;i<8;i++) { %> <p><font size=<%=i%>>Hello World!</font> <% } %> </body> </html> http://clotho/~snell/TestApps/HelloWorld.jsp
Java Server Pages • Allow you to insert code into the HTML • Expressions <%= expression %> • Scriptlets <% code %> • Declarations <%! code %> • Upon access, the JSP is converted to a servlet then executed • Same life cycle as a servlet
JSP Expressions • <%= Java Expression %> • Evaluated, Converted to a string, Inserted • Current Time: <%= new java.util.Date() %> • Predefined Variables • request the HttpServletRequest • response the HttpServletResponse • session the HttpSession • out the PrintWriter • application the ServletContext • config the ServletConfig • pageContext the PageContext
JSP Code • <% code %> • Just executed • <% for(int j = 0; j < 8; j++) { %> <p><font size=<%= j %>>Hi</font> <% } %> • <%-- JSP Comment --%> • <!– HTML Comment -->
JSP Declarations • <%! Code %> • Variable declarations • <%! private int accessCount = 0; %> Accesses: <%= ++accessCount %> • Variables have class scope • Shared between all instances
XML Syntax • Expressions • <jsp:expression> Expression </jsp:expression> • Scriptlets • <jsp:scriptlet> Code </jsp:scriplet> • Declarations • <jsp:declaration> Code </jsp:declaration>
JSP directives • Affect the overall structure of the page • <%@ directive attribute=“value” %> • The page directive • <%@ page import=“java.util.*” %> • <%@ page contentType=“text/plain” %> • <%@ page session=“true” %> <%-- default --%> • The include directive • <%@ include file=“Navbar.jsp” %> • Translation time • <jsp:include page=“Navbar.jsp” flush=“true”/> • Request time
Java Beans • What is a bean? • Data structure that conforms to certain rules • Bean rules • Must have a zero argument constructor • No public instance variables • Persistent values set/accessed through • setXxx • getXxx • Mostly used for persistent storage of data
Bean Usage • <jsp:useBean id=“name” class=“pkg.class” /> • Similar to: • <% name = new pkg.class(); %> • More powerful • Scope (page, application, session, request) • Bean Location • Must be in server’s regular class path
Bean Usage • Getting values • <jsp:getProperty name=“className” property=“variableName” /> • Setting values • <jsp:setProperty name=“className” property=“variableName” value=“The String Value” /> • Or param=“NumberVariable” />
My First Bean public class MessageBean { private String message = "No String Specified"; public String getMessage() { return (message); } public void setMessage(String theMessage) { message = theMessage; } }
The JSP … <jsp:useBean id="myBean" class="MessageBean" scope="session" /> <ol> <li>Initial Value: <i><jsp:getProperty name="myBean" property="message" /></i> <jsp:setProperty name="myBean" property="message" value="Howdy" /> <li>After jsp:setProperty : <i><jsp:getProperty name="myBean" property="message" /></i> <% myBean.setMessage("After Scriptlet"); %> <li>After scriptlet : <i><%= myBean.getMessage() %></i> </ol> http://clotho/~snell/TestApps/MessageBean.jsp
Custom JSP Tags • Tags encapsulate complex behaviors • Make them simple and accessible • Tags are essentially function calls to Java code • Consist of two pieces • The Tag Handler (Java code) • Defines the action • The Tag Library Descriptor (xml) • Identifies the tag to the server
My First Custom JSP Tag Handler import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; // Simple JSP tag that just inserts the string "Simple Example Tag" public class SimpleTagExample extends TagSupport { public int doStartTag() { try { JspWriter out = pageContext.getOut(); out.print("Simple Example Tag"); } catch(IOException ioe) { System.out.println("Error in ExampleTag"); } return(SKIP_BODY); } }
The Tag Library Descriptor <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>simpletag</shortname> <urn></urn> <info> My tag example. </info> <tag> <name>example</name> <tagclass>SimpleTagExample</tagclass> <info>Simple example</info> <bodycontent>EMPTY</bodycontent> </tag> </taglib>
The JSP <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transistional//EN"> <html><head> <%@ taglib uri="simpletagexample-taglib.tld" prefix="simpletag" %> <title><simpletag:example /></title> </head> <body> <h1><simpletag:example /></h1> Here is the output from the tag: <i><simpletag:example /></i> </body> </html> http://clotho/~snell/TestApps/SimpleTagExample.jsp