28
The Tomcat Servlet Container

The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

  • View
    229

  • Download
    2

Embed Size (px)

Citation preview

Page 1: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

The Tomcat Servlet Container

Page 2: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

About Tomcat

• A “servlet container” is like a mini server, but only for serving html, jsp and servlets.

• Many servlet containers could be used for this course. Some may even be easier to configure than tomcat, but tomcat provides an easy-to-use development/deployment tool and also complies with the servlet specification best of all containers.

• Tomcat is from Apache and is open-source.• Tomcat can be used as a stand-alone servlet container.• You can install the Apache server with Tomcat, and then

proceed to configure each for their individual purposes. (The server would relay servlet requests to Tomcat.)

Page 3: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

About these notes

• Two texts & many of my own examples were used to compile servlet notes.

• The texts referenced are Jason Hunter’s Servlet book and Core Servlets Vol 1 by Hall & Brown. Both excellent books.

Page 4: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Asides: XML

• XML stands for eXtensible-markup-language and is a SGML.

• Unlike older HTML, XML can be validated.• XHTML, for example, is HTML that is XML compliant.• XML is widely used in internet programming. Some

technologies - like SOAP - are based on XML. • Many files on Tomcat are XML.• In the past I’ve taught XML as a full component of this

course. I am not doing that this semester. There are websites with tutorials. I have links to a whole text of xml content at

http://employees.oneonta.edu/higgindm/internet%20programming/xmllinks.html

Page 5: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

More Asides

• Case sensitivity – Java, C, C++ and Ruby are all case-sensitive

languages. – At times my notes and ppts will be sloppy and

incorrectly show case, but in your work you can’t be sloppy.

• JAR files, classpath, batch files, environment variables, even WAR files: We are likely to use all of these.

Page 6: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Download tomcat at http://tomcat.apache.org/

• Apache recommends downloading the newest version, although the core servlets text was written with Tomcat 5.

• Download the appropriate zip and extract the files to a directory named, for example, jakarta-tomcat-5.0.28 (or whatever the version is)

• The Core Servlets text maintains a site with tips for configuring different versions of Tomcat at http://www.coreservlets.com. This site has a preconfigured version of Tomcat which I used.

Page 7: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Servlets API

• Links to servlet API are on the text’s web site• You should bookmark the servlet API for

reference purposes: http://tomcat.apache.org/tomcat-5.5-doc/servletapi/

• The servlet class files also need to be on your classpath. These are part of java EE but not part of java SE. I downloaded them and put mine in a c:\servlets directory. You may instead include the jar file “copy” from the server’s library on your classpath. This would be something like

C:\tomcatdir\common\lib\servlet.jar

Page 8: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Classpaths & Environment variables

• Classpaths are where the java compiler looks to find classes needed to perform compilation.

• On your machine, to successfully compile java servlets, packages, and so on, you will have to create an autoexec file to set classpaths, use a special batch file for compilation, or edit the classpath environment variables.

• The classpath setting may need editing whenever you create a package with new classes in it, but should be ok otherwise. Text site has some suggestions of what it should look like. Mine is:

C:\java\sdk\jdk\bin;c:myclasses;.;C:\apache-tomcat-6.0.10\lib\servlet-api.jar;C:\apache-tomcat-6.0.10\lib\jsp-api.jar;C:\apache-tomcat-6.0.10\lib\el-api.jar

• Add ;newdir to the end of this when you add a new classpath.• You may need to define environment variable CATALINA_HOME. Got to

control panel and select system, then select advanced and then environment variables.

• Select new (or edit) and add these (one at a time) along with their paths.• For CATALINA_HOME, point to the jakarta-tomcat-v# directory. • For JAVA_HOME point to the jdk directory

Page 9: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Servlets+JSP

• This directory came in a pre-configured tomcat version on the Core Servlets book’s site which I installed. It unzipped into the tomcat directory but I moved it up to c:

Page 10: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Test tomcat installation• Startup tomcat (click the bat file in tomcat/bin) and open

http://localhost in a browser window. Some tomcat stuff should display. If you don’t see this there could be many reasons. Probably Tomcat is not configured properly, but you might try http:/localhost:8080 because some versions of Tomcat are preconfigured to listen at port 8080. (This feature can be modified by editing the conf/server.xml file.)

• Shutdown tomcat by clicking the shutdown.bat file. • (If your installation has a control panel then use the

startup/shutdown buttons provided).• BTW: you’ll need to shutdown/startup to get tomcat to recognize an

