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