64 CHAPTER 2: Building Web Applications Using Servlets and JSP
Note Once the container sets the init params in ServletConfig, the container never reads init param
from the deployment descriptor again, unless the servlet is redeployed.
Context-Init Parameters
Context-init parameters are similar to init parameters. The main difference between the context-init
parameters and init params is that context parameters are available to the entire web app while the
init parameters are available to just the servlet. Listing 2-6 illustrates the context-init parameters in
the web.xml file.
Listing 2-6. Defining Context-Initialization Parameters
The
Listing 2-7 illustrates how to get the context-init parameters from your servlet.
Listing 2-7. Getting Context-Initialization Parameters
out.println(getServletContext().getInitParameter("email");
Every servlet inherits a getServletContext() method. The getServletContext() method returns a
ServletContext object.
RequestDispatcher
In a web application, there are two ways to alter the request flow.
Redirecting the request: The request is redirected to a completely different URL.
The redirect can be done by calling sendRedirect() on the response object.
The redirect is done by the browser.
Dispatching the request: The request is dispatched to another component in the
web app, typically a JSP page. A request dispatch is different from a redirect in
that it does the work on the server side. A RequestDispatcher is called on the
request, and a redirect is called on the response.
Figure 2-17 shows the methods in the RequestDispatcher interface.