Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java™2 Platform, Enterprise Edition (J2EE™). What is J2EE?  Editions of Java.  Provides a component-based approach to the design, development, assembly,

Similar presentations


Presentation on theme: "Java™2 Platform, Enterprise Edition (J2EE™). What is J2EE?  Editions of Java.  Provides a component-based approach to the design, development, assembly,"— Presentation transcript:

1 Java™2 Platform, Enterprise Edition (J2EE™)

2 What is J2EE?  Editions of Java.  Provides a component-based approach to the design, development, assembly, and deployment of enterprise applications.  It offers a multitiered distributed application model, the ability to reuse components, integrated Extensible Markup Language (XML)-based data interchange, a unified security model, and flexible transaction control.

3 J2EE Components  A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and that communicates with other components.  The J2EE specification defines the following J2EE components: Application clients and applets are components that run on the client. Java Servlet and JavaServer Pages™ (JSP™) technology components are Web components that run on the server. Enterprise JavaBeans™ (EJB™) components (enterprise beans) are business components that run on the server.

4 J2EE Components  Written in the Java programming language and are compiled in the same way.  The difference is, J2EE components and “standard” Java classes is that J2EE components are assembled into a J2EE application, verified to be well formed and in compliance with the J2EE specification, and deployed to production, where they are run and managed by the J2EE server.

5 J2EE Tier  Client Tier  Web Tier  Business Tier  Enterprise Information Systems Tier

6 J2EE Clients  A J2EE client can be a Web client or an application client.  Web client (Thin client) consists web- components generated dynamic mark-up languages pages and Web Browser; includes applet  Application client runs on a client machine

7 Web Components  Servlets and JSP pages.  Servlets are Java programming language classes that dynamically process requests and construct responses.  JSP pages are text-based documents that execute as servlets but allow a more natural approach to creating static content.

8 Business Components  Enterprise Beans which contain logic that solves or meets the needs of a particular business domain.

9 Enterprise Information System Tier  Includes enterprise infrastructure systems such as enterprise resource planning (ERP), mainframe transaction processing, database systems, and other legacy information systems.

10 J2EE Containers  Containers are the interface between a component and the low-level platform specific functionality that supports the component.  Services such as security, transaction management, Java Naming and Directory Interface™ (JNDI) lookups, and remote connectivity.

11 MVC architecture  Model  View  Controller

12 HTTP  Communication protocol over TCP/IP  Request-response, stateless protocol  Only client can initiate communication  HTTPS  An HTTP request consists of three components: Method——URI—Protocol/Version Request headers Entity body  Eg.: http://localhost/servlet/default.jsp

13 HTTP Request Example GET /servlet/default.jsp HTTP/1.1 Accept: text/plain; text/html Accept-Language: en-gb Connection: Keep-Alive Host: localhost Referer: http://localhost/ch8/SendDetails.htm User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98) Content-Length: 33 Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate LastName=Franks&FirstName=Michael

14 HTTP 1.1 request Methods  GET: GET is the simplest, and probably, most used HTTP method. GET simply retrieves the data identified by the URL. If the URL refers to a script (CGI, servlet, and so on), it returns the data produced by the script.  HEAD: The HEAD method provides the same functionality as GET, but HEAD only returns HTTP headers without the document body.  POST: POST is used to transfer a block of data to the server in the entity body of the request.  OPTIONS: The OPTIONS method is used to query a server about the capabilities it provides.  PUT: The PUT method is a complement of a GET request, and PUT stores the entity body at the location specified by the URI.  DELETE: The DELETE method is used to delete a document from the server. The document to be deleted is indicated in the URI section of the request.  TRACE: The TRACE method is used to tract the path of a request through firewall and multiple proxy servers. TRACE is useful for debugging complex network problems and is similar to the traceroute tool.

15 HTTP Responses  Consists of three parts: Protocol—Status code—Description Response headers Entity body

16 HTTP Response Example HTTP/1.1 200 OK Server: Microsoft-IIS/4.0 Date: Mon, 3 Jan 1998 13:13:33 GMT Content-Type: text/html Last-Modified: Mon, 11 Jan 1998 13:23:42 GMT Content-Length: 112 HTTP Response Example Welcome to Brainy Software

17 Web-Container  Apache Tomcat  Setting Classpath  Setting Java Home  Installing & Running Tomcat  Tomcat Features

