1 / 13

Java Servlets

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

hertz
Download Presentation

Java Servlets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Servlets CS-422

  2. 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

  3. 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.

  4. 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

  5. Servlet Life (more) • Once the servlets life cycle us up (when all associated threads quiesce) the destroy( ) method will automatically be run

  6. 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

  7. Scope

  8. 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.

  9. 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

  10. 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

  11. 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

  12. 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>

  13. Web Application Archive files • jar cvf mywebapp.war

More Related