Transcript lecture5_1

CS441
CURRENT TOPICS IN
PROGRAMMING LANGUAGES
LECTURE 5_1
George Koutsogiannakis/ Summer 2011
1
Web Servers
• Large and complex piece of software that interact
with a client software such as a Browser via the
http protocol.
• A web server receives a http request and sends a
http response.
• The job of the web server is to translate the http
request and find and execute the requested
resource. It sends a response back to the client
that sent the request in the form of http response
packet(s)
2
Web Server resources
• Web server resources vary i.e
– HTML or DHTML or XML script files. They should be
transferred to the client for interpretation.
– Java Applets. They should be transferred to the client
for interpretation.
– Java servlets. They get executed by the server and the
results are converted into http response data sent to
the client.
– Java Server Pages. Partial execution by the server (the
java code part) and the results including any HTML
script are sent to the client in the form of http respnse
packets.
3
Web Server resources
• Java script files.
– The Server side java script is interpreted by the
server and the results are sent to the client in the
form of http response packets.
– Client side java script files are sent ton the client
for interpretation.
– Java Beans. Executed by the server.
– Other resources such as Active Server pages,
Visual basic, PHP, C# etc.
4
Common Web Servers
• Which of the listed can be used depends on the
particular web server. Some support Microsoft type of
resources whereas others support Java type of resources.
• Some common Web Servers are:
– Apache: Open Source maintained by Apache Software
Foundation.
– IIS (Internet Information Services): Microsoft’ s Web Server.
– Tomcat: Open Source, maintained by Apache Software
Foundation. Supports Java resources. Not as powerful as Apache
web server. Can be used in low volume web climates.
– Java System Web Server, Zeus and others.
5
Execution Environment for resources
• In order for certain resources to be executed by
the web server an environment is needed (called
engine or container).
• For example:
– To execute java servlet programs a servlet container is
needed.
– To interpret Java Server Pages code a jsp container is
needed.
• Tomcat has both of the above two containers
(engines).
6
Deployment
• A web site can consist of one or more of the
resources mentioned.
• The combination of the resources used to
build the web site is called the “Application”.
• The application needs to be installed on the
web server. The process of installing it on the
web server is called “Deployment”.
7
Deployment on Tomcat
• Normally a web application should be
developed outside Tomcat.
• The applications have to be deployed in a
specific directory of Tomcat called “webapps”.
• Tomcat is normally installed (Windows O.S.) in
the path i.e:
C:\Program Files\Apache Software Foundation\Apache Tomcat
6.0.18
8
Tomcat Directories
9
Tomcat Directories
• Clicking on the webapps directory displays the
applications’ names that are deployed i.e
10
11
Web Apps
• For example the folder: MyWebSite
is a web application whose name is MyWebSite
• The name of the web application in the
webapps directory is referred to as the
“context-root” of the web application.
• The root context needs to be typed in the http
link (URL) on the client side (i.e the browser)
in order to make a connection to this specific
application on the web server.
12
Calling The web application
• To connect to Tomcat one needs the IP
address of the web server.
• In our case since the client and the web server
are collocated on the same machine the IP
address is called “localhost”.
• Besides the IP addres the software port that
the web server uses on your machine to listen
for requests, has to be listed in the url.
13
Calling The web application
• The default software for Windows based web servers is
8080. That port can be changed if it conflicts with
another application on your machine.
• Tomcat is installed as a service in Windows. That means
that every time you boot up your computer Tomcat starts
automatically and it listens for requests to come in.
• If for some reason Tomcat is not working, you can start
the service by going to the Administrative Tools of the
Control Panel and clicking on Services. You will see the
option to either start or stop any of the services including
Tomcat.
14
Calling The web application
• Tomcat ha sits own default web application
that can be called from a Browser by using
the URL:
http://localhost:8080/
• You will see the Tomcats default page that
gives you information about using Tomcat and
also provides links on the left side for various
types of information.
15
Tomcat’ s Administration
• When you installed Tomcat on your computer
you were asked to provide a password for the
administrator. Make sure that you remember
the password!
• Clicking on the Tomcat Manager link will
transfer you to a login page for the
administrator.
16
Tomcat’ s Administration
• As an administrator you will be able to:
– Observe the status of the various applications.
– Deploy new applications.
17
Deployment
• The easiest way to deploy an application is via a
war file.
• If you are creating a war file the deployment
procedure will automatically create the proper
folders and names within webapps directory of
Tomcat.
• A web application should be developed in your
directory structure in some folder not in the path
of Tomcat’s installation (IT SHOULD BE OUTSIDE
OF Program Files directory).
18
Deployment
• The first step in creating a web application is
creating this structure.
• The following table contains a sample web
application, named "MyWebSite".
• Each one of these directories should be created
from the <SERVER_ROOT> of the servlet
container.
• An example of a <SERVER_ROOT>, using Tomcat,
would be Program Files/Apache Software
Foundation/Tomcat6.8/webapps.
19
Deployment
•
The Web Application Directory Structure
1) webapps/MyWebSite
This is the root directory (root context) of the web application. All JSP, .class and
XHTML files are stored here.
2) webapps/MyWebSite/WEB-INF
This directory contains all resources related to the application that are not in the
document root of the application.
The file web.xml (deployment descriptor) resides here. No files contained in this
directory can be served directly to a client.
3) webapps/MyWebSite/WEB-INF/classes
This directory is where servlet .class files and other utility classes are located.
20
Deployment
4) webapps/MyWebSite/WEB-INF/lib
This directory contains Java Archive files that the web application depends upon.
For example, this is where you would place a JAR file that contained a JDBC
driver.
As you look over the contents of the web application's directory structure,
you will notice that web applications allow for classes to be stored in both the
/WEB-INF/classes and
/WEB-INF/lib directories. Of these two, the class loader will load classes from the
/classes directory first
followed by the JARs in the /lib directory. If you have duplicate classes in both
the /classes and /lib directories,
the classes in the /classes directory will be used.
21
Deployment Descriptor File web.xml
• The Web application deployment descriptor file web.xml
is very important and improper formatting of it can cause
your application not to be deployed!
• It describes configuration information for the entire web
application. For our application the location of the
web.xml file is
• in the /<SERVER_ROOT>/MyWebSite /WEB-INF/
directory.
• The information inside in in terms of “elements”. We are
going to discuss that more when we talk about servlets.
22
Deployment Descriptor File web.xml
• The information that is contained in the deployment
descriptor includes the following elements:
•
•
•
•
•
•
•
•
•
* ServletContext Init Parameters
* Localized Content
* Session Configuration
* Servlet / JSP Definitions
* Servlet / JSP Mappings
* Mime Type Mappings
* Welcome File list
* Error Pages
* Security
23
Deploying the web site MyWebSite
• Suppose we have a folder named MyWebSite
and inside the folder we have the resources
needed for the application to run on the web
server.
• For example we have the folder in the path:
C:\CS441\examples\ServletandApplet\MyWebSite
24
Deploying the web site MyWebSite
• Inside the folder MyWebSite we have placed
the needed resources following the folder
names that needed in a Tomcat webapp:
25
Deploying the web site MyWebSite
26
Deploying the web site MyWebSiteInside WEB-INF folder
27
Deploying the web site MyWebSite
• Packaging a Web application using WAR files
• Now that we know what a web application is, we can package it for
deployment. The standard method for packaging web applications
is to use a Web Archive file (WAR). You can create a WAR file by using
Java's archiving tool jar.
• An example of this would be to change to the root directory of your web
application and type the following command:
• >jar cvf MyWebSite.war . (Notice that dot after war and space)
(open DOS command window at the level where your folder MyWebSite
(inside the folder) exists and use the jar command).
• This command will produce an archive file named MyWebSite.war that will
contain your entire web application.
28
Deploying the web site MyWebSite
• Login as an administrator in the Manager site of
Tomcat ( From Tomcat’s Home Page link to
Manager)
• In the Tomcat Web Application Manager page
scroll down to “War File To Deploy” part.
• Browse to where your war file is located in your
directory structure and then press deploy.
• A O.K. message and an entry onto the list of
deployed webapps means successful
deployment.
29
Deploying the web site MyWebSite
• Change to the root directory of your web
application. In this case the root directory
would be
TOMCAT_HOME/webapps/MyWebSite/
• You should be able to see the war file and the
web app called for instance MyWebSite.
30
URL for your application
• Open your Browser and type the URL:
http://localhost:8080/MyWebSite/index.html
Where index.html is the resource that needs to be
called first as an example (or the name of any
other resource that needs to be called first,
depending of course on the application).
• Notice that failure to deploy is a possibility. Make
sure that you double check the structure of the
folders of your web app and more importantly
the web.xml file.
31
Undeploy
• If the web site does not work :
– In the manager page of Tomcat where web apps
status is indicated click “undeploy” in the row
where the name of the web app appears.
– Make corrections and repeat the deploy
procedure after you have created a new war file.
– Follow the same procedure every time a change is
needed in the web app.
32
Deploy Certified Applet
• Create a new webapp folder (new context-root name)
somewhere in your directory structure (not in Tomcat’s
directory structure and NOT in Program Files for Windows).
• Proceed through steps to create the certificate and import it
into cacerts keystore.
• Create the jar file of all the .class files of the Applet.
• Create the signed version of the jar file.
– Make sure that you use the alias and the passwords when the keytool
command was used for the certificate.
– Note: Do not copy certificates from one machine to another unless
you re import the pasted certificate into the new machine’s cacerts
keystore.
33
Deploy Certified Applet
• Delete the unsigned version of the jar file.
• Place the signed jar file in the WEB-INF/lib folder.
• Change the html file with applet tag to include the
applet tag attribute :
archive=“NameofyourSignedJarFile.jar”
• Create the war file with the same name as the
context-root (you must be in the webapp folder
where your html file is located).
• Deploy the web file.
34
Deploy Certified Applet
• Test your web site by opening an instance of
the browser and typing the proper URL for the
new web site.
• A message may be shown asking you to agree
to run the certified applet.
• Test the functionality of the File menu. This
time the exit menu item and the menu items
that have to do with opening files should
work!
35
Deploy Certified Applet
36
Study
• Text: Web Based Application Development
pages 35-39.
• Tomcat’s documentation that came with the
download.
37