Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.

Similar presentations


Presentation on theme: "Java Programming: Advanced Topics 1 Building Web Applications Chapter 13."— Presentation transcript:

1 Java Programming: Advanced Topics 1 Building Web Applications Chapter 13

2 Java Programming: Advanced Topics 2 Objectives Review the way the Web works and the non-Java technology that participates in Web applications Learn how J2EE packages Web applications Program dynamic Web content in servlets Become familiar with the Servlet API Provide continuity as the user navigates through your Web application

3 Java Programming: Advanced Topics 3 Objectives (Cont.) Learn how to generate dynamic Web content in JavaServer Pages Design Web applications based on servlets and JavaServer Pages Use the JavaServer Page tags and Servlet API Apply design patterns and frameworks to Web applications Discuss design issues related to Web applications

4 Java Programming: Advanced Topics 4 The Technology of the Web Web apps are composed of Web and Java technologies A Web app requires a Web server and an application server on the server side and a Web browser on the client side. Servlets and JavaServer Pages (JSPs) are the components that form the bridge between Web pages and Java application code

5 Java Programming: Advanced Topics 5 Web Relationships

6 Java Programming: Advanced Topics 6 The HTTP and HTTPS Protocols The browser communicates with a Web server HyperText Transfer Protocol (HTTP) or HyperText Transfer Protocol Secure (HTTPS) In a Web application, the content of HTML or XHTML pages can be dynamically created at runtime by Java components that run on the server side A Web container provides the context within which servlets and JSPs run

7 Java Programming: Advanced Topics 7 HTTP Request Methods

8 Java Programming: Advanced Topics 8 J2EE Web Application Packaging To be deployed on an application server, a J2EE Web app must be packaged in a Web archive (war file) that packaged in an enterprise application archive (ear file) The ear and war files contain deployment descriptors –Deployment descriptors: XML files that describe your Web app for the application server and request run- time services from the application server

9 Java Programming: Advanced Topics 9 Servlets A servlet is a server-side Java program that runs in response to an HTTP request The role of a servlet is to accept requests from the client, invoke the appropriate application logic to fulfill the request, and return the results to the client Each servlet is an entry point into a Web app or an enterprise application running a J2EE- compliant application server

10 Java Programming: Advanced Topics 10 How a Web App Processes HTTP Requests

11 Java Programming: Advanced Topics 11 Named and Anonymous Servlets Named servlet: when a servlet is assigned a name in the deployment descriptor Anonymous servlet: a servlet not listed in the Web deployment descriptor To protect your servlets from unauthorized use, make all servlets named servlets, disable anonymous servlets, and apply security settings to your Web app

12 Java Programming: Advanced Topics 12 The Servlet API The Servlet API is included in two packages: –javax.servlet –javax.servlet.http Servlet classes extend javax.servlet.http.HttpServlet

13 Java Programming: Advanced Topics 13 Common Types in the Servlet API

14 Java Programming: Advanced Topics 14 Common Types in the Servlet API (Cont.)

15 Java Programming: Advanced Topics 15 A Simple Servlet

16 Java Programming: Advanced Topics 16 The User Experience: Building a Web App with Continuity The HTTP protocol is stateless, so every request and response is a complete, independent transaction The Servlet API provides a simple mechanism to save information on the server side or on the client side to provide some continuity between browser sessions

17 Java Programming: Advanced Topics 17 Storing Data in HTTP Sessions A session is a place to store state information for a specific client, so that the information is available to different servlets You can access or create a session for a client by calling the HttpServletRequest.getSession method An HttpSession object contains a collection of name-value pairs

18 Java Programming: Advanced Topics 18 Places to Store State Data on the Server Side A servlet can store data in and retrieve it from four different scopes: –Data stored in the session scope is specific to one client. –Data stored in the application scope is global to the Web app. –The lifetime of data stored in the request scope is the duration of one HTTP request-response cycle. –Data stored in the page scope is accessible only in the current page

19 Java Programming: Advanced Topics 19 Providing Continuity with Cookies You can also use Cookie objects to store data on the client side from one browser session to the next Cookies: data objects stored on the client side of a Web app and passed back and forth between the Web browser and the server URL rewriting appends the cookie data to the URL

