A web container is responsible for managing the lifecycle of servlets, mapping a URL (Uniform
Resource Locator) to a particular servlet and ensuring that the URL requester has the correct access-
rights. A web container handles requests to servlets, java server pages (JSP) files, and other types of
files that include server-side code. The web container creates servlet instances, loads and unloads
servlets, creates and manages request and response objects, and performs other servlet-management
tasks.
Introduction
A web container (more commonly known as a servlet container) is an application implemented
on web servers to makes the implementation of 'Java Servlets' and 'Java Server Pages' possible. A
container creates a completely independent environment for running servlets and Java server pages for
the purpose of offering dynamic content to website visitors. It is primarily designed to run Java coding
on a web server.
All web containers are JEE (Java Platform Enterprise Edition) compliant. The servlets are
executed in the run time environment provided by the container through the use of JSP engine and
servlet engines. One of the most popular web containers is Apache Tomcat. It is an open source
software program developed by the Apache Software Foundation.
Introduction
Web container,
Web server,
Application Server.
EJB Container
Definition
In addition to the above explanation about Web Container, a Web Container has a couple of
meanings depending on the source. Most refer to a Web container as the part of an application server
that manages servlets, JavaServer Pages (JSP) files, and other Web-tier components. Some refer to a Web
container as the infrastructure for managing the lifecycle for Web services.
Web Container
Web server
A Web server refers to an execution infrastructure that handles HTTP requests and
responses. The primary function of web server is to store, process and deliver web pages to clients.
The communication between client and server takes place using the Hypertext Transfer Protocol
(HTTP). Pages delivered are most frequently HTML documents, which may include images, style
sheets and scripts in addition to the text content.
An application server acts as a set of components accessible to the software developer through a
standard API defined for the platform itself. For Web applications, these components are usually
performed in the same running environment as their web server(s), and their main job is to support the
construction of dynamic pages.
Application server
Enterprise JavaBeans (EJB) is one of several Java APIs for modular construction of enterprise
software. EJB is a server-side software component that encapsulates business logic of an application. An
EJB web container provides a runtime environment for web related software components,
including computer security, Java servlet lifecycle management, transaction processing, and other web
services. The EJB specification is a subset of the Java EE specification
EJB Container
Inside the Web Container
SERVLET
Typically, a Servlet implements some control logic. For example, a Servlet might figure out
what a user typed into some text fields in a web-based form. It might then take that information
and save it to a database. Servlets are intended to be controllers. While Servlets can interact
directly with a database, they’re not really supposed to. Instead, Servlets are supposed to delegate
to a JavaBean.
Servlet
Act as a middle layer between requests from HTTP client (browser) and resources (databases or
applications)
May perform any of the following tasks:
Read
Sent data
Request data
Send
Explicit data
Implicit response data
Generate the results
Servlet
init()
Instantiate / initialize servlet
service()
Execute the servlet
destroy()
Close the resources and remove the servlet from
memory
Valid only within the scope of a servlet’s service method or a filter’s doFilter method
Common to recycle request objects
Filter chain
Sequence of filters with each filter doing some
processing and then passing on to the next in sequence
Chain member
Filter chain in the order of their definition in
deployment descriptor
The last element of the chain is the target
resource/servlet
Servlet request - Request Path
requestURI = contextPath + servletPath + pathInfo + query string
Context Path – prefix associated with the servlet context
Servlet path – mapping to the servlet location
Path info – extra path info in URL if any
Query string – request parameter name and value pairs
Example:
http://server.com/MyApp/MyServlet/xyz?param1=value1¶m2=value2
getContextPath - /MyApp
getServletPath - /MyServlet
getPathInfo - /xyz (many time it’s null)
getQueryString - param1=value1¶m2=value2
JSP
JavaServer Pages (JSP) is a technology that helps software developers create dynamically
generated web pages based on HTML, XML, or other document types. Released in 1999 by Sun
Microsystems, JSP is similar to PHP and ASP, but it uses the Java programming language.
<p>Counting to three:</p>
<% for (int i=1; i<4; i++) { %>
<p>This number is <%= i %>.</p>
<% } %>
<p>OK.</p>
Syntax:
The output displayed in the user's web browser
would be:
Counting to three:
This number is 1.
This number is 2.
This number is 3.
OK.
1. Apache Tomcat
Apache Tomcat is a webcontainer which allows to run servlet and JavaServer Pages (JSP) based web applications. Most
of the modern Java web frameworks are based on servlets, e.g. JavaServer Faces, Struts, Spring. Apache Tomcat also provides by
default a HTTP connector on port 8080, i.e., Tomcat can also be used as HTTP server. But the performance of Tomcat is not as
good as the performance of a designated web server, like the Apache HTTP server.
2. Installation
2.1. Ubuntu Linux
For Ubuntu you can install Tomcat via the following commands.
2.2. Windows
Download the Windows installer for Tomcat7 from the Apache Tomcat Homepage and run the installer.
sudo apt-get install tomcat7
sudo apt-get install tomcat7-admin
sudo apt-get install tomcat7-docs
sudo apt-get install tomcat7-examples
3. Managing Apache Tomcat
3.1. Start Tomcat on Ubuntu (Linux)
In Ubuntu the Tomcat server is started automatically. To restart Tomcat use the following command.
3.2. Start Tomcat on Windows
To start Tomcat use tomcat7.exe in the bin directory.
3.3. Test Tomcat
The default port for Tomcat is 8080. After starting Tomcat on your local machine, you can validate if Tomcat is
running the URL:
This should show a web page similar to the following.
# Restart
sudo /etc/init.d/tomcat7 restart
# Stop
sudo /etc/init.d/tomcat7 stop
http://localhost:8080
3.4. Admin console
Tomcat provides a webbased adminstration console which can be started via the following link:
The available users can be found in the tomcat-users.xml file of the Tomcat configuration directory, i.e.,
the /etc/tomcat/tomcat-users.xml file under Ubuntu.
On Ubuntu the user for the administrator console is not created automatically, you have to add the user entry
manually to the /etc/tomcat7/tomcat-users.xml. The following listing gives an example for a user. To get more
information try to login and see the resulting error message. Once you entered a user and a password, restart the
Tomcat server to ensure your new user is activated.
The default user for the Tomcat administration console under Windows is admin with the admin password.
<role rolename="manager-gui" />
<user username="tomcat" password="s3cret" roles="manager-gui" />
http://localhost:8080/manager/html
3.5. Deployment
The standard deployment format for web applications is a .war file. If you create a war application just
put this application into the webapps folder. The next time tomcat starts it will unpack the war and make the
application available.
Web applications may require external libraries. Typically, web applications contain their own libraries but if you
want to make certain libraries available for all applications you can put them into the folder "lib" and a subfolder
below "lib". These libraries are then available for all web applications.
4. Developing Java web applications
After going through the setup you probably want to learn how to develop servlets and JSP on ab
installation directory.
As was stated earlier, the Web server tries to handle every single request that it receives.
However, when the Application server comes onto the scene, it introduces itself to the Web server and has
a conversation that goes something like this:
What Exactly Does The Web Server Plug-in Do?
Application server: Hey, Webserver..
Web Server: Yo, what’s up?
Application server: Hey, not much.
Web Server: What can I do for you?
Application server: Well, I know that you’re really great at serving up static files and all, but you’re going to
get some crazy requests for JSPs and Servlets that you won’t be able to find on your file system.
Web Server: Really? What am I going to do? I won’t be able to find any of these JSPs and Servlets, and I’ll
end up sending a bunch of 404 errors back to clients, and the clients will be pissed!
Application server: Hey, calm down. Here’s what you do: just take those requests and send them to me. I’ll
handle the request, generate some HTML, give that HTML back to you, and you can send the HTML back to
the client.
Web Server: Kewl. You do the work, but the client thinks it’s me handling the request? I like this
arrangement already. How do I know what files to send to you though?
Application server: Don’t worry. I’ll make a thorough list and write it all down in a special XML file. Just read
that file every once in a while and keep up to date on which files you need to send back to me.
Web Server: Great. But when I do get a request for an item on the list, how will I know where to send it.
Application server: Don’t worry. I’ll make a thorough list and write it all down in a special XML file. Just read
that file every once in a while and keep up to date on which files you need to send back to me.
Web Server: Great. But when I do get a request for an item on the list, how will I know where to send it.
Application server: Hey, don’t worry. I’ve got it all covered. That XML file also contains a list of which IP
addresses/port combinations to send the requests to. It’s all right there in that XML file. And if you have a
problem understanding how to use it, here’s a .dll file that explains everything to you as well. Read it every
time you start up.
Web Server: Kewl. I think this is going to be a great relationship.