Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)

Similar presentations


Presentation on theme: "Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)"— Presentation transcript:

1 Advanced Programming Estifanos T. (MSc in Computer Networking)

2 CHAPTER SEVEN Java Servlets Estifanos T. (MSc in Computer Networking)

3 Objectives 3 Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking) Servlet and JSP Overview Handling HTTP get Request Servlet Life Cycle Servlet and JSP Implementations JSP Components

4 4 Servlets, JSP, and J2EE Outlook Socketssocket reads/writes from/to data streams similar to read/write to filejava.net RMIremote method calls similar to local function calls java.rmi servletsrequest-response model javax.servlet extends server functionality javax.servlet.http JSPrequest-response on top of servlets (in fact translated into servlets) allows writing Java code directly javax.servlet.jsp into Web page - sripletsjavax.servlet.jsp.tagext Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

5 5 Servlets and JSPs Application Servlets are typically used for data base intensive applications with a thin client, i.e. an applications with minimal client support:  The server is responsible for data base access;  Presentation logic is written once and resides on the server;  The client accesses server through standard protocols available on most client platforms Servlets and JSPs can be used interchangeably. JSPs are an extension of servlets that allows using the request/response mechanism without getting into lower level servlet details. However, JSP are primarily used when most content sent to client is static or markup, and only a small portion is generated dynamically with Java code. As opposed to this Servlets are typically used for performing tasks on behalf of the client; they would then invoke other servlets or JSPs to provide response Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

6 6 Servlets and JSPs Implementation Sun initiated the development of servlets and JSP through the Java Community Process (www.jcp.org) that allows individuals, organizations, and corporations to participate in the development of new technologies and APIs for the Java platformwww.jcp.org Reference implementation of servlets and JSP standards is under development by the Apache Software Foundation (www.apache.org) and is part of the Jakarta Project (jakarta.apache.org)www.apache.orgjakarta.apache.org Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

7 7 Servlet and JSP part of the Jakarta project is called Tomcat and is the official reference implementation of these two standards. The Jakarta project contains other subprojects Servlets and JSPs are supported directly or with third party plug-ins by most Web and application servers, e.g. Netscape iPlanet Application Server, MS' Internet Information Server (IIS), Apache HTTP Server, BEA WebLogic application server, IBM's WebSphere application server, WWW Consortium's Jigsaw Web server Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking) Servlets and JSPs Implementation

8 8 Servlet Overview A servlet must implement the Servlet interface Servlet interface defines methods void init( ServletConfig ) ServletConfig getServletConfig () String getServletInfo() void service( ServletRequest, ServletReply) void destroy() The servlet package has two abstract classes that implement the Servlet interface GenerigServlet from javax.servlet HttpServlet from javax.servlet.http These classes provide default implementations of all service methods and override some of these methods to provide the appropriate specification Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

9 9 Servlet Communication: request - reply A typical communication consists of The client sending a request to server or servlet container, The server/container receiving the request, and directing it to The appropriate servlet which does the processing, and The container sending back the result For instance a client (e.g. browser) sends an http request to server/container, the servlet does the required processing, e.g. by interacting with DB or assembling the document, interacting with other server side components (other servlets, JSPs, EJBs), then sends the document (e.g. in http, xhtml, xml, image format) to client to display in browser Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

10 10 Servlet Lifecycle Life cycle of servlet starts by Server (also called servlet container or servlet engine) loads servlet into memory after receiving first request; init() method is automatically invoked to initialize servlet thus providing access to the environment (container, server) on which servlet executes; Servlet responds to request; All requests are handled by service() method which is called once per request during the life cycle of the servlet. The service method can be overridden in derived classes Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

11 11 Example: http get request HttpServlet class differentiates between get and post request types from Web browsers and provides doGet() and doPost() methods to handle them The client invokes the servlet through a form in an XHTML document. The form action specifies the path to the servlet, and the method the request type, here a "get" URL path that invokes servlet at server side request type Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

12 12 Client can access servlet only if it is installed on server that can respond to servlet requests Sometimes servlet support is built directly into the Web server and no special configuration is required to handle servlet requests Alternatively, it may be necessary to integrate a servlet container with a Web server: e.g. this can be done with Tomcat (a fully functional implementation of JSP and servlet standards) for Apache Web server of the Apache Software Foundation or Microsoft's Internet Information Server (IIS) Tomcat is integrated in the Java 2 Enterprise Edition reference implementation from Sun Notes on Servlets Support Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

13 13 // A simple servlet to process get requests. package com.deitel.advjhtp1.servlets; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class WelcomeServlet extends HttpServlet { // process "get" requests from clients protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { //specify content (MIME)type for client browser response.setContentType( "text/html" ); //instantiate PrintWriter object to send to client PrintWriter out = response.getWriter(); //Note: for binary data use getOutputStream() //method to instantiate an ServletOutputStream //object Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking) Example: server side

14 14 Example: server side // send XHTML page to client // start XHTML document out.println( " " ); out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); out.println( "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); // head section of document out.println( " " ); out.println(" A Simple Servlet Example "); out.println( " " ); // body section of document out.println( " " ); out.println( " Welcome to Servlets! " ); out.println( " " ); Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

15 15 Example: server side // end XHTML document out.println( " " ); // commit info to client: close o-stream, //flush o-buffers, //send info to client out.close(); } Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

16 16 JSP Overview JSP is an extension of servlets to simplify delivery of dynamic Web content. As with servlets they execute as part of a Web server JSPs looks very much like an XHTML document, and normally include markup. JSP requests are specified in the page with corresponding syntax When the server receives the first request for JSP it translates it into a servlet that handles current and future requests (the corresponding Java statements are placed into method _ jspService); the servlet is then compiled and executed The lifecycle of the JSP is the same as the lifecycle of the servlet Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

17 17 JSP Components directives – messages to JSP container to specify page settings, include content from other resources, specify custom tag libraries actions – encapsulate functionality of predefined tags, that can be embedded in the JSP; often performed on info sent in client request scriplets or scripting elements – allow insertion of Java code. Generally defined as blocks of code enclosed between with expressions that are evaluated at client request defined in, e.g. to display the current Date declarations enclosed in, e.g comments enclosed in, e.g. tag libraries – part of tag extension mechanism that allows to create custom tags Lecture 7: Java Servlets 9/10/2019 Estifanos T. (MSc in Computer Networking)

18 End Of Chapter Seven


Download ppt "Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)"

Similar presentations


Ads by Google