File Structures for Servlets
Fall 2005, David Matuszek

These are notes that I made for myself, while getting assorted servlets to work. I'm posting it for whatever value it might have.

Where there are numbered options, the numbers in each section must correspond.

Simple web page; no servlets or JSP
URL
  1. http://192.168.1.152/Hello.html
  2. http://192.168.1.152/simple/Hello.html
  3. http://192.168.1.152/SimpleNonROOT/Hello.html
Description Static web page; WEB-INF folder must be present
File structure
  1. webapps
    • ROOT
      • Hello.html
      • WEB-INF
  2. webapps
    • ROOT
      • simple
        • Hello.html
      • WEB-INF
  3. webapps
    • SimpleNonROOT
      • WEB-INF
      • Hello.html
web.xml none

 

A simple servlet
URL http://192.168.1.152/ch1/Serv1
Description Servlet that displays the current time.
File structure
  • webapps
    • ch1
      • WEB-INF
        • classes
          • Ch1Servlet.class
        • web.xml
package none
servlet-class Ch1Servlet
url-pattern /Serv1

 

JSP only
URL http://192.168.1.152/Hello.jsp
Description JSP only page; WEB-INF folder must be present
File structure
  • webapps
    • ROOT
      • Hello.html
      • WEB-INF
web.xml none

 

A packaged servlet that uses JSP
URL http://192.168.1.152/Beer-v1/form.html
Description Beer HTML page calls BeerExpert to compute results.
File structure
  • webapps
    • WEB-INF
      • classes
        • com
          • example
            • model
              • BeerExpert.class
            • web
              • BeerSelect.class
      • web.xml
    • form.html
    • result.jsp
package

com.example.model
com.example.web

servlet-class com.example.web.BeerSelect
url-pattern /SelectBeer.do

 

A servlet that gets parameters
URL
  1. http://192.168.1.152/TestInitParams/TestInitParams
  2. http://192.168.1.152/TestInitParams2
Description Servlet reads parameters from web.xml file
File structure
  1. webapps
    • TestInitParams
      • WEB-INF
        • web.xml
        • classes
          • com
            • example
              • TestInitParams.class
  2. webapps
    • TestInitParams2
      • WEB-INF
        • web.xml
        • classes
          • TestInitParams2.class
package
  1. com.example
  2. (none)
servlet-class
  1. com.example.TestInitParams
  2. TestInitParams2
url-pattern
  1. /TestInitParams
  2. /TestInitParams2

 

A servlet that opens a dialog on the server (!)
URL
  1. http://192.168.1.152/SendText/SendText
  2. http://192.168.1.152/SendText/
    (if final slash is omitted, servlet works, and the response page adds a slash to the URL)
  3. http://192.168.1.152/SendText (see below)
Description

Servlet opens an input dialog and sends the inputted text back in an HTML page. All newlines are lost, however.

Observation: The URL is the path structure, skipping over WEB-INF/classes/. Hence,
webapps/x/WEB-INF/classes/y/z/Code.class -->
/x/y/z/Code

File structure
  • webapps
    • SendText
      • WEB-INF
        • classes
          • SendText.class
          • [some additional classes]
        • web.xml
package none
servlet-class SendText
url-pattern
  1. /SendText
  2. /
  3. This is actually <url-pattern></url-pattern> -- worked yesterday, but no longer works.

 

A servlet that returns a jar file
URL http://192.168.1.152/JarDownload/form.html
Description HTML page has a link to http://192.168.1.152/JarDownload/FunWithPanels.jar
File structure
  • webapps
    • JarDownload
      • form.html
      • SwingLayoutExamples.jar
      • WEB-INF
        • classes
          • CodeReturn.class
        • web.xml
package none
servlet-class CodeReturn
url-pattern /FunWithPanels.jar

Things I think I've learned

You always need a WEB-INF folder, even if it's empty.

If the package declaration says
     package x.y.z;
then the file structure is
     webapps - projectName - WEB-INF - classes - x - y - z - MyCode.class
and the servlet-class is
     x.y.z.MyCode
and the url-pattern is
     /whatever
then we need the URL
     http://localhost/whatever/