JavaServer Pages enable Web developers and designers to rapidly develop and easily maintain, information-rich, dynamic Web pages that leverage existing business systems.
www.silenceit.ca
2. Topics What is JSP What is JSTL What is a Servlet? What are Beans? JSP Access Models Benefits and Shortcomings Samples Questions / Comments
3. What is JSP? JavaServer Pages enable Web developers and designers to rapidly develop and easily maintain, information-rich, dynamic Web pages that leverage existing business systems. JSP technology enables development of Web-based applications that are platform independent. JSP technology separates the user interface from content generation, enabling designers to easily change the overall page layout without altering the underlying dynamic content.
4. What is JSTL? The JavaServer Pages Standard Tag Library (JSTL) encapsulates core functionality common to many web applications. single, standard set of tags. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags. Examples: 1: <c:forEach var="item“ items="${sessionScope.cart.items}"> <tr><td> <c:out value=“${item.name}”></td> <td><c:out value=“${item.price}”> </td></tr> </c:forEach> 2: <c:choose> <c:when test='${param.responseText == “Fail"}'> <b>There was an error!</b> </c:when> <c:otherwise> Everything is great! </c:otherwise> </c:choose>
5. What is a Servlet? Java Servlet technology provides Web developers with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systems A servlet can almost be thought of as an applet that runs on the server side--without a face. Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. Servlets can also access a library of HTTP-specific calls and receive all the benefits of the mature Java language, including portability, performance, reusability, and crash protection. Servlet: package silenceit;public class Hello{ public String show(){ return ”Hello”; }} JSP Page: <%@page import=“silenceit.hello" %> <html> <head> <title> Example of page Directive in JSP </title> </head> <body> <% Hello he = new Hello(); out.print(he.show()); %> </body> </html>
6. What are Beans? Java Beans are reusable components. They are used to separate Business logic from the Presentation logic. Internally, a bean is just an instance of a class with special naming conventions and characteristics. Bean Conventions: In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behavior. These conventions make it possible to have tools that can use, reuse, replace, and connect JavaBeans. The class must have a public default constructor. This allows easy instantiation within editing and activation frameworks. The class properties must be accessible using get, set, and other methods (so-called accessor methods and mutator methods), following a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. The class should be serializable. This allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion that is independent of the VM and platform. Example Bean: publicclassPersonBeanimplementsjava.io.Serializable { private String name; privateboolean deceased; /** No-arg constructor*/ publicPersonBean() { } /** * Property */ public String getName() { returnthis.name; } /** * Setter for property Name */ publicvoidsetName(final String name) { this.name = name; } /** * Getter for property "deceased" * Different syntax for a boolean field (is vs. get) */ publicbooleanisDeceased() { returnthis.deceased; } /** * Setter for property deceased */ publicvoidsetDeceased(finalboolean deceased) { this.deceased = deceased; } }
7. JSP Access Models The early JSP specifications advocated two philosophical approaches, popularly known as Model 1 and Model 2 architectures, for applying JSP technology. These approaches differ essentially in the location at which the bulk of the request processing is performed Model 1 Model 2 The Model 2 architecture, shown above, is a server-side implementation of the popular Model/View/Controller design pattern. Here, the processing is divided between presentation and front components. Presentation components are JSP pages that generate the HTML/XML response that determines the user interface when rendered by the browser. Front components (also known as controllers) do not handle any presentation issues, but rather, process all the HTTP requests Although the Model 1 architecture is suitable for simple applications, it may not be desirable for complex implementations. Indiscriminate usage of this architecture usually leads to a significant amount of scriptlets or Java code embedded within the JSP page