1 / 19

Technology Overview

Technology Overview. JAVA Servlets CS-611 S. Witherspoon. JAVA Servlets. What are servlets Why use them Anatomy of a servlet Sample servlets Servlets & J2EE. Servlet paradigm. Description. Servlets are the Java platform technology of choice for extending and enhancing web servers

Télécharger la présentation

Technology Overview

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. Technology Overview JAVA Servlets CS-611 S. Witherspoon

  2. JAVA Servlets • What are servlets • Why use them • Anatomy of a servlet • Sample servlets • Servlets & J2EE

  3. Servlet paradigm

  4. Description • Servlets are the Java platform technology of choice for extending and enhancing web servers • Servlets are programs that run on a Web server and build Web pages based on data: • Submitted by user • Data that changes frequently • Data from corporate databases or other sources

  5. HTTP servlet

  6. Advantage of Servlets Over CGI • Efficient • Runs in the Java Virtual Machine • Each request is handled by lightweight thread • Only 1 copy of servlet class in memory • Optimization alternative ( caching previous computations, keeping database connections open)

  7. Advantage of Servlets Over CGI • Convenient • Extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions • Servlets receive all the benefits of the JAVA language (portability, performance, reusability and crash protection)

  8. Advantage of Servlets Over CGI • Powerful • Servlets have access to the entire family of JAVA APIs, including JDBCTM API to access enterprise databases • Servlets can talk directly to Web servers • Servlets can talk and share data with each other

  9. Advantage of Servlets Over CGI • Inexpensive • Adding server support to your web server is inexpensive of free (Apache Tomcat) • Disadvantage?? You must learn Java!!

  10. Servlet code Servlet code Servlet code Anatomy of a Servlet Load Server Server Client Handle Client Requests Client Server Unload

  11. Anatomy of a Servlet • init() • destroy() • service() • doGet() • doPost()

  12. Anatomy of a Servlet • HTTPServletRequest object • Information about an HTTP request • Headers • Query String • Session • Cookies • HTTPServletResponse object • Used for formatting an HTTP response • Headers • Status codes • Cookies

  13. Sample Servlets Servlet Examples (localhost) or CSDateTime Demo (remote host)

  14. Sample Servlet (source code) Sue Example CSDateTime

  15. Software Setup • Directory Structure <tomcat base>/webapps/ROOT/WEB-INF/classes • Environment Variables JAVA_HOME J2EE_HOME CLASSPATH=$JAVA_HOME/jre/lib/ • Path Settings PATH=$JAVA_HOME/bin:$J2EE_HOME/bin:$PATH

  16. Compiling the Servlet Sun ONE Studio • Open <servlet code>.java • Select Compile under Build menu • Copy resultant <servlet code>.class file to : <tomcat base>/webapps/ROOT/WEB-INF/classes w/o JAVA ToolKit cd <tomcat base>/webapps/ROOT/WEB-INF/classes javac <servlet code>.java

  17. Running the Servlet • Make sure Web Server (Tomcat) is running • Bring up Web Browser • Bring up Servlet http://localhost:<port>/servlet/<servlet code>

  18. Enterprise Servlets and J2EE

  19. Enterprise Servlets and J2EE • Distributed Environment (rules of thumb) • Instance variables and static variables should not be used to store state • The context should not be used to store application state • Any object placed into an HttpSession should be capable being moved to a different machine • Avoid using java.io package for file access, use getServletContext().getResource() • Syncrhonization is not global and works only for the local JVM

More Related