20 Java Programming: Advanced Topics 20 JavaServer Pages A JSP is Web resource with embedded Java code A JSP takes the form of an HML or XHMTL document that includes ordinary HTML tags and some additional JSP- specific tags The application server converts the entire page to HTML and resolves all dynamic content before sending the page to the client browser

21 Java Programming: Advanced Topics 21 Implementing MVC with Servlets and JSPs The MVC design pattern: –The view layer consists of static HTML pages and JSP documents –The controller layer consists of servlets that receive HTTP requests –The model layer consists of classes and other components that perform the core functionality of the application

22 Java Programming: Advanced Topics 22 Implementing MVC with Servlets and JSPs

23 Java Programming: Advanced Topics 23 JSP Tags and API JSPs that conform to HTML or XHTML syntax have four types of tags: –Java directives are used to import packages and set other parameters of the page as a whole –Declarations define methods and variables used by Java code in the JSP –Java expressions are evaluated, and its string representations of the value are inserted into the resulting HTML –Scriptlets contain one or more complete Java statements or blocks of code

24 Java Programming: Advanced Topics 24 HTML-Based JSP Tags

25 Java Programming: Advanced Topics 25 HTML-Based JSP Tags (Cont.)

26 Java Programming: Advanced Topics 26 HTML-Based JSP Tags (Cont.)

27 Java Programming: Advanced Topics 27 HTML-Based JSP Tags (Cont.)

28 Java Programming: Advanced Topics 28 A Simple JSP

29 Java Programming: Advanced Topics 29 An XML JSP

30 Java Programming: Advanced Topics 30 How the Server Processes JSPs The Web container converts a JSP into a servlet using the page compile process The Web container generates a service method from the body of the JSP source By default, a JSP is compiled when it is first called If you change the.jsp file between calls, the server reads the source page and compiles, loads, and runs it again

31 Java Programming: Advanced Topics 31 Java Coding in JSPs In a servlet, the doGet, doPost, and service methods receive the HTTP request and response objects as arguments and access the servlet context by calling methods on those arguments The JSP can call the getAttribute method on the variables application, session, or request to access objects stored there

32 Java Programming: Advanced Topics 32 Implicit Objects in JSPs

33 Java Programming: Advanced Topics 33 How to Retrieve Data

34 Java Programming: Advanced Topics 34 Custom Tags in JSPs Custom tags can help remove all Java code from JSPs Custom tags can have attributes Custom tags are grouped into libraries, and each library has a unique prefix For every tag library, there is a descriptor file identified by the uri attribute of the taglib directive

35 Java Programming: Advanced Topics 35 JSP Tags for JavaBeans

36 Java Programming: Advanced Topics 36 JSP Tags for JavaBeans (Cont.)

37 Java Programming: Advanced Topics 37 Frameworks for Building Web Applications Frameworks are productivity aids for creating resources that are common requirements in Web apps Frameworks can incorporate best practices for Web app design so that the Web apps they produce are extensible, easy to maintain, and lend themselves to being made secure

38 Java Programming: Advanced Topics 38 Building Robust Web Apps Servlets should be designed for multithreaded use Turn off caching for all pages and responses that include dynamic content Build precondition checks for servlets or JSPs Use JavaScript to disable the submit button after the first click or program the servlet to record the start and stop of processing as session state data

39 Java Programming: Advanced Topics 39 Summary In a Web application, the content of HTML or XHTML pages can be dynamically created at runtime by Java components that run on the server side A Web container provides the context within which servlets and JSPs run A J2EE Web app must be packaged in a Web archive (war file) A servlet is a server-side Java program that runs in response to an HTTP request The Servlet API is included in javax.servlet and javax.servlet.http packages

40 Java Programming: Advanced Topics 40 Summary (Cont.) HTTP requests and responses are stateless You can use Cookie objects to store data on the client or session objects on the server A servlet can store data in and retrieve it from global, session, request, and page scopes JSPs are HTML, XHTML, or XML documents that contain snippets of Java code JSP use directives, declarations, expressions, scriptlets, and custom tags For Web apps use the MVC design pattern


Download ppt "Java Programming: Advanced Topics 1 Building Web Applications Chapter 13."

Similar presentations


Ads by Google