23
Web Application Deployment COMP 118 Spring 2003

Web Application Deployment

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Web Application Deployment

Web Application Deployment

COMP 118

Spring 2003

Page 2: Web Application Deployment

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.

Page 3: Web Application Deployment

Example 3

• Same as example 2, but uses built in Java session support.

Page 4: Web Application Deployment

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.

Page 5: Web Application Deployment

Java Servlet API

• Documentation on Java Servlet API

• A good book.– Java Servlet Programming by Jason Hunter,

published by O’Reilly• Unfortunately, pretty expensive.

Page 6: Web Application Deployment

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.

Page 7: Web Application Deployment

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.

Page 8: Web Application Deployment

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

Page 9: Web Application Deployment

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.

Page 10: Web Application Deployment

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.

Page 11: Web Application Deployment

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

Page 12: Web Application Deployment

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

Page 13: Web Application Deployment

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

Page 14: Web Application Deployment

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.

Page 15: Web Application Deployment

Defining Servlets

<servlet>

<servlet-name>Name</servlet-name>

<servlet-class>servlet.class</servlet-class>

</servlet>

Page 16: Web Application Deployment

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>

Page 17: Web Application Deployment

Revisiting previous examples.

Page 18: Web Application Deployment

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.

Page 19: Web Application Deployment

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.

Page 20: Web Application Deployment

Debugging

• Our server is set up to log error messages to a file in WEB-INF/logs

Page 21: Web Application Deployment

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.

Page 22: Web Application Deployment

Redirection and Errors

• Servlet can redirect request– sendRedirect()

• Servlet can generate an error page.– sendError()– Status codes in documentation for

HttpServletResponse

Page 23: Web Application Deployment

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>