18 Servlets  Java programs which run on Java-enabled web-server.  Executed and managed by servlet container or servlet engine.  Servlet Application Architecture

19 Servlet Request-Response Process  Client Issues an HTTP request  Interception by Web Server  Servlet handles the request  Optionally, communicates with other tiers  Writes response to output stream  Server sends response to the client

20 The Life Cycle of a Servlet  init( ), service( ), and destroy( ) methods  HTTP request for generated for a URL.  Web server maps this request to a particular servlet which is dynamically retrieved and loaded.  The server invokes the init( ) method of the servlet.  The server invokes the service( ) method of the servlet; may also formulate an HTTP response for the client.  The server may decide to unload the servlet from its memory using destroy() method

21 Servlet API  javax.servlet Package Interfaces Description  Servlet- Declares life cycle methods for a servlet.  ServletConfig- Allows servlets to get initialization parameters.  ServletContext- Enables servlets to log events and access information  about their environment.  ServletRequest- Used to read data from a client request.  ServletResponse- Used to write data to a client response.  SingleThreadModel- Indicates that the servlet is thread safe.

22 Servlet API Contd… Classes Description  GenericServlet- Implements the Servlet and ServletConfig interfaces.  ServletInputStream- Provides an input stream for reading requests from a client.  ServletOutputStream- Provides an output stream for writing responses to a client.  ServletException- Indicates a servlet error occurred.  UnavailableException- Indicates a servlet is unavailable.

23 Servlet API Contd…  javax.servlet.http Package Interface Description  HttpServletRequest Enables servlets to read data from an HTTP request.  HttpServletResponse Enables servlets to write data to an HTTP response.  HttpSession Allows session data to be read and written.

24 Servlet API Contd… Classes Description  Cookie- Allows state information to be stored on a client machine.  HttpServlet- Provides methods to handle HTTP requests and  responses.  HttpSessionEvent- Encapsulates a session- changed event.

25 Java Server Pages (JSP)  To generate Dynamic HTML on the server side  A JSP document must end with the extension.jsp.  The first time the file is requested, it is translated into a servlet and then compiled into an object that is loaded into resident memory.  The generated servlet then services the request, and the output is sent back to therequesting client.  On all subsequent requests, the server will check to see whether the original JSP source file has changed. If it has not changed, the server invokes the previously compiled servlet object.  If the source has changed, the JSP engine will reparse the JSP source.

26 The Components of a JavaServer Page  Directives, scripting, implicit objects, and standard actions.  Directives General syntax: The page Directive:  Globally affect the JSP  Syntax: The include Directive:  Insert text and/or code at JSP translation time.  Syntax: The taglib Directive:  Include custom tag library  Syntax:

27 The Components of a JavaServer Page  JSP Scripting Declarations: Syntax: Expressions  Text upon evaluation by the container, is replaced with the resulting value of the container evaluation  Syntax: Scriptlets

28  Implicit Objects Out: represents a JspWriter (derived from a java.io.Writer) that provides a stream back to the requesting client Request: The implicit request object represents the javax.servlet.http.HttpServletRequest Response: The implicit response object represents the javax.servlet.http.HttpServletResponse object. PageContext: The pageContext object provides access to the namespaces associated with a JSP page. Session: The implicit session object represents the javax.servlet.http.HttpSession object.

29 Application: The application object represents the javax.servlet.ServletContext. Config: The implicit config object holds a reference to the ServletConfig Page: The page object contains a reference to the current instance of the JSP being accessed. Exception: The implicit exception object provides access to an uncaught exception thrown by a JSP. It is available only in JSPs that have a page with the attribute isErrorPage set to true.

30  Standard Actions: JSP standard actions are predefined custom tags that can be used to encapsulate common actions easily.  There are two types of JSP standard actions: the first type is related to JavaBean functionality, and the second type consists of all other standard actions.

31  The action provides parameters and values to the JSP standard actions,, and.  Syntax: action provides a method for including additional static and dynamic Web components in a JSP. action enables the JSP engine to execute a runtime dispatch of the current request to another resource existing in the current Web application, including static resources, servlets, or JSPs. action enables a JSP author to generate the required HTML, using the appropriate client−browser independent constructs, to result in the download and subsequent execution of the specified applet or JavaBeans component.

