Java Server PagesJSP, JSTL, and Servlets
TopicsWhat is JSPWhat is JSTLWhat is a Servlet?What are Beans?JSP Access ModelsBenefits and ShortcomingsSamplesQuestions / Comments
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.
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>
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 systemsA 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>
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;   }}
JSP Access ModelsThe 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 performedModel 1Model 2The 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 requestsAlthough 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
Benefits and ShortcomingsJSP Benefits:Platform and Server IndependenceOpen Development Process, Open SourceExtensible JSP TagsApplication persistence and variable scopesMature Java languageEasier MaintenanceScripting languages are fine for small applications, but do not scale well to manage large, complex applications. Because the Java language is structured, it is easier to build and maintain large, modular applications with it.JSP technology's emphasis on components over scripting makes it easier to revise content without affecting logic, or revise logic without changing content.The Enterprise JavaBeans architecture encapsulates the enterprise logic, such as database access, security, and transaction integrity, and isolates it from the application itself.Because JSP technology is an open, cross-platform architecture, Web servers, platforms, and other components can be easily upgraded or switched without affecting JSP-based applications. This makes JSP suitable for real-world Web applications, where constant change and growth is the norm.SecuritySince JSP can interoperate with standard Java it has access to the underlying Java SE Platforms extensible security architecture .Security features — cryptography, authentication and authorization, public key infrastructure, and more.JSP Shortcomings:Compiler Required
JSP Typically issues poor error reports

JavaServer Pages

  • 1.
    Java Server PagesJSP,JSTL, and Servlets
  • 2.
    TopicsWhat is JSPWhatis JSTLWhat is a Servlet?What are Beans?JSP Access ModelsBenefits and ShortcomingsSamplesQuestions / Comments
  • 3.
    What is JSP?JavaServerPages 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?TheJavaServer 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 aServlet?Java Servlet technology provides Web developers with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systemsA 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?JavaBeans 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 ModelsTheearly 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 performedModel 1Model 2The 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 requestsAlthough 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
  • 8.
    Benefits and ShortcomingsJSPBenefits:Platform and Server IndependenceOpen Development Process, Open SourceExtensible JSP TagsApplication persistence and variable scopesMature Java languageEasier MaintenanceScripting languages are fine for small applications, but do not scale well to manage large, complex applications. Because the Java language is structured, it is easier to build and maintain large, modular applications with it.JSP technology's emphasis on components over scripting makes it easier to revise content without affecting logic, or revise logic without changing content.The Enterprise JavaBeans architecture encapsulates the enterprise logic, such as database access, security, and transaction integrity, and isolates it from the application itself.Because JSP technology is an open, cross-platform architecture, Web servers, platforms, and other components can be easily upgraded or switched without affecting JSP-based applications. This makes JSP suitable for real-world Web applications, where constant change and growth is the norm.SecuritySince JSP can interoperate with standard Java it has access to the underlying Java SE Platforms extensible security architecture .Security features — cryptography, authentication and authorization, public key infrastructure, and more.JSP Shortcomings:Compiler Required
  • 9.
    JSP Typically issuespoor error reports