Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.

Similar presentations


Presentation on theme: "Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are."— Presentation transcript:

1 Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Application Delivery Fundamentals 2.0: Java Module 25: Java EE Overview, Servlets, and JSP

2 Copyright © 2012 Accenture All Rights Reserved. Module Objectives (1 of 2) Describe Enterprise Applications. State both Java Server Page (JSP) Architectural Models. Describe a Servlet and its use. Describe the purpose of a Java Servlet container. Demonstrate an understanding of Servlet lifecycle methods. Develop a Java Presentation Tier using a Servlet. At the end of this module, you will be able to: 2

3 Copyright © 2012 Accenture All Rights Reserved. Module Objectives (2 of 2) Describe the Java Server Pages scripting elements used in the JSP definition. Describe the compilation of Java Server Pages to Java Servlets. Describe the Java Server Pages life cycle. Identify Java Server Pages components. Demonstrate the use of JSP in a web- based application. 3

4 Copyright © 2012 Accenture All Rights Reserved. Agenda Introduction to Enterprise Applications Purpose of Architectural Models Servlets Java Server Page (JSP) Summary 4

5 Copyright © 2012 Accenture All Rights Reserved. [Portrait image size: 10.03cm x 6.67cm, position: 17.5cm x 5.41cm] Introduction to Enterprise Applications (1 of 2) An Enterprise Application is a business application, as opposed to a domestic application used in the home. These business applications are: Complex Scalable Distributed Component-based Mission-critical They may be deployed on a variety of platforms across corporate networks, intranets, or the Internet. 5

6 Copyright © 2012 Accenture All Rights Reserved. [Portrait image size: 10.03cm x 6.67cm, position: 17.5cm x 5.41cm] Introduction to Enterprise Applications (2 of 2) Designing and developing such enterprise applications means satisfying hundreds or thousands of separate requirements and every development decision may affect many other requirements. They are data-centric, user-friendly, and must meet stringent requirements for security, administration, and maintenance. 6

7 Copyright © 2012 Accenture All Rights Reserved. Purpose of Architectural Models JSP Model 1 Architecture Web Browser DB JSP Model Request Response Application Server Enterprise Server 7

8 Copyright © 2012 Accenture All Rights Reserved. Purpose of Architectural Models JSP Model 2 Architecture Web Browser DB (Controller) Servlet (View) JSP (Model) Request Response Instantiate Select View Application ServerEnterprise Server 8

9 Copyright © 2012 Accenture All Rights Reserved. Servlets Introduction (1 of 2) A Servlet: Is used as a server-side Java module for processing web-based client requests. Runs in a Servlet container under the Application Server (Tomcat). Enables communication between the front-end, business, and integration layers. Belongs to only one application and can be concurrently accessed by multiple requests. 9

10 Copyright © 2012 Accenture All Rights Reserved. Servlets Introduction (2 of 2) Server Side Web Container Servlet Container loginServlet Service () profileServlet Service () Network Application Server http://server. 8080/loginSe rvlet?user=1 http://server. 8080/loginSe rvlet?user=3 http://server. 8080/loginSer vlet?user=n Browser 1 Browser 2 Browser 3 Browser n Client Side http://server.8 080/loginServl et?user=2 10

11 Copyright © 2012 Accenture All Rights Reserved. Servlets Java Servlet Container Servlet container implements the Java Servlet specification and resides in an application server. All Servlets must be deployed to a compliant Servlet container. Service Lifecycle Management Security Threading Exception Handling Servlet service() Servlet Container Web Container 11

12 Copyright © 2012 Accenture All Rights Reserved. Servlets Lifecycle Methods Instantiation Initialization Invokes init() Service Invokes service() Destroy Invokes destroy() Unavailable Request Response 12

13 Copyright © 2012 Accenture All Rights Reserved. Servlets Develop Java Presentation Tier using a Servlet Browser Thick Client JSP Servlets POJO EJB JDBC RDBMS Client UI Layer Business Layer Data Access Layer Data Layer 13

14 Copyright © 2012 Accenture All Rights Reserved. Java Server Page Introduction A Java Server Page: Enables development of a dynamic user interface Is an extension of a Servlet Is a combination of: –Snippets of Java code –Hyper Text Markup Language (HTML) –JavaScript –Cascading Style Sheets (CSS) 14

15 Copyright © 2012 Accenture All Rights Reserved. Basic JSP: See It Demonstration: Faculty will demonstrate how to create a basic Java Server Page. Time Allocated: 10 minutes Environment or File: BasicJSPDemo.jsp Steps: 1.Open the project Week2CodebaseM25_participant 2.Navigate to the WebContent folder. 3.Open the BasicJSPDemo.jsp file 4.Follow the TODOs to create the four basic components: a.declaration, expression, scriptlet, and comments 5.Save the file 6.Run the JSP a.Right click on the JSP file b.Choose Run as, run on server 15

16 Copyright © 2012 Accenture All Rights Reserved. Basic JSP: Try It Now You Try It: Create a basic Java Server Page to display the characters A to Z.. Time Allocated: 15 minutes Environment or File: BasicJSPTryit.jsp Steps: 1.Open the project Week2CodebaseM25_participant 2.Navigate to the WebContent folder. 3.Open the BasicJSPTryIt.jsp file 4.Follow the TODO to write code to print characters A to Z. 5.Save the file 6.Run the JSP a.Right click on the JSP file b.Choose Run as, run on server 16

17 Copyright © 2012 Accenture All Rights Reserved. Basic JSP: Solution Your faculty will now provide you with the Solution to check and update your file. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> Insert title here 17

18 Copyright © 2012 Accenture All Rights Reserved. Database Java Server Page Introduction - Architecture Model Servlet (Controller) Model View (JSP) JDBC 1. HTTP Request 3 4 Web Container JSP Container Web Server Web Browser 2. Request 7 5 6. Redirect 9. HTTP Response 8. Response 18

19 Copyright © 2012 Accenture All Rights Reserved. Java Server Page Structure and Scripting Elements: Overview 19

20 Copyright © 2012 Accenture All Rights Reserved. Java Server Page Structure and Scripting Elements: Types JSP element types are used to: Createand access objects Definemethods Managethe flow of control Givespecial instructions to the container Generateoutput 20

21 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: See It (1 of 2) Demonstration: Faculty will demonstrate how to write an advanced Java Server Page that will create a form to accept student data and an ‘Add Student’ submit button. Time Allocated: 20 minutes Environment or File: AdvanceJSPDemo.jsp Steps Part 1: 1.Open the project Week2CodebaseM25_participant 2.Navigate to the WebContent folder. 3.Open the AdvanceJSPDemo.jsp file 4.Follow the TODO to add action and method HTML attributes to the form element. The action should invoke the NextJSPDemo.jsp 21

22 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: See It (2 of 2) Demonstration: Faculty Demo Continued – Part Two Steps Part Two: 1.Open the NextJSPDemo.jsp file 2.Follow the TODOs to add the SQL code to connect to the Faculty database and retrieve the form data from AdvanceJSPDemo.jsp and insert into the Student table. 3.Print the number of rows added. 4.Run AdvanceJSPDemo.jsp on the server 22

23 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: Try It (1 of 2) Now You Try It: Create an advanced Java Server Page that will create a form to accept employee data and an ‘Add Employee’ submit button. Time Allocated: 30 minutes Environment or File: AdvanceJSPTryIt.jsp Steps Part One: 1.Open the project Week2CodebaseM25_participant 2.Navigate to the WebContent folder. 3.Open the AdvanceJSPTryIt.jsp file 4.Follow the TODO to set the form attributes: a.Action to NextJSPTryIt.jsp b.Method to Post c.onsubmit to return validateForm() javascript 23

