TomcatThe Apache Jakarta ProjectGetting TomcatWeb appsDirectoriesPackagesTomcat directory structureMy filesmyWebForm.htmlweb.xmlServlet without JSPServlet with JSPJSP (result.jsp)FlowAlternatives to TomcatThe EndJan 14, 2019Tomcat2The Apache Jakarta ProjectThe Apache Jakarta Project “creates and maintains open source solutions on the Java platform for distribution to the public at no charge”Apache Jakarta Tomcat--or just “Tomcat”--is one of those projectsTomcat is a container for servletsTomcat can act as a simple standalone server for Web applications that use HTML, servlets, and JSPApache is an industrial-strength, highly optimized server that can be extended with Tomcat3Getting TomcatThe Apache Jakarta website is hard to navigateIf you want to get Tomcat, one reasonable download site is http://mirrors.xtria.com/apache/jakarta/tomcat-5/v5.0.29/bin/You would need the whole “tarball”, which will have a name such as jakarta-tomcat-5.0.29.tar.gzAn excellent tutorial site is Configuring & Using Apache Tomcat, http://www.coreservlets.com/Apache-Tomcat-Tutorial/This site also contains many examples you can use to test your installationInstalling Tomcat by itself is much easier than installing Apache and then adding Tomcat to it4Web appsA web application is basically a web site that:“Knows who you are”--it doesn’t just give you static pages, it interacts with youCan permanently change data (such as in a database)A web application can consist of multiple piecesStatic web pages (possibly containing forms)ServletsJSPTomcat organizes all these parts into a single directory structure for each web application...but you have to help with the organization5DirectoriesTo create servlets, you really should have two directory structures:A development directory, in which you can write and partially debug your codeA deployment directory, in which you put “live” codeTomcat requires a particular set of directories for your web applicationIt is extremely picky about having everything in the right place!Since your web application must typically co-exist with other web applications, you should use packages to avoid name conflictsThis further complicates the Tomcat directory structure6PackagesA package statement in Java must be the very first line of code in the fileExample:package com.example.model;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;public class MyServlet extends HttpServlet { ... }This implies thatThis program is in a file named MyServlet.java, which isin a directory named model, which isin a directory named example, which isin a directory named com7Tomcat directory structure myApplicationDirectory/ -- this is your top level directory myWebForm.html myJspPage.jsp WEB-INF/ -- must have this directory, named exactly like this lib/ -- mostly for external .jar files classes/ -- must have this directory, named exactly like this com/ -- The com.example.model package directory example/ model/ myModel.class -- in package com.example.model; web/ myServlet.class --in package com.example.web; web.xml -- this is the deployment descriptor, it must have this name8My filesmyWebForm.htmlThis is the web page with a form that starts up the servletcom/example/web/myServlet.classThis is the servlet I intend to use; it will use the myModel class, but to do this it needs an import statement:import com.example.model.myModel;com/example/model/myModel.classThis does the “business logic” it is good form to keep it separatemyJspPage.jspThe (optional) JSP page to create the HTML output (could be done directly by myServlet)web.xmlA file required by Tomcat to tell it what class to start with and how to refer to that class9myWebForm.html<html> ... <body> ... <form method="POST" action="NameSeenByUser.do"> ...various form elements... </form> ... </body></html>10web.xml<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>Some internal name</servlet-name> <servlet-class>com.example.web.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Some internal name</servlet-name> <url-pattern>/NameSeenByUser.do</url-pattern> </servlet-mapping> </web-app>11Servlet without JSPpublic class MyServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String value = request.getParameter("name"); out.println("<html><body>I got: " + name + " = " + value + "</body></html>"); }}12Servlet with JSPpublic class MyServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String value = request.getParameter("name"); ...computation resulting in some Object obj... request.setAttribute(”objName", obj); RequestDispatcher view = request.getRequestDispatcher("result.jsp"); view.forward(request, response); }}13JSP (result.jsp)<%@ page import="java.util.*" %><html><head><title>Your results</title></head><body><% MyObject object = (MyObject)request.getAttribute("objName"); String someResult = ...computations using object... out.print("<br>And the answer is: " + someResult);%></body></html>14FlowThe user submits an HTML formTomcat finds the servlet based on the URL and the deployment descriptor (web.xml) and passes the request to the servletThe servlet computes a responseEither:The servlet writes an HTML page containing the responseOr:The servlet forwards the response to the JSPThe JSP embeds the response in an HTML pageTomcat returns the HTML page to the user15Alternatives to TomcatSun’s Java Web ServerOld, no longer being developed, all in JavaJava Web Server Development Kit (JWSDK)Official reference implementationDifficult to install and configureJBossOpen sourceOpinions vary on how easy it is to installComes with built-in database16The
View Full Document