edited/recompiled classfile unless you edit the server.xml file and add

<DefaultContext reloadable=“true” /> to the Service element of this file.

Page 11: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Testing it

• Download or copy the helloservlet.java (It is in the servlets+jsp directory also.)

• Compile it. Getting the compile to work will reveal problems with your classpath settings.

• Drop the class file into c:/your-tomcat-dir/webapps/ROOT/WEB-INF/classes directory

Page 12: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

HelloServlet.java… note importsimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + "<HTML>\n" + "<HEAD><TITLE>Hello</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1>Hello from Higgins in the world of servlets</H1>\n" + "</BODY></HTML>"); }}

Page 13: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Test the hello servlet. You may edit it first

Page 14: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

HelloServlet2.java: a servlet in a package to test

package coreservlets;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class HelloServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + "<HTML>\n" + "<HEAD><TITLE>Hello (2)</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1>Hello (2)</H1>\n" + "</BODY></HTML>"); }}

Page 15: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Hello2

Page 16: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

A servlet using a package of utilities to test: HelloServlet3.java minus imports

public class HelloServlet3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Hello (3)"; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1>" + title + "</H1>\n" + "</BODY></HTML>"); }}

Page 17: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Hello3 servlet uses a utility class in the same package

Page 18: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Apache Tomcat Native Library

• You may get an error that the APR is missing. Tomcat will run ok without it.

• To eliminate this warning, you need to get a dll file for the appropriate tomcat version (mine wanted tcnative.dll v1.8) and put it into C:/your-tomcat/bin and make sure this dir is on the path settings to get rid of the error. More suggestions are at:

• http://tomcat.apache.org/native-doc/

Page 19: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Remarks on Tomcat5.5

• Does not require editing classpath or definition of JAVA_HOME or CATALINA_HOME

• Has windows installer download

• Has GUI interface for startup/shutdown

Page 20: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

tomcat5w

• The new version of tomcat (5.5) seems to install itself ok and comes with a GUI for configuration, startup and shutdown

Page 21: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

ROOT

• Under the jakarta directory is jakarta-tomcat-v.?.? And under that is webapps. In this directory appears ROOT. The servlets deployed in ROOT are recognized by the server without further path information.

• I will often deploy my servlets elsewhere.

Page 22: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Servlet directory structures

• In the webapps directory of jakarta/tomcat create a directory for your work, let’s say you call it MyStuff

• Create 2 directories, servlets and web-inf under MyStuff.

• In servlets, you will put all your servlet.html files.• In web-inf you will place web.xml (your servlet

information file) and a directory called classes, where you’ll place the servlet.class files.

Page 23: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Directory Structure

Jakarta/tomcat

Webapps

ROOT myexamples

Servlets- put html in here

Web-inf

Other directories

web.xml

Class- put the .class files in here

Page 24: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

web.xml

• I’ve given you a model of your web.xml showing how to place servlet information in the xml file, and also how to “grow” the file as you complete more servlets.

Page 25: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

web.xml<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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<!-- General description of your Web application --> <display-name> Java How to Program JSP and Servlet Chapter Examples </display-name>

<description> This is the Web application in which we demonstrate our JSP and Servlet examples. </description>

Page 26: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

web.xml- con’t<!-- Servlet definitions --> <servlet> <servlet-name>welcome1</servlet-name>

<description> A simple servlet that handles an HTTP get request. </description>

<servlet-class> WelcomeServlet </servlet-class> </servlet><servlet> <servlet-name>welcome2</servlet-name>

Page 27: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

web.xml- con’t<description> A simple servlet that handles an HTTP get request with data. </description>

<servlet-class> WelcomeServlet2 </servlet-class> </servlet> <!-- Servlet mappings --> <servlet-mapping> <servlet-name>welcome1</servlet-name> <url-pattern>/welcome1</url-pattern> </servlet-mapping><servlet-mapping> <servlet-name>welcome2</servlet-name> <url-pattern>/welcome2</url-pattern></web-app>

Page 28: The Tomcat Servlet Container. About Tomcat A “servlet container” is like a mini server, but only for serving html, jsp and servlets. Many servlet containers

Tomcat

• You’ll have to watch the blackscreen associated with your server session for errors on startup or reloads. If these occur you may have to fix them before your servlets can be accessed.