Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOSSE - OO Review Review session A review of other OO technologies and may be useful for exam (not required for assignment) OO web development philosophy.

Similar presentations


Presentation on theme: "OOSSE - OO Review Review session A review of other OO technologies and may be useful for exam (not required for assignment) OO web development philosophy."— Presentation transcript:

1 OOSSE - OO Review Review session A review of other OO technologies and may be useful for exam (not required for assignment) OO web development philosophy JSP and OO MVC models review Struts Tag libaries Ajax Summary Slide 1 Source: j.c.westlake@staffs.ac.uk

2 OOSSE - OO Review OO Software Engineering By now you have experienced UML JSP Let us review the OO tenets (beliefs, principles) and how JSP reflects these tenets What are the tenets of OO? Version 1.1 Dec 2008 Slide 2 Source: j.c.westlake@staffs.ac.uk

3 OOSSE - OO Review UML for web development Can we also reflect on this? How does UML help in web development What diagrams have been useful? What else is available from UML? Answer: Web Application Extensions (WAE) A set of UML modelling aspects designed to help the web developer model web applications Reading herehere Version 1.1 Dec 2008 Slide 3 Source: j.c.westlake@staffs.ac.uk

4 OOSSE - OO Review JSP engine reviewed The JSP engine builds a servlet. The HTML portions of the JavaServer Page become Strings transmitted to print methods of a PrintWriter object. The JSP tag portions result in calls to methods of the appropriate class whose output is translated into more calls to a println method to place the result in the HTML document. Version 1.1 Dec 2008 Slide 4 Source: j.c.westlake@staffs.ac.uk

5 OOSSE - OO Review JSP engine reviewed JSP actually supports real Java code, not a new scripting language. Java code inside JSP is added to methods of a Java Servlet that are generated the first time the JSP is requested. Uses set of pre-established objects by the Web server that they use to generate a dynamic Web page. Version 1.1 Dec 2008 Slide 5 Source: j.c.westlake@staffs.ac.uk

6 OOSSE - OO Review OO web development philosophy Web development... A collection of individual objects, that act on each other Different from traditional view in which a program may be seen as a collection of actions, or simply as a list of instructions Objects capable of receiving messages, processing data, and sending messages to other objects New object that inherits features from existing objects What we are seeing is a move from scripting/HTML websites to a more OO philosophy Version 1.1 Dec 2008 Slide 6 Source: j.c.westlake@staffs.ac.uk

7 OOSSE - OO Review JSP Model 1 architecture JSPs to handle HTTP requests Data layer via Java Beans For small web applications this model is okay but more complex commercial applications a different model is required – Model 2 Version 1.1 Dec 2008 Slide 7 Source: j.c.westlake@staffs.ac.uk

8 OOSSE - OO Review Version 1.1 Dec 2008 Slide 8 Source: j.c.westlake@staffs.ac.uk

9 OOSSE - OO Review JSP Model 2 architecture Also called Web Model-View-Controller (MVC) design see next slide Separates the GUI from the business layer Uses a Java Servlet to receive the HTTP requests from the browser and communicate with the model layer JSPs only used for presentation – the Servlet determines which view to send back to the client Version 1.1 Dec 2008 Slide 9 Source: j.c.westlake@staffs.ac.uk

10 OOSSE - OO Review Version 1.1 Dec 2008 Slide 10 Source: j.c.westlake@staffs.ac.uk

11 OOSSE - OO Review Servlets - “nuts and bolts” The Servlet class extends the HttpServlet class Essentially this means it can use the attributes/methods of this class The HttpServlet class has a number of methods an init( ) method - used for connecting to databases dsn’s it has service method e.g. doGet, doPost, doPut and doDelete the above methods are the “meat” of the HttpServlet class It uses two objects, namely HttpServlet Request and HttpServlet Response Here is an example of the MVC approach link Version 1.1 Dec 2008 Slide 11 Source: j.c.westlake@staffs.ac.uk

12 OOSSE - OO Review Other technology - Apache Struts Interesting web architecture framework http://struts.apache.org/ Designed to handle requests made by the client or by web browsers Apache struts is an open source framework using servlet and JSP See next slide for picture of struts Version 1.1 Dec 2008 Slide 12 Source: j.c.westlake@staffs.ac.uk

13 OOSSE - OO Review Version 1.1 Dec 2008 Slide 13 Source: j.c.westlake@staffs.ac.uk

14 OOSSE - OO Review Other technology - Tag Libaries Custom tags are dynamic page elements. This means they can provide functionality to a webpage using a syntax similar to normal HTML tags. The functionality is up to you… They can provide an alternative (or an additional) mechanism to JavaBeans for dynamic scripting As an example see this link JSP Controls Version 1.1 Dec 2008 Slide 14 Source: j.c.westlake@staffs.ac.uk

15 OOSSE - OO Review What do you need? There are two components to a tag: A Java tag handler class that implements the tag’s functionality. A Tag Library Descriptor (TLD) in the form of an XML file describing the tag. By including this tag on a JSP page you have access to its functionality. Version 1.1 Dec 2008 Slide 15 Source: j.c.westlake@staffs.ac.uk

16 OOSSE - OO Review Java handler class Yes, you have to code the functionality in Java. So why bother? Why not just create a JavaBean instead? For standard webpage scripters, tags are more familiar and provide a simpler mechanism. For web application developers, JavaBeans integrate more fully with the full application environment so are more powerful. Your choice…. Version 1.1 Dec 2008 Slide 16 Source: j.c.westlake@staffs.ac.uk

