Servlet Mappings
Servlet Mappings allow you to define certain URL patterns that get directed to a specified servlet. To understand this topic, it is important to understand the components of the URL. Here is a sample URL:
http://machinename:portnumber/arbs/rbsdir/display.jsp
After the name of the server box and the port number of the Web Server, there is some characters (in this case /arbs) that tell the Java App Server what Web Application to associate this request with. This is also called the Web Application's 'context root'. After that is a directory name (in this case /rbsdir - that is a subdirectory of the Web Application), and that is followed by the name of actual file to be served up - whether it is html, JSP, or Servlet.
First a word to clarify by what we mean by Web Application mapping characters, or 'context root', in this case /arbs. The way a context root for a Web Applicaiton is determined depends on whether or not the Web Application part of an Enterprise Application.
Web Application Not Part of an Enterprise Application
o A Web Application can be in a subdirectory of the root directory of the web applicaitons. In this case its context root is the name of the Web Application's subdirectory.
o One can 'jar' the contents of a Web Application directory into what is called a Web Application Archive (war file). You can put this war file (it has a suffix of .war) in the /jserv directory. In this case its context room is the name of the .war file.Web Application is Part of an Enterprise Application
o A Web Application can be in a subdirectory of the Enterprise Application root directory (root-ear in our case). Or it can be a .war file in this directory. In either case, it is defined in a file called root-ear/META-INF/application.xml. All the modules in the Enterprise Application are defined in the application.xml file, along with what their context root is. In other words, when a Web Application is part of an Enterprise Application, its context root is defined in the application.xml file for the Enterprise Application.
Only one Web Application can have a context root of "/", as defined in application.xml.
Servlet Mapping CharactersFor the first example, let's say we want a group of URL's to be handled by a Servlet called Payroll.class. You could say that you want all URL's that begin as follows to be routed to the Servlet called Payroll.class.
http://eaesvr1.nj.ssmb.com:31020/arbs/payroll
Anything after the letters /payroll in the URL are passed as parameters to the Payroll.class Servlet for it to handle. This Servlet mapping definition is put in the web.xml file as follows:
<servlet-mapping>
<servlet-name>Payroll</servlet-name>
<url-pattern>/payroll</url-pattern>
</servlet-mapping>
The second type of servlet mapping is for a set of file names. Let's say you want all URL's that end with a file name that end with the characters *.doc to be handled by the Servlet called HandleDocs.class. You would put into the web.xml file the following lines:
<servlet-mapping>
<servlet-name>HandleDocs</servlet-name>
<url-pattern>*.doc</url-pattern>
</servlet-mapping>
In order to have the Servlet Mappings take effect, the Server has to be restarted.
Parenthentically, there are two Servlet Mappings that are built into the Servlet/JSP specification. There are Servlets that are associated with the a url-pattern of /servlet to handle all Servlets, and there is a Servlet that is to be given control whenever the URL ends with a file *.jsp, to handle JSP processing.
Here is an example of the Servlet itself, to which the Servlet Mapping refers:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;public class HandleDocs extends HttpServlet{
public void service (ServletRequest req , ServletResponse res) throws IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("This Servlet Can Now Handle Documents<BR>");
}
}