100 likes | 214 Views
JavaServer Pages. Introduction & Overview Implicit Objects Scripting Standard Actions Directives References. Introduction. References: Deitel & Deitel Java chap 25; Liang, Java Programming , chap 40
E N D
JavaServer Pages Introduction & Overview Implicit Objects Scripting Standard Actions Directives References
Introduction • References: Deitel & DeitelJava chap 25; Liang, Java Programming, chap 40 • JavaServer Pages (JSPs) are a way of simplifying the delivery of dynamic web content • Underneath, they are servlets, but you do not program them in Java; • Rather, you write XHTML documents with embedded • directives • actions • Scriptlets (fragments of Java code) • tag libraries • Accessible to non Java programmers • Even experienced Java programmers are prone to errors when writing a lots of printlln(...) statements to a reponse object’s printWriter, in order to create a dynamic web page: it is easier to write the web page and embed the dynamic bits.
Introduction • When a JSP-enabled server receives a request for a JSP • The JSP container (within the server) translates the JSP into a Java servlet • The servlet handles the current and future requests for this JSP • Errors compiling the servlet result in translation-time errors. • JSP container places the Java statements that implement the JSP’s response in method _jspService at translation time. • If the JSP compiles correctly, the JSP container invokes method _jspService to process the request. • May respond directly; or • May invoke other Web application components • A first example – clock.jsp • Deploy in Tomcat • ...webapps\jspexs\jsp\clock.jsp • jspexs is the context root for the present jsp examples • In web browser, go to • http://localhost:8080/jspexs/jsp/clock.jsp
Implicit Objects • A JSP page is XHTML with embedded pieces of Java. These java statements can refer to a number of implicit objects associated with the page, or the HTTP request which led to the production of the page, or with the browser session. • javax.servlet.ServletContext application • This object represents the container in which the JSP executes • Scope is “global” – any servlet or JSP in this container can access it • javax.servlet.ServletConfig config • Represents the JSP configuration options • Has page scope: exists just in the current page • java.lang.Throwable exception • Passed to a JSP error page; has page scope. • javax.servlet.jsp.JspWriter out (page scope) • Writes text in response to a request • Used implicitly with JSP expressions and actions that output string content • java.lang.Object page (page scope) • The this reference
Implicit Objects • Implicit objects (ctd) • javax.servlet.jsp.PageContext pageContext (page scope) • Hides implementation of page’s underlying servlet • Provides access to these implicit objects • javax.servlet.http.HttpServletResponse response (page scope) • Or some class that implements javax.servlet.ServletResponse • Implements response to client • javax.servlet.http.HttpServletRequest request • Or some class that implements javax.servlet.ServletRequest • Has request scope: accessible to all pages which are part of this request • Represents the client request • javax.servlet.http.HttpSession session • Represents client session information • Has session scope – the clients entire brwosing session
Scripting • You write XHTML with embedded JSP scripting components • Scriptlets • blocks of Java code delimited by <% and %>. • Java expressions delimited by <%= and %>. • Placed in method method _jspService a translation time. • 3 kinds of comments • XHTML comments go between <!-- and --!> and can go anywhere in a JSP except inside scriptlets • JSP comments go between <%-- and --%> and can go anywhere in a JSP except inside scriptlets; do not appear in response to client • Java comments between /* and */ or between // and end-of-line can go inside scriptlets • Escape sequences • If you want a literal <%, use escape sequence <\%; for literal %>, use %\> • For literal ', ", \ use escape sequences \', \", \\
Scripting • See scripting example welcome.jsp • Deploy in Tomcat • …\webapps\jspexs\jsp\welcome.jsp • http://localhost:8080/jspexs/jsp/welcome.jsp • or • http://localhost:8080/jspexs/jsp/welcome.jsp?firstName=Michael
Standard Actions • See examples listed at end of these notes • <jsp:include> • Dynamically include another referenced resource in a JSP • <jsp:forward> • Forwards request to another JSP or servlet or static page. • Terminates current processing • <jsp:plugin> • Adds a browser-specific object to a page • <jsp:param • Use with the 3 actions above to specify parameters as name,value pairs • <jsp:useBean> • Specifies that the page uses a given JavaBean instance: • Specifies scope of bean • Assigns it an ID that scripting components can refer to • <jsp:setProperty> • in a given JavaBean instance • <jsp:getProperty> • in a given JavaBean instance • Converts to String for output to response
Directives • Messages to the JSP container allowing programmer to specifiy page settings • Delimited by <%@ and %> • Processed at translation time • include Directive • See example includeDirective.jsp • Guest Book case study • GuestBean.java • GuestDataBean.java • guestBookLogin.jsp • guestBookView.jsp • guestBookErrorPage.jsp
Further Reading • WWW Resources • Look at the links from http://java.sun.com/ Java Enterprise Edition documentation • JavaServer Pages specification • http://java.sun.com/products/jsp/download.html#specs • Tutorial • http://java.sun.com/javaee/5/docs/tutorial//doc/ • The API referece • http://java.sun.com/javaee/5/docs/api/ • Look up packages javax.servlet.jsp and javax.servlet.jsp.tagext • The Deitelet al and Liang textbook references cited above.