17 OOSSE - OO Review Making the handler You Java class needs to extend the TagSupport class. This class is only available in the Enterprise version of the Java Development Kit (from version 1.3.1). Or, the Java Web Services Developer Pack. These are free of course… However, you just need the lib/j2ee.jap file (in the case of J2EE) or the lib/servlet.jar for the JWSDP. You can then extract the javax.servlet.jsp.tagext class from the.jar file (using WinZip etc.) and put it into folder containing your tag classes. Version 1.1 Dec 2008 Slide 17 Source: j.c.westlake@staffs.ac.uk

18 OOSSE - OO Review package yvanstags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; public class HelloWorld extends TagSupport { public int doStartTag() { try { JSPWriter out = pageContext.getOut(); out.print(“HelloWorld”); } catch(IOException ioe) { System.out.println(“Error: “ + ioe.getMessage()); } return SKIP_BODY; } Version 1.1 Dec 2008 Slide 18 Source: j.c.westlake@staffs.ac.uk

19 OOSSE - OO Review Making the handler Once you have your custom tag Java code, you need to compile it and put the resultant.class file in your WEB-INF/classes folder. For the HelloWorld example, this would go in: WEB-INF/classes/yvanstags Next you need the descriptor… Version 1.1 Dec 2008 Slide 19 Source: j.c.westlake@staffs.ac.uk

20 OOSSE - OO Review Tag Library Descriptor A TLD is a (fairly) simple XML file that describes a set of custom tags. You can put as many or as few custom tags in a TLD and more than one TLD can be used in a single project. In principle, you could create many useful tags and zip them into one.jar file for easy resusability. Version 1.1 Dec 2008 Slide 20 Source: j.c.westlake@staffs.ac.uk

21 OOSSE - OO Review Tag Library Descriptor <!DOCTYPE taglib PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN” http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd”> 1.0 1.2 mytaglibs Yvan’s Tag Library helloworld yvanstags.HelloWorld empty Display Hello World greeting message Version 1.1 Dec 2008 Slide 21 Source: j.c.westlake@staffs.ac.uk

22 OOSSE - OO Review Using your custom tag To use your custom tag, you need a JSP page. You need to tell your JSP where to find your tag library so you use a page directive: Anywhere in your JSP page, you can then use your custom tag(s): Version 1.1 Dec 2008 Slide 22 Source: j.c.westlake@staffs.ac.uk

23 OOSSE - OO Review JSP custom tag example Tag Library Test Custom tag output: Version 1.1 Dec 2008 Slide 23 Source: j.c.westlake@staffs.ac.uk

24 OOSSE - OO Review Easy tagging The previous method for utilising tag libraries works fine… if you can get your tags to compile. From Tomcat version 5, there is a much simpler method for custom tags. This simpler method does not require any compilation (we just use standard JSP) but there are some rules. Version 1.1 Dec 2008 Slide 24 Source: j.c.westlake@staffs.ac.uk

25 OOSSE - OO Review A simple tag For the display of static/dynamic text, you can create a piece of JSP code. This file needs to be named with a.tag suffix and placed in a folder called tags in your WEB- INF area. Tomcat 5+ will then be able to use your tag from within your normal JSP files using the same mechanism as before. Here’s an example for displaying the date… Version 1.1 Dec 2008 Slide 25 Source: j.c.westlake@staffs.ac.uk

26 OOSSE - OO Review dateTag.tag code <% DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); Date now = new Date(System.currentTimeMillis()); out.println(dateFormat.format(now)); %> Note: there is no page directive for tags, however, there is a tag directive that works in a similar manner. Version 1.1 Dec 2008 Slide 26 Source: j.c.westlake@staffs.ac.uk

27 OOSSE - OO Review date.jsp code Today is Version 1.1 Dec 2008 Slide 27 Source: j.c.westlake@staffs.ac.uk Call the tag code using your prefix and the filename you used for the tag. Specify a shorthand name in the prefix attribute for the tags folder.

28 OOSSE - OO Review Directives DirectiveDescription tag This directive is similar to the page directive for JSP pages. include Use this directive to include other resources from the tag file. taglib Use this directive to use a custom tag library from inside of the tag file. attribute Use this directive to declare an attribute in a tag file. variable Use this directive to define a variable that you can expose to the calling JSP page. Version 1.1 Dec 2008 Slide 28 Source: j.c.westlake@staffs.ac.uk

29 OOSSE - OO Review Tag directive attributes AttributeDescription body-content The information about the body content of this tag. The value can be empty, tagdependent, or scriptless (default). dynamic-attributes Indicates support for dynamic attributes. The value identifies a scoped attribute in which to place a Map containing the names and values of the dynamic attributes passed during this invocation. import Used to import a class or an interface or all members of a package. The same as the import attribute in the page directive. Version 1.1 Dec 2008 Slide 29 Source: j.c.westlake@staffs.ac.uk Note: only the import attribute can appear more than once.

30 OOSSE - OO Review Other technology - Ajax AJAX, shorthand for Asynchronous JavaScript and XML exchanging small amounts of data with the server behind the scenes Speeds up web interaction Version 1.1 Dec 2008 Slide 30 Source: j.c.westlake@staffs.ac.uk

31 OOSSE - OO Review Version 1.1 Dec 2008 Slide 31 Source: j.c.westlake@staffs.ac.uk

32 OOSSE - OO Review Summary Doing development in an OO style is beneficial even if the software being used is not directly OO For the enterprise level OO is evident in the selection of technologies mentioned Version 1.1 Dec 2008 Slide 32 Source: j.c.westlake@staffs.ac.uk


Download ppt "OOSSE - OO Review Review session A review of other OO technologies and may be useful for exam (not required for assignment) OO web development philosophy."

Similar presentations


Ads by Google