A web application is an application that is accessed via web browser over a network. Server-side technologies, such as Java EE, Microsoft .NET, and PHP, are used to provide dynamic functionalities for a web application. A database is usually necessary to provide data storage for a mission-critical web application.
In this class, we only focus on the Java-based web technologies. From now on, when we mention web applications, we mean those web applications that use the Java-based web technologies.
Tier | Technologies |
---|---|
Presentation | HTML/XHTML, CSS, etc. |
Business logic | Servlets, JavaServer Pages, tag libraries, etc. |
Database access | MySQL, Oracle, etc. |
A web application bundles all the web content into a single collection that can be used on any server compatible with servlet specification. The main purpose of web applications are three folds:
A web container is a runtime platform that provides the services to web components. A Web container is essentially the component of a Web server that interacts with the servlets. The Web container is responsible for managing the lifecycle of servlets. In other words, a web container is essentially a servlet container.
Web components provide the dynamic extension capabilities for a web server. When a web-based client such as a browser communicates with a Java EE application, it does so through web components. There are three types of web components:
HTTPServletRequest
object.
HTTPServletRequest
object carries all the data that the users enter into the
forms of the web pages, and it can be conveniently handled by Java classes.
HTTPServletRequest
object is delivered to a web component, such as a servlet or
a JSP, which can interact with JavaBeans components or a database to generate dynamic content.
HTTPServletResponse
object
or it can pass the request to another web component. Eventually a web component
generates a HTTPServletResponse
object.
HTTPServletResponse
to an HTTP response and returns
it to the client.
.jsp
extension.
WEB-INF
are directly accessible to clients through URLs. WEB-INF
web.xml
and subdirectories classes
and
lib
. WEB-INF\web.xml
web.xml
, configures the resources such as servlets
and welcome files of the web application. It must conform to the schema described in the Java
Servlet Specification. WEB-INF\classes
WEB-INF\lib
Duke
ExampleDuke
Duke
Duke\index.jsp
Duke\response.jsp
Duke\duke.waving.gif
Duke\WEB-INF
Duke\WEB-INF\web.xml
Duke\WEB-INF\classes
is not needed. We do not use any libraries
in this example, so there is no Duke\WEB-INF\lib
folder.
An image file: Duke\duke.waving.gif
Web components mean dynamic resouces of the web application.
There are two web components in this example: Duke\index.jsp
and Duke\response.jsp
.
Code Listing: Duke\index.jsp
1: <%@page contentType="text/html"%> 2: <%@page pageEncoding="UTF-8"%> 3: 4: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 5: "http://www.w3.org/TR/html4/loose.dtd"> 6: 7: <html> 8: <head><title>Hello</title></head> 9: <body bgcolor="white"> 10: <img src="duke.waving.gif"> 11: <h2>Hello, my name is Duke. What's yours?</h2> 12: <form method="get"> 13: <input type="text" name="username" size="25"> 14: <p></p> 15: <input type="submit" value="Submit"> 16: <input type="reset" value="Reset"> 17: </form> 18: 19: <% 20: String name = request.getParameter("username"); 21: if (name != null) { 22: %> 23: <%@include file="response.jsp" %> 24: <% 25: } 26: %> 27: 28: </body> 29: </html>Explanation:
HTTPServletRequest
object created by the container. Then the value
of the parameter username can be accessed through its getParameter method.
Duke\response.jsp
1: <h2><font color="black">Hello, <%= request.getParameter("username") %>!</font></h2>Explanation:
web.xml
Code Listing: Duke\WEB-INF\web.xml
1: <?xml version="1.0" encoding="UTF-8"?> 2: 3: <web-app xmlns="http://java.sun.com/xml/ns/j2ee" 4: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5: xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 6: version="2.4"> 7: <session-config> 8: <session-timeout> 9: 30 10: </session-timeout> 11: </session-config> 12: <welcome-file-list> 13: <welcome-file> 14: index.jsp 15: </welcome-file> 16: </welcome-file-list> 17: </web-app>
web.xml
without changing the Java code.
WEB-INF
directory within each web application.
web-app
. (See line 3 and line 17.)
web-app
element.
This declaration tells the server the version of the specification that applies and specifies the
XML schema location that governs the syntax of the rest of the file. (See line 3-6.)
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
must actually exists,
pointing to the location of the XML schema file. The rest of the URL looking strings are namespaces.
web-app
does not matter if you are using version 2.4.
This is one of the major differences between web.xml
of version 2.4 and earlier versions.
welcome-file-list
element tells the server what file to use when the server receives
URLs that refer to the context-root name (sometimes the same as the directory name) but not
a file name.
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list>The server would try
index.jsp
first and index.html
second. If neither is found,
the result is server specific.
Question: What would happen in Tomcat if there is no welcome file given in the web.xml
?
web.xml
.
web.xml
.