Edit Edit

Java Servlet FAQ


How do you install a servlet on Apache?



As of 1.3.9, Apache does not have servlet and JSP support integrated in the main download yet. You can install Tomcat which is a reference implementation of JSP 1.1 and Java Servlets 2.2. Tomcat is a separate Java program that is run in its own process (outside of Apache). You can then install a JServ communication module in Apache so appropriate JSP and servlet URLs are invoked correctly using Tomcat. You also have to have the JSDK installed so that Tomcat can compile the JSP files into servlets.

You can place Servlets in Tomcat's lib directory either as class files or jar files; you can then invoke the servlet using the standard <hostname>/servlet/<servletname> URL. However, the recommended way of configuring servlets is via a Web Application Archive (.war) file which is a jar file with a special structure:
  index.html
  howto.jsp
  feedback.jsp
  images/banner.gif
  images/jumping.gif
  WEB-INF/web.xml
  WEB-INF/lib/jspbean.jar
  WEB-INF/classes/MyServlet.class
  WEB-INF/classes/com/mycorp/frontend/CorpServlet.class
  WEB-INF/classes/com/mycorp/frontend/SupportClass.class
You can assign a directory to the entire .war file. E.g., if you assign the archive to the "demo" directory, you have to reference servlets with the URL <hostname>/demo/servlet/<servletname>.

The web.xml is where you specify the parameters for the servlet. E.g., here is the web.xml for the Tomcat JSP servlet:
<!--
     A simple web.xml file example.  For article text, see
     http://www.javaworld.com/jw-10-1999/jw-10-servlet22.html
-->

<web-app>
    <servlet>
        <servlet-name>
            default
        </servlet-name>
        <servlet-class>
            org.apache.tomcat.core.DefaultServlet
        </servlet-class>
    </servlet>
    <servlet>
        <servlet-name>
            invoker
        </servlet-name>
        <servlet-class>
            org.apache.tomcat.core.InvokerServlet
<!--
            org.apache.tomcat.core.NoInvokerServlet
-->
        </servlet-class>
    </servlet>
    <servlet>
        <servlet-name>
            jsp
        </servlet-name>
        <servlet-class>
            org.apache.jasper.runtime.JspServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>
            invoker
        </servlet-name>
        <url-pattern>
            /servlet/*
        </url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>
            jsp
        </servlet-name>
        <url-pattern>
            *.jsp
        </url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <mime-mapping>
        <extension>
            txt
        </extension>
        <mime-type>
            text/plain
        </mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>
            html
        </extension>
        <mime-type>
            text/html
        </mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>
            htm
        </extension>
        <mime-type>
            text/html
        </mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>
            gif
        </extension>
        <mime-type>
            image/gif
        </mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>
            jpg
        </extension>
        <mime-type>
            image/jpeg
        </mime-type>
    </mime-mapping>
    <!--
      Additional mime mappings deleted for sake of brevity
    -->
    <welcome-file-list>
        <welcome-file>
            index.jsp
        </welcome-file>
        <welcome-file>
            index.html
        </welcome-file>
        <welcome-file>
            index.htm
        </welcome-file>
    </welcome-file-list>
</web-app>



Last updated 12/26/1999 12:56:36 PM