Presentation on theme: "Michael Brockway Application Integration JavaServer Pages l Introduction & Overview l Implicit Objects l Scripting l Standard Actions l Directives l References."— Presentation transcript:
Michael Brockway Application Integration JavaServer Pages l Introduction & Overview l Implicit Objects l Scripting l Standard Actions l Directives l References
2 Introduction l References: Deitel & Deitel Java chap 25; Liang, Java Programming, chap 40 l JavaServer Pages (JSPs) are a way of simplifying the delivery of dynamic web content n Underneath, they are servlets, but you do not program them in Java; n Rather, you write XHTML documents with embedded u directives u actions u Scriptlets (fragments of Java code) u tag libraries n 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.
3 Introduction l When a JSP-enabled server receives a request for a JSP n The JSP container (within the server) translates the JSP into a Java servlet n The servlet handles the current and future requests for this JSP n 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. u May respond directly; or u May invoke other Web application components l A first example – clock.jsp n Deploy in Tomcat u...webapps\jspexs\jsp\clock.jsp jspexs is the context root for the present jsp examples n In web browser, go to u http://localhost:8080/jspexs/jsp/clock.jsp
4 Implicit Objects l 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. n javax.servlet.ServletContext application u This object represents the container in which the JSP executes u Scope is “global” – any servlet or JSP in this container can access it n javax.servlet.ServletConfig config u Represents the JSP configuration options u Has page scope: exists just in the current page n java.lang.Throwable exception u Passed to a JSP error page; has page scope. javax.servlet.jsp.JspWriter out (page scope) u Writes text in response to a request u Used implicitly with JSP expressions and actions that output string content java.lang.Object page (page scope) The this reference
5 Implicit Objects l Implicit objects (ctd) javax.servlet.jsp.PageContext pageContext (page scope) u Hides implementation of page’s underlying servlet u Provides access to these implicit objects javax.servlet.http.HttpServletResponse response (page scope) Or some class that implements javax.servlet.ServletResponse u Implements response to client javax.servlet.http.HttpServletRequest request Or some class that implements javax.servlet.ServletRequest u Has request scope: accessible to all pages which are part of this request u Represents the client request javax.servlet.http.HttpSession session u Represents client session information u Has session scope – the clients entire brwosing session
6 Scripting l You write XHTML with embedded JSP scripting components n Scriptlets blocks of Java code delimited by. Java expressions delimited by. Placed in method method _jspService a translation time. n 3 kinds of comments XHTML comments go between and can go anywhere in a JSP except inside scriptlets JSP comments go between 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 n Escape sequences If you want a literal, use %\> For literal ', ", \ use escape sequences \', \", \\
7 Scripting See scripting example welcome.jsp n Deploy in Tomcat u …\webapps\jspexs\jsp\welcome.jsp n http://localhost:8080/jspexs/jsp/welcome.jsp u or n http://localhost:8080/jspexs/jsp/welcome.jsp?firstName=Michael
8 Standard Actions See examples listed at end of these notes l n Dynamically include another referenced resource in a JSP l n Forwards request to another JSP or servlet or static page. n Terminates current processing l n Adds a browser-specific object to a page l
9 Directives l Messages to the JSP container allowing programmer to specifiy page settings Delimited by l Processed at translation time include Directive n See example includeDirective.jsp l Guest Book case study n GuestBean.java n GuestDataBean.java n guestBookLogin.jsp n guestBookView.jsp n guestBookErrorPage.jsp
10 Further Reading l WWW Resources n Look at the links from http://java.sun.com/ Java Enterprise Edition documentationhttp://java.sun.com/ n JavaServer Pages specification u http://java.sun.com/products/jsp/download.html#specs http://java.sun.com/products/jsp/download.html#specs n Tutorial u http://java.sun.com/javaee/5/docs/tutorial//doc/ n The API referece u http://java.sun.com/javaee/5/docs/api/ u Look up packages javax.servlet.jsp and javax.servlet.jsp.tagext l The Deitel et al and Liang textbook references cited above.