1 / 39

Web Tier Technologies

Web Tier Technologies. Agenda. Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces. Web Tier JEE patterns. Intercepting Filter Front Controller Context Object Application Controller View Helper Composite View Service to Worker

gary-rogers
Download Presentation

Web Tier Technologies

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. Web Tier Technologies

  2. Agenda • Introduction • Web Tier JEE Patterns • Web Container • Servlets • JSP & JSTL • Expression Language • Java Server Faces

  3. Web Tier JEE patterns • Intercepting Filter • Front Controller • Context Object • Application Controller • View Helper • Composite View • Service to Worker • Dispatcher View

  4. Web Tier Architectural Pattern • Request based MVC framework – Aware of HttpServletRequest Struts, Spring MVC • Component based MVC framework JSF, Wicket, Tapestry

  5. Model View Controller- separation of concerns

  6. Model View Controller • Features of MVC 1 Html or jsp files are used to code the presentation. To retrieve the data JavaBean can be used. In MVC1 architecture all the view, control elements are implemented using Servlets or Jsp. In MVC1 there is tight coupling between page and model as data access is usually done using Custom tag or through java bean call. • Features of MVC 2 The MVC2 architecture removes the page centric property of MVC1 architecture by separating Presentation, control logic and the application state. In MVC2 architecture there is only one controller which receives all the request for the application and is responsible for taking appropriate action in response to each request.

  7. View Type • Controller based View HTML tag mixed with Java code within Servlet • Template based View • JSP • Freemarker • Velocity • WebMacro

  8. Web application framework • Apache Struts • Spring MVC • Apache Tapestry • Apache Wicket • Java Server Faces • Grails • Google web toolki • JBoss Seam • Zk • AppFuse • FormEngine • Hamlet • OpenXava • Ztemplates • WaveMaker Toolkit

  9. Containers Java EE architecture has different containers: • A Web Container for hosting Java Servlets and JSP pages. • A EJB container for hosting Enterprise JavaBean components. • An applet container to run applets, • An application client container for running standard Java application clients. What is a Container ? • Containers are the interface between a component and the low-level platform specific functionality that supports the component. How does it work • Container settings are specified for each component during assembly process before deployment. What do I get • Support for security, transaction management, JNDI lookup, Remote connectivity.

  10. Web Container Example • Apache Tomcat • Resin • Jetty • Apache Geronimo Application Server • Jboss Application Server • Weblogic Application Server • Websphere Application Server • Tc server • NetWeaver

  11. Web Container • The Web container is the part of the application server in which Web application components run. • Web application components include servlets, JSP files, and HTML files. • The Web container processes the web components. Each application server runtime has one logical Web container, which can be modified, but not created or removed. Each Web container provides the following. • Communications support • Lifecycle management • Multi-threading support • Declarative security • JSP Support

  12. Web Container Contd.. • Every web app contains a deployment descriptor called web.xml • It describes the web application to the servlet container. • It is an XML file

  13. Sample Web.xml

  14. What is Servlet? A Servlet is a java technology-based Web component, managed by a container, that generates dynamic content • is a simple, consistent mechanism for extending the functionality of a web server • Are pre-compiled Java programs that are executed on the server side. • Require a Servlet container to run in

  15. Servlet Architecture Overview • Servlet Interface Methods to manage servlet • Generic Servlet Implements Servlet • HttpServlet Extends GenericServlet Exposes HTTP – specific functionality Servlet ServletConfig Serialization Interface GenericServlet Built-in Classes HttpServlet UserDefinedServlet User defined

  16. Servlet Life Cycle • Load • Initialize • Service • Destroy

  17. Servlet Context Every web application has exactly one instance of javax.servlet.Servlet-Context (assuming that the servlet container is not distributed across multiple JVMs). The context is initialized at the time that the web application is loaded Initialization parameters for a servlet context can be defined in Deployment descriptor The servlets of a web application can retrieve these initialization parameters using the methods of the ServletContext interface as shown below The servlet context initialization parameters are used to specify application-wide information, such as the developer’s contact information and the database connection information. public void init() { ServletContext context = getServletConfig().getServletContext(); //ServletContext context = getServletContext(); String dburl = context.getInitParameter("dburl"); //use the dburl to create database connections } Uses ServletConfig to get ServletContext

  18. Filters Filters are server-side components hosted by the web container that receive an inbound request before it is received by any other component Filters are deployed in the deployment descriptor file web.xml and then map to either servlet names or URL patterns in application's deployment descriptor. <filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

  19. Listeners Listeners are server-side components hosted by the web container that are notified about specific events that occur during a Servlet’s lifecycle. Listeners are deployed in the deployment descriptor file web.xml and then map to either servlet names or URL patterns in application's deployment descriptor. Process of defining Listener Interfaces • Write a class that implements the corresponding listener interface • Specify the class name in the deployment descriptor • Servlet container will call the appropriate methods on objects of this class when the events occur <listener> <listener-class> com.listeners.MyContextListener </listener-class> </listener>

  20. Why Use Servlets • Work well in a Heterogeneous Environments • OS and platform neutral • Work with all major web servers (IIS, Apache,etc..) • Clean separation of controller / logic from presentation • Efficient, scales very well • Well defined Web Architecture framework • Standard built in services such as: • Standard Approach to Authentication using declarative security vice programmatic security • Database connection pooling • Complete support for sessions via cookies and/or URL re-writing • Has automatic fallback to URL re-writing

  21. What is Java Server Page • Java Server Pages (JSP) • A simplified, fast way to create dynamic web content • HTML or XML pages with embedded Java Code or Java Beans • Can be a mix of template data in HTML/XML with some dynamic content • A JSP is a complied to a Java Servlet automatically by the Servlet container, it is then cached

  22. JSP Construct • Used in JSP pages, pages that end *.jsp • Comment <%-- Comment --%> • Declaration <%! int x = 0; %> • Expression <%= expression %> • Outputs to the Response stream • Like a “printf” to the browser • Do NOT use semi-colon to terminate the line • Scriplets - contains Java Code • <% code fragments %> • Implicit objects always available in the JSP Page • request – Http Request Object to get HTTP Headers, paramters passed from servlet • response – Http Response Object • session – Http Session Object • pageContext • application

  23. JSP Construct Contd. • JSP Directives • Are messages or instructions to the JSP container • Do not produce any output • “page” directive • <%@ page import=“com.lucek.*” %> • Commonly used for importing class paths • “include” directive • <%@ include file=“header.htm” %> • Good for including static content • “taglib” – lists the tag library descriptor location • Required when using tab libraries

  24. JSTL The ultimate goal of JSTL is to help simplify JavaServer Pages.JSTL offers the following capabilities • General-purpose actions • These actions complement the expression language by allowing a page author to easily display expressions in the expression language, set and remove the value of JSP scoped attributes, as well as catch exceptions • Control flow actions • Tag-based control flow structures (conditionals, iterators), which are more natural to page authors. • Tag library validators • Allow projects to only allow specific tag libraries, as well as enforce JSPcoding styles that are free of scripting elements. All the capabilities are fulfilled using the tab libraries

  25. JSP - Expression Language JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components. JSP EL allows you to create expressions both (a) arithmetic and (b) logical. The EL is a simple language designed to meet the needs of the presentation layer in web applications. It features: • A simple syntax restricted to the evaluation of expressions • Variables and nested properties • Relational, logical, arithmetic, conditional, and empty operators • Functions implemented as static methods on Java classes • Lenient semantics where appropriate default values and type conversions are provided to minimize exposing errors to end users <% Person p = (Person) request.getAttribute(“person”) %> ………. Person Name: <%= p.getName() %> ……… <% if (p.getAddress( ).equals(“defence”) ) { %> ……. <% } %> Person Name: $ { p.name } … <c:if test = “$ {p.address == param.add }” > ${ p.name } </c:if>

  26. Using Expressions • EL expressions can be used: • In static text, the value is computed and inserted in output. • In any standard or custom tag attribute that can accept an expression example : <some:tag value="${expr}"/> • Expressions could be used • Variables • Implicit Objects • Literals • Operators

  27. JSF - Overview • Holistic solution to several longstanding problems • Tedious & repetitive coding • Directly working with HTTP request/response • Non availability of IDE • Easier, more intuitive usage experience • Developed by a community of Web application experts • Combines • Ubiquity of JSP and servlets • Simplicity of JavaBeans • Power of Java EE • Common-sense of frameworks like Struts

  28. Benefits of JSF • Ease/Simplify Development • Easy creation of UI • Handles complexities of UI management • Clean separation between presentation and logic • Shorter development cycle • Standard Java framework • An extensible architecture • Support for multiple client devices • Flexible rendering model • International language support

  29. JSF and MVC • JSF and JSP • JSF UI comprised of JSP pages but not bound to that • Lots of GUI tools to develop UI • MVC model1 and model2 • Mixed java code with HTML: was easy to put business logic there • Model1 : all backend code moved to java beans and use of JSTL EL • Model2 : watered down version of MVC for web apps. Struts, Spring, Webwork etc. • Richer MVC environment • JSF component model • Closer to true MVC programming than other model2 frameworks • JSF gives host of event options but model2s rely on “request received” • JSF's MVC implementation • Faces Servlet is the controller • Backing beans are the model • JSP pages with custom JSF tags are the view

  30. Struts vs JSF Advantages of Struts • Mature and proven • Lots of documentation and reference materials • Large developer base • Broad tool and IDE support • Open source Advantages of JSF • Standard UI component model • Robust event handling mechanism • Render kit support for different clients • Highly 'pluggable' - components, view handler, etc • Governed by JCP When to go for JSF • Most new applications • Applications with sophisticated user interfaces • Applications requiring multiple client support • Smaller Struts applications that require significant upgrades • Larger Struts applications that require significant upgrades • Integrating selected JSF components into an existing Struts applications

  31. Relationship to existing APIs

  32. JSF Roles

  33. Configuring Web Application for JSF • FacesServlet Mapping <!-- Faces Servlet --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class> javax.faces.webapp.FacesServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <!-- Faces Servlet Mapping --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping>

  34. JSF Application Configuration Files • All JAR resources located in the /WEB-INF/lib • Context initialization parameter javax.faces.application.CONFIG_FILES in the web.xml <context-param> <param-name> javax.faces.application.CONFIG_FILES </param-name> <param-value> /WEB-INF/faces-config1.xml, /WEB-INF/faces-config2.xml, ... </param-value> </context-param> • faces-config.xml in the /WEB-INF/

  35. Request Processing Life Cycle <f:view> <h:form> <p>Enter your username: <h:inputText value="#{LoginBean.username}" id="usernameTextField" required="true"/> <h:message for="usernameTextField" /> </p> <p>Enter your password: <h:inputSecret value="#{LoginBean.password}" id="passwordTextField" required="true"/> <h:message for="passwordTextField" /> </p> <h:commandButton value="Submit Values" action="loginWelcome"/> </h:form> </f:view> <p>Enter your phone-number <h:inputText value="#{UserBean.phoneNumber}" id="phoneNumberTextField" required="true"> <f:validator validatorId="PhoneNumberValidator" /> </h:inputText> <h:message for="phoneNumberTextField"/> </p>

  36. JSF Managed/Backing Beans • Collect form input from components • Properties can be synchronized with component values • Can reference and manipulate UI component instances • Handle UI events • A combination of Struts ActionForms and Struts Actions • Conceptually similar to code-behind classes in ASP.NET WebForms • Usually talk to model objects to execute actual business logic • Bind component’s value to backing bean property • Usually configured using Managed Bean Creation facility • Facility can be used for model objects as well • Configured in JSF configuration file • Object will be created automatically if it doesn't exist

  37. JSF navigation Framework • Provides navigation rules that allow you to define navigation from view to view (mostly JSP pages) in a Web application. • Defined in JSF configuration files along with other definitions for a JSF application. • Usually, this file is named faces-config.xml. • However, you can assign any other name and even use more than one file to store JSF configuration data. • Full support for declarative navigation • Outcome of action methods used to select next page • Eliminates need for Java code or JSPs to know file names • To specify configuration files, use lines like the following in your web.xml file: • Code: <context-param>  <param-name>javax.faces.CONFIG_FILES</param-name>   <param-value>/WEB-INF/faces-config-navigation.xml, /WEB-INF/faces-beans.xml </param-value> </context-param>

  38. Sample Navigation <navigation-rule>     <from-view-id>/pages/inputname.jsp</from-view-id>     <navigation-case>       <from-outcome>sayHello</from-outcome>       <to-view-id>/pages/greeting.jsp</to-view-id>     </navigation-case>     <navigation-case>       <from-outcome>sayGoodbye</from-outcome>       <to-view-id>/pages/goodbye.jsp</to-view-id>     </navigation-case>     <navigation-case>   <to-view-id>/pages/goodbye.jsp</to-view-id> </navigation-case> </navigation-rule>

  39. Web Tier Specifications • JavaServer Pages 2.1 specification JSR 245  http://jcp.org/aboutJava/communityprocess/final/jsr245/ • Servlet s 2.5 pecification JSR 154  http://jcp.org/aboutJava/communityprocess/mrel/jsr154/index.html • JSF 1.2 specification JSR 252  http://jcp.org/aboutJava/communityprocess/final/jsr252/index.html • JSTL 1.2 specification JSR 52  http://jcp.org/aboutJava/communityprocess/mrel/jsr052/index2.html • Java EE 5 specification JSR 244  http://jcp.org/aboutJava/communityprocess/final/jsr244/index.html

More Related