We know how servlets can be configured within the web.xml deployment descriptor. JSP can also be configured within this file.The following aspects of JSP can be configured in web.xml:
Each of these will be discussed in turn.
Suppose you enter a URL such as the following:
What gets displayed in the browser? If you are familiar with configuring Web servers, you will be familiar with the concept of a welcome file.The welcome file is what is presented to a user if no specific page is requested. It is the configuration of welcome files that enables you to enter domain names into the address bar of your browser without specifying the actual file that you want.
Welcome files can be set up for your Web application within web.xml. Listing shows an example
Listing 4.1 web.xml Setting Up Welcome Files
As you can see, the core elements to enable the setting up of welcome files is the <welcome-file-list> element, which contains one or more <welcome-file> elements, each containing a relative URL for a specific welcome file.
It is also possible to map JSP files to specific URLs other than the straightforward paths to the files that you have seen up to now. For example, in all the JSP examples you have seen
The same file index.jsp could be mapped to an alternative
This is done using <servlet> and <servlet-mapping> elements that you were introduced to in Listing 1.3 of Topic “JSP, J2EE, and the Role of the Servlet.” In Listing 4.2, the <servlet> tag is used as it would be for servlets, but for a JSP, the <servlet-class> tag has been replaced by a <jsp-file> tag. Notice that the <jsp-file> and <url-pattern> elements must begin with a forward slash.
Listing 4.2 web.xml Setting Up a URL Mapping for a JSP
It is also worth mentioning at this point the <url-pattern> element can also be used to map multiple URLs to the same JSP or servlet.This is done with the use of the ‘*’ as a wild card. So, you could set up a mapping as follows:<servlet-mapping><servlet-name>SomeServlet</servlet-name><url-pattern>/folder/*</url-pattern></servlet-mapping>
In this example, the following URLs will all result in SomeServlet being invoked.
This is because the ‘*’ has been used to map any path that begins with the structure /folder/.
An alternative way of using the wildcard is to specify that any resource ending with a specific extension will be mapped.
In the above example, any URLs that are for resources within folder with the extension .html, will result in SomeServlet being invoked.
Because of the specifications in the web.xml XML Schema, the elements must be in the correct order.This means that all the <servlet> entries must precede the <servlet-mapping> entries.
you saw how servlets can be passed initialization parameters from the web.xml file at startup, and because JSPs also become servlets, it makes sense to allow them to also access initialization parameters. Listing shows a web.xml file that passes a parameter to a JSP called initialization.jsp.
Listing 4.3 web.xml Passing Initialization Parameters to a JSP
Listing is remarkably similar to Listing of Topic “JSP, J2EE, and the Role of the Servlet”, which demonstrates how initialization parameters are set for servlets.The <init-param>, <param-name>, and
<param-value> elements are used in exactly the same way.
Listing shows initialization.jsp, and how it can access the initialization parameter passed in
Listing 4.4 initialization.jsp<html><head><title>Initialization Parameters and JSP</title></head><body>
This page has retrieved the following initialization parameter from web.xml:<br><%= pageContext.getServletConfig().getInitParameter(“testParam”) %></body></html>
The initialization parameter is accessible via the ServletConfig object for this JSP, and this is obtained from the pageContext implicit variable.
JSP Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||Java Servlets Interview Questions|
|Hibernate Interview Questions||JavaServer Faces (JSF) Interview Questions|
|JSTL(JSP Standard Tag Library) Interview Questions||JBOSS Interview Questions|
|Log4j Interview Questions||NHibernate Interview Questions|
|Apache Struts 2 Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.