260 likes | 465 Vues
JSP (Java Server Pages). DotCom Offsite Meeting 00 Raymond Gao September 28, 2000. HTML Web Server Model. PCs. <html>. HTML, CSS, JHTML, SHTML JSPs, Servlets, and Applets. Web Server. Java Beans, JMS, EJB (Session Beans). Application Server. JDBC, EJB (Entity Beans). EIS.
E N D
JSP (Java Server Pages) DotCom Offsite Meeting 00 Raymond Gao September 28, 2000
HTML Web Server Model PCs <html>
HTML, CSS, JHTML, SHTML JSPs, Servlets, and Applets Web Server Java Beans, JMS, EJB (Session Beans) Application Server JDBC, EJB (Entity Beans) EIS Java In The 3-Tier Web Architecture <html> Javascripts, CSS, applets </html>
What is JSP? • Separates the presentation from the business logic • Extends the Java servlet technology • Dynamic page reloading
JSP Extends The Java Servlet Technology • Includes 2 main packages • javax.servlet.jsp.* • javax.servlet.jsp.tagext.* • Lifecycle of JSP • Precompiling --> Dynamic Reloading • jspInit() --> _jspService() --> jspDestroy()
JSP’s Main Components • Scriptlet <% %> <% code fragment %> • Directives <%@ page …%> <%@ include …%> • TagLib Directive <%tag uri=“…” prefix=“…”%> • Beans <jsp:useBean …>
First JSP Example - HelloWorld <html> <head> <title>Greetings</title> </head> <body> <% for(int i=0;i<5;i++) { %> <h1>Hello World!</h1> <% } %> </body> </html>
A little bit more fancy <HTML> <HEAD><TITLE>Creating Hello World with Incremental Text Size Increase</TITLE></HEAD> <% for(int i = 1;i < 8;i++) { %> <FONT SIZE=<%=i%>>Hello World</FONT><BR> <% } %> <% for(int i = 7;i > 0;i--) { %> <FONT SIZE=<%=i%>>Hello World</FONT><BR> <% } %> </BODY> </HTML>
What about using variables? <HTML> <HEAD><TITLE>Creating Hello World with Incremental Text Size Increase</TITLE></HEAD> <% for(int i = 1;i < 8;i++) { %> <FONT SIZE=<%=i%>>Hello World</FONT><BR> <% } %> <% for(int i = 7;i > 0;i--) { %> <FONT SIZE=<%=i%>>Hello World</FONT><BR> <% } %> <Font size = 5> <% long t_mill = System.currentTimeMillis(); java.util.Date d = new java.util.Date (t_mill); out.println("The current time is --> " + d); %> </FONT><BR> </BODY> </HTML>
Look at the Dynamic Compiling • From: • H:\apps2\Allaire\JRun\servers\default\RG\OFFSITE00 • To: • H:\apps2\Allaire\JRun\servers\default\RG\WEB-INF\jsp • JSP • jspInit() --> _jspService() --> jspDestroy()
Getting user Inputs - Page 1 <HTML> <TITLE>Login</TITLE> <BODY BGCOLOR=#FFFFFF> <CENTER> <FORM METHOD=post ACTION=/rg/OFFSITE00/login.jsp> <TABLE BORDER=2 CELLPADDING=4 CELLSPACING=0 BACKGROUND=/rg/OFFSITE00/images/marble.gif><TR><TD> <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0> <TR> <TD><FONT FACE=Arial,Helvetica> <B>USERNAME</B> </FONT></TD> <TD><FONT FACE=Arial,Helvetica> <INPUT TYPE=text NAME=user SIZE=10> </FONT></TD> </TR> <TR> <TD><FONT FACE=Arial,Helvetica> <B>PASSWORD</B> </FONT></TD> <TD><FONT FACE=Arial,Helvetica> <INPUT TYPE=password NAME=pass SIZE=10>
Getting user input - page 2 </FONT></TD> </TR> <TR> <TD COLSPAN=2 ALIGN=right><FONT FACE=Arial,Helvetica> <INPUT TYPE=submit VALUE=login> </FONT></TD> </TR> </TABLE> </TD></TR></TABLE> </FORM> The user name is <b><%= request.getParameter("user") %></b><BR> The password is <b><%= request.getParameter("pass") %></b> </CENTER> </BODY> </HTML>
What is a Java Bean? • Java Bean is reusable components to be visually manipulated. • Java Bean relies on accepted convention. • setXXX() method. • getXXX() method. • Java Bean allows reflection and introspection. -- BeanInfo class.
JSP Tag library Overview • Why? Better separation of the “noun” (tags & the content) from the “verb” (Java code and scripts). • How? - It has 4 pieces • Java tag classes • JSP file • TLD - taglib.tld • web.xml - optional
JSP Methods That Are Pertinent To Tag Library setPageContext(PageContext pctx) setParent(Tag Parent) doStartTag() doInitBody() - BodyTag doAfterBody() - BodyTag doEndTag() Release()
JSP TagLib Example -The Java File The Java File - Page 1 import java.util.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class Greetings implements Tag { private PageContext pageContext; private Tag parent; private String world = "world"; public int doStartTag() throws javax.servlet.jsp.JspTagException { return SKIP_BODY; }
JSP TagLib Example - The Java File The Java File - Page 2 public int doEndTag() throws JspTagException { JspWriter out = pageContext.getOut(); try { out.println(“Today is “ + new Date()); out.println("Hello "+ world); } catch (Exception ex) { throw new JspTagException(ex.getMessage()); } finally { return EVAL_PAGE; } }
JSP TagLib Example - The Java File The Java File - Page 3 public void release() {} public void setPageContext(final PageContext pageContext) { this.pageContext = pageContext; } public void setParent(javax.servlet.jsp.tagext.Tag parent) { this.parent=parent; } public Tag getParent() { return parent; } }
JSP TagLib Example -The TLD File The TLD File <?xml version="1.0"?> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>rgdemo</shortname> <info>My Tag Library Demo</info> <tag> <name>greetings</name> <tagclass>Greetings</tagclass> <bodycontent>empty</bodycontent> </tag> </taglib>
JSP TagLib Example - The JSP File The JSP file <html> <head> <title>ray's jsp demo with taglib</title> </header> <body> <%@ taglib uri="WEB-INF/rgdemo.tld" prefix=“rgdemo” %> <p> <rgdemo:greetings/></p> </body> </html>
Where To Get More Information About JSP • http://www.javasoft.com/products/jsp • http://www.javasoft.com/products/servlet • http://www.javasoft.com/j2ee • http://www.jsptags.com • jsp-interest@java.sun.com