130 likes | 306 Views
Java Servlets. CS-422. Application Mapping. Your servlet application will be mapped to a directory structure: “myapp” maps to some directory C:/docs/apps/myapp in the myapp directory create a directory called “Web-inf” in Web-inf create classes jsp
E N D
Java Servlets CS-422
Application Mapping • Your servlet application will be mapped to a directory structure: • “myapp” maps to some directory C:/docs/apps/myapp • in the myapp directory create a directory called “Web-inf” • in Web-inf create • classes • jsp • lib - use this for common things like JDBC drivers • sessions • these directories are used for the various components of your application • when invoking your servlet • http://www.myserver.com/myapp/servlet/servletname • the “magic name” myapp/setvlet will really refer to • c:docs/apps/myapp/Web-inf/classes
Deploying your application • To deploy a servlet based application create a .war (web archive) file containing all of the files needed for the application and place it on the server • the servlet container will work from the web archive extracting files as needed • the base JVM has all of the classes it needed for working with files in the various versions of zipped files. • Use the JDK JAR utility to create your .war file • ex cd myapp; jar -cvf myapp.war (make sure that …JDKnnn/bin is in your path and all of the files in the current directory will be placed into the file myapp.war • copy the .war file to the webapp directory of your server and its work • make sure to back-up both your development directory and your deployed war file.
Servlet Life • A servlet can come into existence for two reasons: • it was configured to be loaded on start-up of the servlet engine • a user request called for it • a servlet object is created on the main thread • init( ) is started on a secondary thread • the servlets init( ) method is guaranteed to be called once (and only once) for the life of a given servlet. • init( ) is guaranteed to complete before any service methods are invoked. • the main thread for the servlet will terminate at the end of the init( ) • when a request for the servlet is received the servlet engine spawns a new thread and calls the appropriate service( ) method • in the case of http this will be: • doGet • doPost • doDelete • doOptions • doPut • doTrace
Servlet Life (more) • Once the servlets life cycle us up (when all associated threads quiesce) the destroy( ) method will automatically be run
Servlet APIs • The servlet APIs are split into two packages: • javax.servlet • contains classes and interfaces that implement generic, protocol independent servlets, having this generiv layer allows it to be extended other protocols (like IIOP) • javax.servlet.http • extends javax.servlet for the HTTP protocol • javax implies it is an official java extension • TOMCAT is the official reference implementation
J2EE Application Servers • Tomcat – Apache Foundation – Open Source • JRUN – Macromedia • WebSphere – IBM • BEA Weblogic – BEA • Sun Java System Application Server – SUN • Oracle Application Server – Oracle Corp.
Deployment • Tomcat v.4 (Apache Foundation, Jakarta Project) • Implements the Servlet 2.3 and JSP 1.2 Specifications • Servlet 2.3 Specification • Outlines requirements for deploying servlets • Directory Structure • Deployment Descriptors
Directory Structure • /application • /application/WEB-INF • Web.xml (deployment Descriptor) • /application/WEB-INF/classes • Compiled class files • May be a WAR (Web Archive file containing all class files); create using the JAR application found in the JSDK bin directory
Deployment Descriptors • XML file describing the application • Used by the server to deploy the application • DTD is at java.sun.con/j2ee/dtds/web-app_2_2.dtd
Web.xml (sample) <web-app> <display-name>CS422Sample</display-name> <description>Steflik’s Sample App for CS422</description> <servlet> <servlet-name>BrowserDataServlet</servlet-name> <servlet-class>BrowserDataServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>BrowserDataServlet</servlet-name> <url-pattern>/steflik/BrowserDataServlet</url-pattern> <servlet-mapping> </web-app>
Web Application Archive files • jar cvf mywebapp.war