24 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: Try It (2 of 2) Now You Try It: Continue the Try It Environment or File: Eclipse Steps Part Two: 1.Open the NextJSPTryIt.jsp file 2.Follow the TODOs to add the SQL code to connect to the Participant database and retrieve the form data from AdvanceJSPTryIt.jsp and insert into the Employee table. 3.Print the number of rows added. 4.Run AdvanceJSPTryIt.jsp on the server 24

25 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: Solution (1 of 6) Your faculty will now provide you with the Solution to check and update your files. The AdvanceJSPTryIt code should look something like this: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> ADF 2.0 Java Employee Registration System 25

26 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: Solution (2 of 6) The AdvanceJSPTryIt code should look something like this: Employee Registration Form Enter Employee Id Enter Employee Name Enter Department Id 26

27 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: Solution (3 of 6) The AdvanceJSPTryIt code should look something like this: 27

28 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: Solution (4 of 6) The NextJSPTryIt code should look something like this: <%@ page import="java.sql.*" language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> ADF 2.0 Java <% final String INSERTQUERY = "insert into Employee values (?,?,?)"; String url = "jdbc:mysql://localhost/participant"; String user = "root"; String pass = "abcd1234"; 28

29 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: Solution (5 of 6) The NextJSPTryIt code should look something like this: Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(url,user,pass); PreparedStatement ps = con.prepareStatement(INSERTQUERY); String id = (String)request.getParameter("employeeId"); String name =(String)request.getParameter("employeeName"); String departmentId = (String)request.getParameter("departmentId"); ps.setString(1,id); ps.setString(2,name); ps.setString(3,departmentId); con.setAutoCommit(false); int rows = ps.executeUpdate(); con.commit(); 29

