To tell the application server where and how the incoming HTTP
requests should be routed, we need to define a
web.xml inside the WEB-INF
directory.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <listener> <listener-class>com.couchbase.beersample.ConnectionManager</listener-class> </listener> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>com.couchbase.beersample.WelcomeServlet</servlet-class> </servlet> <servlet> <servlet-name>BreweryServlet</servlet-name> <servlet-class>com.couchbase.beersample.BreweryServlet</servlet-class> </servlet> <servlet> <servlet-name>BeerServlet</servlet-name> <servlet-class>com.couchbase.beersample.BeerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>BreweryServlet</servlet-name> <url-pattern>/breweries/*</url-pattern> <url-pattern>/breweries</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>BeerServlet</servlet-name> <url-pattern>/beers/*</url-pattern> <url-pattern>/beers</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>welcome</welcome-file> </welcome-file-list> </web-app>
Don't try to run this yet, because none of these classes are
already implemented. The listener directive
references the ConnectionMananger class,
which we'll implement to manage the connection instance to our
Couchbase cluster. The servlet directives
define our servlet classes that we're going to use (three of
them) and the following servlet-mapping
directives map HTTP URLs to them. The final
welcome-file-list directive is used to tell
the application server where to route the root URL
("/").
For now, comment out all servlet,
servlet-mapping and
welcome-file-list directives, because the
application server will complain if they're not implemented.
When you implement the appropriate servlets, remove the comments
accordingly (if you don't know how comment inside an XML file,
use the <!-- and -->
characters). If you plan to add your own servlets, don't forget
to add and map them inside the web.xml
properly!