CS639 Servlet1: First Web app project

package cs639.hello.servlet;

import ...

public class HelloWorld extends HttpServlet {

       public void init() throws ServletException {

              // put any init code you need here (to be called before any requests come in)

       }

 

       public void doGet(HttpServletRequest request, HttpServletResponse response)

                     throws ServletException, IOException {

              response.setContentType("text/html");

              PrintWriter out = response.getWriter();

              System.out.println("in doGet");

              out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

              out.println("<HTML>");

              out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

              out.println("  <BODY>");

              out.print("    This is ");

              out.print(this.getClass());

              out.println(", using the GET method");

              out.println(" at " + new Date());  //added by eoneil

              out.println("  </BODY>");

              out.println("</HTML>");

              out.close();

       }

...  doPost(...)

}

 

Project directories:

C:\cs\cs639>tree servlet1

Folder PATH listing

Volume serial number is 535E-BDDB

C:\CS\CS639\SERVLET1

├───.settings

├───src

   └───cs639

       └───hello

           └───servlet

└───WebContent

    ├───META-INF

    └───WEB-INF

        ├───classes

           └───cs639

               └───hello

                   └───servlet

        └───lib  (empty)

 

Deployed directories:

C:\tomcat-6.0.29\webapps>tree servlet1

Folder PATH listing

Volume serial number is 0006EE38 535E:BDDB

C:\TOMCAT-6.0.29\WEBAPPS\SERVLET1

├───META-INF

└───WEB-INF

    ├───classes

       └───cs639

           └───hello

               └───servlet

    └───lib   (empty)

 

 

 

<?xml version="1.0"?>

<!--CS639 servlet example build file

    Note: This build.xml is not useful for building an eclipse Web project.

    Instead, use it as guide to what has to be added to Web project

    built from this file tree as in a "non-default" location

    and without writing the deployment descriptor.

    We make a *portable* build.xml by setting up the platform-dependent

    locations in environment variables and accessing them from here.

    These are CATALINA_HOME and TOMCAT_URL (use tomcat.bash for Linux)

    This build file assumes assumes there is a src tree of

    sources and a WebContent/WEB-INF/web.xml file already there.

    We use the ant project name (here servlet1) as the webapp name.

-->

<project name="servlet1" default="build" basedir=".">

   <!--grab onto environment variables as follows -->

   <property environment="env"/>

   <property name="CATALINA_HOME" value="${env.CATALINA_HOME}"/>

   <property name="TOMCAT_URL" value="${env.TOMCAT_URL}"/>

   <property name="deploy.dir" value="${CATALINA_HOME}/webapps/${ant.project.name}"/>

   <property name="classes.dir" value="WebContent/WEB-INF/classes"/>

    <!--a servlet needs the servlet API for compilation -->

    <path id="project.classpath">

        <pathelement location="${classes.dir}"/>

        <pathelement location="${CATALINA_HOME}/lib/servlet-api.jar"/>

    </path>

    <target name="init">

        <mkdir dir="${classes.dir}"/>

    </target>

    <target name="clean">

        <delete dir="${classes.dir}"/>

    </target>

    <target name="build" depends="init">

        <echo message="${ant.project.name}: ${ant.file}"/>

        <javac destdir="${classes.dir}" debug="on">

            <src path="src"/>

            <classpath refid="project.classpath"/>

        </javac>

    </target>

 

    <!-- It's hard to run browsers under scripts, but wget does the job-->

    <!-- wget is on UMB UNIX/Linux systems, and is available for PCs

         (wget.exe is in tomcat's bin in provided tomcat.zip, and

         we are assuming that tomcat's bin is in your path

         so that startup.bat and shutdown.bat or work, so

         wget should work too, on PCs)-->

    <target name="test1">

        <echo message="running wget"/>

        <exec executable="wget">

          <arg line="${TOMCAT_URL}/${ant.project.name}/servlet/HelloWorld"/>

        </exec>

    </target>

 

    <!-- simple deployment: copy WebContent file tree to webapps area -->

    <!-- also, clean up first -->

    <target name="deploy" depends="build">

        <delete dir="${deploy.dir}"/>

        <copy todir="${deploy.dir}">

            <fileset dir="WebContent"/>

        </copy>

    </target>

</project>


 

web.xml files: in WEB-INF directory of webapp

 

web.xml for servlet1:


<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"

        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">

  <!--Note how <servlet-name> connects these two elements logically -->

  <servlet>

    <description>my first servlet, a description</description>

    <display-name>firstServlet display-name</display-name>

    <servlet-name>HelloWorld</servlet-name>

    <servlet-class>cs639.hello.servlet.HelloWorld</servlet-class>

  </servlet>

 

  <servlet-mapping>

    <servlet-name>HelloWorld</servlet-name>

    <url-pattern>/servlet/HelloWorld</url-pattern>

  </servlet-mapping>

 

</web-app>

 

Thus this webapp can be reached at ${TOMCAT_URL)/servlet1/servlet/HelloWorld

 

web.xml for servlet2:

<web-app version="2.4"

        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">

  <!--Note how <servlet-name> connects these two elements logically -->

  <servlet>

    <description>Servlet that serves out html from server files</description>

    <display-name>EchoHtml</display-name>

    <servlet-name>EchoHtml</servlet-name>

    <servlet-class>cs639.servlet.EchoHtml</servlet-class>

  </servlet>

 

  <servlet-mapping>

    <servlet-name>EchoHtml</servlet-name>

    <url-pattern>*.html</url-pattern>

  </servlet-mapping>

 

</web-app>

 

Thus this webapp can be reached at ${TOMCAT_URL)/servlet2/xxx.html   for any xxx