Saturday, May 17, 2008

Servlets

  1. Servlets are simply .class files, i.e., compiled .java files.
  2. Servlet files are stored in the WEB-INF/classes subdirectory of the main web application directory. In this case the .class files for the examples web app is stored in %TOMCAT_HOME%/webapps/examples/WEB-INF/classes.
  3. So when the user requests
    http://localhost:8080/examples/servlet/HelloWorldExample
    Tomcat runs
    %TOMCAT_HOME%/webapps/examples/WEB-INF/classes/HelloWorldExample.class



  • Create a folder inside webppas of Tomcat server (star, for example)
  • "\WEB-INF\classes" is the folder to keep Servlet class files
  • 'web.xml' is saved in the '/WEB-INF' folder
  • Compile command
javac -cp %CATALINA_HOME%\lib\servlet-api.jar TestServlet.java


The url to access the servlet , after starting Tomcat server

http://localhost:8080/star/TestServlet

------------------------------------------------
1- must run Tomcat server then try http://localhost:8080 to verify it works
2- There is a specific structure for the files
/webapps/myApp/WEB-INF/classes/ will contain java classes
/webapps/myApp/WEB-INF/src/ will contain the source
/webapps/myApp/script/ will contain other script code

3-assign your servlet class to a package, for example at the beginning of the Java file
package org.oats;


4- Need to tell were the servlets resides (the path classes) using web.xml
which is saved in /webapps/myApp/WEB-INF/web.xml



Ontology
Ontology


org.oats.OntologyServlet



Finally, again in the web.xml file, add:


Ontology


/Ontology



That says "When you see the URL /Ontology, call the Servlet name "Ontology",
which is mapped to the 'org.oats.OntologyServlet' class".


5- the two main methods to implement in servlets are doGet and doPost

No comments: