Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Ch03 Servlets - Objectives • Introduce concepts of Java Servlet Web components • Support Environments for Java Servlets • Discuss the functionality of Java Servlets • Discuss the Java Servlet API • Discuss the Java Servlet Debugging • Provide step-by-step tutorials on building, deploying, and using Java Servlet components Java Servlets • Java Servlets technology provides an HTTP based request and response paradigm on Web servers. Java Servlets can handle generic service requests and respond to the client’s requests. • Applications include embedded system, wireless communication, and any other generic request/response application. • Java Web HTTP Servlets take HTTP requests, process them, and respond to the client with process results via HTTP protocol. Java Servlets (cont.) • A Servlet object is supported by a Servlet container, which is supported by an HTTP Web server. • The Servlet container is responsible of managing the life cycle of a Servlet object. Functions of the Servlet container include taking input requests from clients; instantiating an instance of the Servlet class; passing the requests to the Servlet object and letting the Servlet object process the requests; forwarding the results to clients. • The results returned to the client by a Servlet Web component may be in a format of TEXT/HTML, TEXT/XML, TEXT/PLAIN, IMAGE/JPEG, or in other binary types of formats. Java Servlets (cont.) • A Servlet component can delegate the requests to its back-end tier such as a database management system, RMI, EAI, or other Enterprise Information System (EIS). In this case the Servlet plays a role of the middle tier in a 3-tier architecture. • A Servlet is deployed as a middle tier just like other Web component such as JSP components. The Servlet Web components are always parts of the Web application. • For example, in J2EE, Servlet Web components and Java Server Pages (JSP) Web components can be deployed in a .WAR file; EJB components can be deployed in a .JAR file Java Servlets (cont.) • All together, an Enterprise application can be deployed in an .EAR file, which consists of .JAR files and .WAR files. • The Servlet components are building block components which always work together with other components such as JSP components, JavaBean components, Enterprise Java Bean (EJB) components, and Web service components. • A Servlet component is also a distributed component, which can provide services to remote clients and also access remote resources. Support Environments for Java Servlets • A Java Servlet application is supported by its Servlet container. The container may be an add-on Servlet container or standalone container, which comes as a part of a Web server. • Since a Java Servlet itself is a Java class. It needs Java API support, specifically the Java Servlet API that is available in an archive file called servletapi.jar in Tomcat. Support Environments for Java Servlets • The Apache Tomcat web server is the official reference implementation of Servlet containers, supporting Servlets and JSP. • Tomcat itself can be a standalone Web server and can also be an add-on Servlet/JSP engine/container for other web servers. • Tomcat 5.x is the newest edition at the time of this writing, which supports Servlet 2.4 and JSP 2.0. Support Environments for Java Servlets • The Tomcat Web server is an open source Servlet container originally developed by Sun Microsystems. • There are many other Web servers supporting Servlets and JSP, such as Sun’s Java Web server, and Macromedia s JRun, Caucho Resin, and Jetty. • Many application servers, like Sub Java System Application Server, BEA WebLogic and IBM WebSphere, Oracle Application Server, Pramati Server, JBoss also support Servlets and JSP. Web Server Configuration (server.xml) • An XML format file called server.xml is used to control and configure the behavior and setting of the Tomcat Web server. This file is located in the conf subdirectory of the Tomcat installation directory. • Some common changes needed in the configuration of Tomcat Web server may be: • 1. Reset the server port number where the Servlet class or other Web component will listen for requests. • <Connector port=”80” … /> • where 8080 is the initial port number. It is replaced by 80 to make it much more convenient for clients to access Servlets since 80 is the default HTTP port. Web Server Configuration (server.xml) 2. Turn on the Servlet reloading so that you don’t need to reload the recompiled Servlet. <DefaultContext reloadable=”true”/> is inserted in the Service tag. A sample of server.xml. <Server port="8005" shutdown="SHUTDOWN" debug="0"> <Service name="Catalina"> <Connector port="80" maxThreads="150" minSpareThreads="25“ maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" /> <Engine name="Catalina" defaultHost="localhost" debug="0“> <Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true"> . . . <DefaultContext reloadable="true" /> <Context path="" docBase="ROOT" debug="0" /> </Host> </Engine> </Service> </Server> Java Servlet Deployment Descriptor (web.xml) • An XML format file called web.xml in WEB-INF subdirectory of your Web application directory is used to control and configure the behavior and setting of Java Servlets in your specific Web application. There is another server-wide web.xml in the same place as server.xml. • The server-wide configurations will apply to all Web components on the server. As each application is deployed, this file is processed first, followed by the "/WEBINF/web.xml" deployment descriptor from your own applications. The application specific resource configurations should go in the "/WEB-INF/web.xml" in your application. • A sample Web server-wide web.xml for Tomcat 5. Java Servlet Deployment Descriptor (web.xml) (cont.) web.xml example “myPackage” <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Welcome to Tomcat</display-name> <description>Welcome to Tomcat</description> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>myPackage.myServlet</servlet-class> Java Servlet Deployment Descriptor (web.xml) (cont.) <init-param> <param-name>key1</param-name> <param-value>value1</param-value> </init-param> <init-param> <param-name>key2</param-name> <param-value>value2</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/GetMyServlet</url-pattern> </servlet-mapping> </web-app> A common change is to modify or add a servlet mapping. Basics of Java Servlets • The Java Servlet is a server-side Web component which takes a HTTP request from a client, handles it, talks to a database, talks to a JavaBean component, and responds a HTTP response or dispatch the request to other Servlets or JSP components. • Servlets can dynamically produce text-based HTML markup contents and binary contents as well based on the client’s request. Since a Servlet is a Web component it needs be deployed in a Servlet supporting web server with its deployment descriptor. Java Servlet architecture • A Java Servlet is just a typical Java class which extends an abstract class HttpServlet. • The HttpServlet class extends another abstract class GenericServlet . The GenericServlet class implements three interfaces: javax.servlet.Servlet, javax.servlet.ServletConfig, and java.io.Serializable. Java Servlet Class Hierarchy