1 / 18

Java Server Pages

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.

Download Presentation

Java Server Pages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Server Pages

  2. 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

  3. 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

  4. 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

  5. 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

  6. JSP Code • <% code %> • Just executed • <% for(int j = 0; j < 8; j++) { %> <p><font size=<%= j %>>Hi</font> <% } %> • <%-- JSP Comment --%> • <!– HTML Comment -->

  7. JSP Declarations • <%! Code %> • Variable declarations • <%! private int accessCount = 0; %> Accesses: <%= ++accessCount %> • Variables have class scope • Shared between all instances

  8. XML Syntax • Expressions • <jsp:expression> Expression </jsp:expression> • Scriptlets • <jsp:scriptlet> Code </jsp:scriplet> • Declarations • <jsp:declaration> Code </jsp:declaration>

  9. 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

  10. 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

  11. 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

  12. 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” />

  13. My First Bean public class MessageBean { private String message = "No String Specified"; public String getMessage() { return (message); } public void setMessage(String theMessage) { message = theMessage; } }

  14. 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

  15. 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

  16. 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); } }

  17. 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>

  18. 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

More Related