32 JavaServer Pages and JavaBeans  JavaBeans are reusable Java classes whose methods and variables follow specific naming conventions to give them added abilities.  Makes information available to the page through simple accessor methods.  A server can automatically set a bean’s properties (instance variables) using the parameter values in the client’s request.  A bean can also have its scope managed automatically by the server. A bean can be assigned to a specific request (where it is used once and destroyed or recycled) or to a client session

33 Standard Actions: JavaBean  Three predefined standard actions relate to using JavaBeans in a JSP:,, and.  - The JavaBean standard action creates or looks up an instance of a JavaBean with a given ID and scope. When a action is encountered, the action tries to find an existing object using the same ID and scope. If it cannot find an existing instance, it will attempt to create the object and store it in the named scope associated with the given ID.

34  The syntax of the action is as follows: <jsp:useBean id="name" scope="page|request|session|application" typeSpec> body typeSpec ::=class="className" | class="className" type="typeName" | type="typeName" class="className" | beanName="beanName" type="typeName" | type="typeName" beanName="beanName" | type="typeName"

35  : standard action sets the value of a bean’s property. Its name attribute represents an object that must already be defined and in scope.  The syntax for the action is as follows: property="*" | property="propertyName" | property="propertyName" value="propertyValue"

36  :standard action that relates to integrating JavaBeans into JSPs is.  It takes the value of the referenced bean’s instance property, converts it to a java.lang.String, and places it on the output stream.  The referenced bean instance must be defined and in scope before this action can be used.  The syntax for the action is as follows: <jsp:getProperty name="beanName" property="propertyName" />

37  Example: Counter.java public class Counter { int count = 0; public Counter() {} public int getCount() {return ++count;} public void setCount(int count) {this.count =count;} }___________________________________________ Counter.jsp Bean <jsp:useBean id="counter" scope="session" class="Counter" /> <% out.println("Count from scriptlet code : " + counter.getCount() + " "); %> Count from jsp:getProperty :

38 Writing servlets Examples 1) import java.io.*; import javax.servlet.*; public class HelloServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println(" Hello!"); pw.close(); }

39 2) <form name="Form1" method="post" action="http://localhost:8080/examples/PostParametersServlet"> Employee Phone

40 Writing servlets Contd.. import java.io.*; import java.util.*; import javax.servlet.*; public class PostParametersServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get print writer. PrintWriter pw = response.getWriter(); // Get enumeration of parameter names. Enumeration e = request.getParameterNames(); // Display parameter names and values. while(e.hasMoreElements()) { String pname = (String)e.nextElement(); pw.print(pname + " = "); String pvalue = request.getParameter(pname); pw.println(pvalue); } pw.close(); } }

41 Forwarding requests  RequestDispatcher object  Obtained by calling the getRequestDispatcher method of ServletContext, supplying a URL relative to the server root  Use Forward method to completely transfer control to the associated URL and use include to output the associated URL’s content  Example: public void doGet(HttpServletRequest request, HttpServletResponse response){ String url = "/presentations/presentation1.jsp"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); dispatcher.forward(request, response); }

42 Contd…  sendRedirect() method of HttpServletResponse Redirects the response to the specified location, automatically setting the status code and Location header. Syntax: public abstract void sendRedirect(String location) throws IOException The default implementaion also writes a short response body that contains a hyperlink to the location, to support browers without redirect capabilities. Flow: The servlet calls the sendRedirect() method of the response object and sends back the response to the browser along with the status code. Then the browser sees the status code and look for that servlet which can now handle the request. Again the browser makes a new request, but with the name of that servlet which can now handle the request and the result will be displayed to you by the browser. In all this process the client is unaware of the processing.

43 Contd…  Example: public void doGet(HttpServletRequest request, HttpServletResponse response){ String url = "/presentations/presentation1.jsp"; response.sendRedirect(url); }

44 Initialization Parameters in Servlet  Web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> ShowMsg ShowMessage message Hello World repeats 5

45 public class ShowMessage extends HttpServlet { private String message; private int repeats = 1; public void init(ServletConfig config) throws ServletException { super.init(config); message = config.getInitParameter("message"); try { String repeatString = config.getInitParameter("repeats"); repeats = Integer.parseInt(repeatString); } catch(NumberFormatException nfe) {..} } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(“ ”); for(int i=0; i ");} out.println(" "); }}

46 Thank-you


Download ppt "Java™2 Platform, Enterprise Edition (J2EE™). What is J2EE?  Editions of Java.  Provides a component-based approach to the design, development, assembly,"

Similar presentations


Ads by Google