Web Application Deployment


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Web Application Deployment

  1. 1. Web Application Deployment COMP 118 Spring 2003
  2. 2. Help from Java <ul><li>Session tracking is provided by Java servlet API. </li></ul><ul><ul><li>HttpSession object </li></ul></ul><ul><ul><ul><li>Returned by getSession method of request. </li></ul></ul></ul><ul><ul><li>Can store info in session object as named attributes. </li></ul></ul><ul><ul><ul><li>setAttribute, getAttribute </li></ul></ul></ul><ul><ul><li>Other methods to configure session timeout, get information about session, etc. </li></ul></ul><ul><ul><li>Uses cookies. </li></ul></ul>
  3. 3. Example 3 <ul><li>Same as example 2, but uses built in Java session support. </li></ul>
  4. 4. Example 3 discussion <ul><li>What happens is cookies are disabled? </li></ul><ul><ul><li>Can use URL rewriting, but need to let servlet encode URL’s with session id. </li></ul></ul><ul><ul><li>Use encodeURL method of request object </li></ul></ul><ul><ul><ul><li>Does rewrites URL with session id embedded as additional parameter is cookies are turned off. </li></ul></ul></ul><ul><ul><ul><li>Returns URL unchanged if cookies turned on. </li></ul></ul></ul>
  5. 5. Java Servlet API <ul><li>Documentation on Java Servlet API </li></ul><ul><li>A good book. </li></ul><ul><ul><li>Java Servlet Programming by Jason Hunter, published by O’Reilly </li></ul></ul><ul><ul><ul><li>Unfortunately, pretty expensive. </li></ul></ul></ul>
  6. 6. Web Application Deployment <ul><li>Typically a web application is comprised of: </li></ul><ul><ul><li>HTML pages </li></ul></ul><ul><ul><li>Data </li></ul></ul><ul><ul><li>Images </li></ul></ul><ul><ul><li>Servlets </li></ul></ul><ul><ul><li>Source code </li></ul></ul><ul><ul><li>Libraries </li></ul></ul><ul><ul><li>Other resources </li></ul></ul><ul><li>A servlet is a component of a web application. </li></ul>
  7. 7. Separating server from web app. <ul><li>Person running the web server and person writing a web application running on that web server may not be the same. </li></ul><ul><ul><li>Example: a web hosting company runs a web server which “hosts” the sites of several other companies for a fee. </li></ul></ul><ul><ul><li>What could be problematic about this? </li></ul></ul><ul><ul><ul><li>Need to isolate customers. </li></ul></ul></ul><ul><ul><ul><li>Web app developer shouldn’t need to know anything about web server’s installation. </li></ul></ul></ul>
  8. 8. Standardizing deployment <ul><li>Server products (like Tomcat) have support for setting up resources and isolating one web application from another. </li></ul><ul><li>Old days: different servers each did things their own way. </li></ul><ul><li>Now: there is a standard (Servlet 2.4) that defines exactly how a web application is organized and how a server maps requests to it. </li></ul><ul><ul><li>Note: our version of Tomcat implements 2.3 </li></ul></ul>
  9. 9. Mapping to a web app. <ul><li>The server is responsible for mapping URL’s that start with a specific prefix to the location of a web application. </li></ul><ul><ul><li>This is the “root” of the web application. </li></ul></ul><ul><li>In Tomcat, this is done with a configuration file. </li></ul><ul><li>We have defined a single web application that maps to each of your class directories. </li></ul>
  10. 10. Web app structure <ul><li>There is a specific organization of the files within a web application that you need to adhere to. </li></ul><ul><li>/WEB-INF </li></ul><ul><ul><li>Private resources of a web application that can not be directly served to a client. </li></ul></ul><ul><li>/WEB-INF/web.xml </li></ul><ul><ul><li>Deployment descriptor that configures the web application. </li></ul></ul>
  11. 11. Web app structure <ul><li>/WEB-INF/classes </li></ul><ul><ul><li>Class files for servlets </li></ul></ul><ul><ul><li>Directory structure needs to reflect package names. </li></ul></ul><ul><li>/WEB-INF/lib </li></ul><ul><ul><li>Libraries (jar) files that may be needed by servlets </li></ul></ul>
  12. 12. Common code <ul><li>Many web applications may need access to common libraries and classes. </li></ul><ul><ul><li>Servlet API </li></ul></ul><ul><ul><li>XML processing libraries </li></ul></ul><ul><ul><li>Email processing libraries </li></ul></ul><ul><li>These are put in the server’s root </li></ul><ul><ul><li>/common/classes </li></ul></ul><ul><ul><li>/common/lib </li></ul></ul>
  13. 13. COMP 118 Setup <ul><li>Server: wwwj.cs.unc.edu:8080 </li></ul><ul><li>To compile correctly, be sure to: </li></ul><ul><ul><li>setenv JAVA_HOME /usr/java1.2 </li></ul></ul><ul><ul><li>setenv CLASSPATH /opt/jakarta-4.1.18/common/lib/servlet.jar:. </li></ul></ul>
  14. 14. web.xml <ul><li>This file contains important configuration information needed to make servlets work. </li></ul><ul><li>For a servlet to function properly, you need to add a “servlet definition” and a “servlet mapping” for each servlet. </li></ul><ul><ul><li>/servlet/class_name used to be a shortcut </li></ul></ul><ul><ul><ul><li>Supposed to work, but doesn’t seem to in the new version. </li></ul></ul></ul><ul><li>The order of things in web.xml is important. </li></ul><ul><ul><li>We will put in a template which you can modify. </li></ul></ul>
  15. 15. Defining Servlets <ul><li><servlet> </li></ul><ul><li><servlet-name>Name</servlet-name> </li></ul><ul><li><servlet-class>servlet.class</servlet-class> </li></ul><ul><li></servlet> </li></ul>
  16. 16. Servlet Mapping <ul><li>This determines what URL’s get mapped to the servlet. </li></ul><ul><li><servlet-mapping> </li></ul><ul><li><servlet-name>Name</servlet-name> </li></ul><ul><li><url-pattern>/path</url-pattern> </li></ul><ul><li></servlet-mapping> </li></ul>
  17. 17. Revisiting previous examples.
  18. 18. Administrivia <ul><li>Examples from class will be put up on the web site. </li></ul><ul><li>First programming assignment will be put up on the web site by the weekend. </li></ul><ul><li>Make a home page (index.html) and put it at the top of your class directory. </li></ul><ul><ul><li>Put a picture and your name on it. </li></ul></ul><ul><li>Write and deploy a “hello world” servlet to test your environment. </li></ul>
  19. 19. Initialization Parameters <ul><li>Can use web.xml to provide initialization parameters to a web application. </li></ul><ul><ul><li>Within servlet definition: </li></ul></ul><ul><ul><ul><li><init-param> </li></ul></ul></ul><ul><ul><ul><ul><li><param-name>name</param-name> </li></ul></ul></ul></ul><ul><ul><ul><ul><li><param-value>value</param-value> </li></ul></ul></ul></ul><ul><ul><ul><li></init-param> </li></ul></ul></ul><ul><li>Example reading book info from file. </li></ul>
  20. 20. Debugging <ul><li>Our server is set up to log error messages to a file in WEB-INF/logs </li></ul>
  21. 21. Multithread Issues <ul><li>There is one instance of each servlet deployed which handles every request to that servlet. </li></ul><ul><li>But, each request is handled in its own thread. </li></ul><ul><ul><li>When will this cause problems? </li></ul></ul><ul><ul><ul><li>Problematic if the servlet updates non-local variables (i.e., persistent state). </li></ul></ul></ul><ul><ul><li>What is the solution? </li></ul></ul><ul><ul><ul><li>Use Java’s synchronization mechanisms </li></ul></ul></ul><ul><ul><ul><li>Example using forms to add new books. </li></ul></ul></ul>
  22. 22. Redirection and Errors <ul><li>Servlet can redirect request </li></ul><ul><ul><li>sendRedirect() </li></ul></ul><ul><li>Servlet can generate an error page. </li></ul><ul><ul><li>sendError() </li></ul></ul><ul><ul><li>Status codes in documentation for HttpServletResponse </li></ul></ul>
  23. 23. Generating Your Own Error Pages <ul><li>Server puts together error page to report errors to users. </li></ul><ul><li>You can configure your web application in order to generate your own error pages. </li></ul><ul><ul><li><error-page> </li></ul></ul><ul><ul><ul><li><error-code> 400 </error-code> </li></ul></ul></ul><ul><ul><ul><li><location> /400.html </location> </li></ul></ul></ul><ul><ul><li></error-page> </li></ul></ul>