1 / 21

JSP structure and models

JSP structure and models. Format of lecture: Assignment context JSP models JSPs calling other JSPs i.e. breaking up work Parameter passing JSPs with Add SQL examples Summary. JSP Built-in Objects.

Télécharger la présentation

JSP structure and models

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. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk JSP structure and models • Format of lecture: • Assignment context • JSP models • JSPs calling other JSPs i.e. breaking up work • Parameter passing • JSPs with Add SQL examples • Summary

  2. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk JSP Built-in Objects • JSPs have access to several objects that we can use when processing a request from a browser and then generate a dynamic response. • There are eight in all: request out response pagecontext session application config page

  3. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Built-in objects • You will see these objects being used in the examples provided during the module coverage of JSPs. • Request objects methods allow us to access information about: • The user • HTTP headers • Clients machine • Request URL and parameters.

  4. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Built-in objects • Response objects methods allow access to: • Setting HTTP headers. • Set cookies. • Encode session information into URLs.

  5. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Built-in objects • Out object - has methods to generate and control output to the browser. • PageContext object - has page attributes. • Page object - represents the servlet instance i.e. the object generated from having compiled the JSP. • Application object - used for extracting information about the lifecycle of the Servlet.

  6. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk The Structure of JSPs • An initial selection of tags to get to know (Scriptlet Tags = v. important!!!). <% //embed Java code %> • Used for embedding blocks of Java code into JSPs. • Scriptlets provide control structures like decision (if and switch) and iteration control structures (for and while).

  7. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Structure of JSPs • Declaration Tags – also important. <%! // Variable declaration - method declaration %> • Used for declaring variables and methods. For example: <%! HttpSession ses = null; %> <%! public String whereFrom(HttpServletRequest req) { ses = req.getSession(); return req.getRemoteHost(); } %> <% out.print("Hi there, I see that you are coming in from "); %> <%= whereFrom(request) %>

  8. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Structure of JSPs • Expression Tags. <%= java expression %> • Used for inserting the value of a Java expression into JSPs. e.g. <td><%= rs.getString("LastName") %></td>

  9. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Structure of JSPs • Directive Tags – used to provide information to the JSP server. • There are three directives: • page • include • taglib • We will only review the page and include directive. • Page Directive - used to set properties of the JSP. E.g. what packages it imports from the Java library or is an error page. <%@ page page_attributes %> e.g. <%@ page import="java.sql.*" %> <%@ page errorPage=“myErrorPage.jsp" %>

  10. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Catching Errors – myErrorPage.jsp • <%@ page isErrorPage="true" %> • <HTML> • <HEAD><TITLE>My Error Page</TITLE></HEAD> • <BODY> • <H2>Exception Information</H2> • <TABLE> • <tr><td>Exception Class: </td><td><%= exception.getClass() %></td></tr> <tr><td>Message: </td><td><%= exception.getMessage() %></td></tr> </TABLE> • </BODY> • </HTML>

  11. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk JSP Action Tags • Action tags are used to perform actions when a JSP page is requested by a browser. • There are six JSP action tags: • useBean • setProperty • getProperty • Include • Forward • plugin • We will review two of them in during the module: • include • forward

  12. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk include action and directive • Actions and Directives – there is a difference: • Actions are elements that can create and access programming language objects and affect the output stream • include Action. <jsp:include page= “Relative_URL” /> • Used to include the output of another JSP • include Directive. <%@ include file=“Relative_URL_to_file” %> • Used to include the text of another file • There are examples of both on the Tomcat server

  13. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk include example • <body> • <%@ page buffer="5" autoFlush="false" %> • <p>In place evaluation of another JSP which gives you the current time: • <jsp:include page="clock.jsp" flush="true"> • <jsp:param name="type" value="small"> • </jsp:include> • </html> • See: http://java.sun.com/products/jsp/tags/12/syntaxref1214.html

  14. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Action or Directive? • Well both have their pros and cons and, as always, it just depends on what your requirements are. The benefits of using the <jsp:include/> ACTION are: • Guarantees automatic recompilation, • Smaller class sizes, • Can make use of parameters (treated like form parameters) • JSP expressions can be used in attribute values • One of the main benefits of using the include DIRECTIVE is that local page variables can be shared between the two files. It also has slightly better run time efficiency and doesn't restrict output buffering.

  15. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk forward Action • <jsp:forward page=“Relative_URL” /> • Used to forward the request to another JSP • This will forward the current request object to the specified file • When a forward tag is encountered, the JSP engine doesn't process the remainder of the current JSP file • Anything that has been written into the output buffer by the current JSP file will be lost • The following example tests the virtual memory and either forwards to another .jsp or another html page - see code screen grab on next page…

  16. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk forward Action example • <% • double freeMem = Runtime.getRuntime().freeMemory(); • double totlMem = Runtime.getRuntime().totalMemory(); • double percent = 100*freeMem/totlMem; • if (percent < 50.0) { • %> • <jsp:forward page="/jsp/forward/one.jsp"/> • <% } else { %> • <jsp:forward page="two.html"/> • <% } %> • </html>

  17. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Forward model The JSP parses the input from the client form (and optionally updates the Database) • Forward model Client Request HTTP Server - Tomcat JSP HTML Form (optional) SQL JSP Response (optional) DATABASE

  18. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Demos in server folder • CRUD examples • Create(add);Retrieve(view/get); • Adding information to a database • Demo of a add JSP (addtousers.jsp) • Retrieving (getting) information from the Guestbook database with JSPs • Demo of a simple Get JSP (SimpleGetUserExample.jsp) • Demo of a user input version of get (GetUserUsingInputExample.jsp)

  19. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk addtousers.jsp

  20. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk GetUserUsingInputExample.jsp

  21. j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk Summary • You have learnt more about the structure of JSPs • Learning from examples is good • You have seen an Add something JSP • You have seen a more sophisticated view something JSP (compared to last week with the simple guest listing)

More Related