30 Copyright © 2012 Accenture All Rights Reserved. Advanced JSP: Solution (6 of 6) The NextJSPTryIt code should look something like this: if(rows == 1) { %> 30

31 Copyright © 2012 Accenture All Rights Reserved. Java Server Page: Structure and Scripting Elements: JSP Implicit Objects Objects are created automatically by the Web container: –Need not be declared –Are accessible in JSP Objects contain important information, such as: –Request information –Application information –Page information 31

32 Copyright © 2012 Accenture All Rights Reserved. Java Server Page Compiled to Java Servlet JSP is compiled into a Java Servlet. Typically, each JSP is first translated into the Java source code for a Servlet class. The created Servlet is then compiled. The resulting Servlet is invoked to handle all requests for that page. The page translation step is performed the first time a given JSP is requested. 32

33 Copyright © 2012 Accenture All Rights Reserved. Java Server Page Life Cycle JSP file jspinit() Servlet file Servlet class _jspService() jspDestroy() 33

34 Copyright © 2012 Accenture All Rights Reserved. Java Server Page JSP Components: Introduction The Java Server Pages Standard Tag Library (JSTL) and Custom tags extend the JSP specification by adding a tag library for common tasks. Some of the common tasks are: –XML data processing –Conditional execution –Loops –Internationalization 34

35 Copyright © 2012 Accenture All Rights Reserved. Java Server Page JSP Components: JSP Standard Tag Library (JSTL) JSTL: –Encapsulates useful dynamic functionality. –Provides more readability and maintainability. JSTL has five tag libraries: –core –XML –sql –fmt –function 35

36 Copyright © 2012 Accenture All Rights Reserved. 1String firstName = request.getParameter("fName"); Java Server Page JSTL Example 1 2//Adds a tag library. Done only once in a JSP page to access all //its tags 3 4//Core tag library with an alias c. Creates a variable named //firstName and accesses the HTML page’s data using //${param. } Existing method using JSP Revised method using JSTL The following examples obtain a parameter value from an HTML page. 36

37 Copyright © 2012 Accenture All Rights Reserved. Java Server Page JSP Components: Custom Tags Custom tags are: Extensions to the JSP language. Powerful mechanisms that allow the developer to: –Create components that are unavailable in: Standard actions JSTL actions –Encapsulate functionality –Reuse those components in more than one application 37

38 Copyright © 2012 Accenture All Rights Reserved. Servlet: See It Demonstration: Faculty will demonstrate how to create a servlet to print ‘Hello World’ within a browser. Time Allocated: 10 minutes Environment or File: ServletDemo.java Steps: 1.Open the project Week2CodebaseM25_participant 2.Navigate to the Java Resources/src folder. 3.Open the package com.accenture.adf.newcodington.module25.sample 4.Open the ServletDemo.java file 5.Follow the TODOs to override the doGet method and print ‘Hello World’ 6.Run the Index.html file in the WebContent folder on the server 38

39 Copyright © 2012 Accenture All Rights Reserved. Servlet: Try It Now You Try It: Create a servlet to print the numbers 1 to 10 within a browser. Time Allocated: 20 minutes Environment or File: ServletTryIt.java Steps: 1.Open the project Week2CodebaseM25_participant 2.Navigate to the Java Resources/src folder. 3.Open the package com.accenture.adf.newcodington.module25.sample 4.Open the ServletTryIt.java file 5.Follow the TODOs to override the doGet method and print numbers from 1-10 6.Run the Index.html file in the WebContent folder on the server 39

40 Copyright © 2012 Accenture All Rights Reserved. Servlet: Solution (1 of 3) Your faculty will now provide you with the Solution to check and update your file. package com.accenture.adf.newcodington.module25.sample; import java.io.IOException; public class ServletTryIt extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletTryIt() { super(); } 40

41 Copyright © 2012 Accenture All Rights Reserved. Servlet: Solution (2 of 3) The code should look something like this: // Overriding doGet method to print numbers from 1 to 10 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Obtaining PrintWriter object from HttpServletResponse object PrintWriter pw = response.getWriter(); //Printing numbers. You may optionally print those in a table. pw.print(" "); pw.print(" Numbers "); for (int i = 1; i <= 10; i++) { pw.print(" "); pw.print(i); pw.print(" "); } pw.println(" Home "); } 41

42 Copyright © 2012 Accenture All Rights Reserved. Servlet: Solution (3 of 3) The code should look something like this: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Calling doGet method from this method doGet(request, response); } 42

43 Copyright © 2012 Accenture All Rights Reserved. Activity 1: JSP and Servlets Objective: Create a web application to insert records into a Zoo table using JSP and Servlets. Instructions: Navigate to the Module 25, Activity 1 page on the course web site. Follow the instructions provided on the web page to complete the activity. 43

44 Copyright © 2012 Accenture All Rights Reserved. Questions and Comments 44

45 Copyright © 2012 Accenture All Rights Reserved. Module Summary (1 of 3) A Servlet enables communication between the front-end, business, and integration layers and must be deployed to a compliant Servlet container. That MVC2 supports reusability of application components. The Presentation Tier encapsulates the presentation logic and interfaces with a Client Tier. The key points from this module are: 45

46 Copyright © 2012 Accenture All Rights Reserved. Module Summary (2 of 3) A Java Server Page is a combination of: –Snippets of Java code –Hyper Text MarkUp Language (HTML) –Javascript –Cascading Style Sheets (CSS) A Web Server transfers the client’s request to be processed by Application Server. An Application Server processes the client’s request through the Model-View- Controller (MVC) pattern. 46

47 Copyright © 2012 Accenture All Rights Reserved. Module Summary (3 of 3) The JSP structure provides mechanisms to create and access implicit objects within scripting elements. Application objects can be accessed within a JSP page using Java and Enterprise Beans components. JSTL has five tag libraries: –core –XML –sql –fmt –function 47


Download ppt "Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are."

Similar presentations


Ads by Google