Java Reference
In-Depth Information
Then you need to set up your servlet in web.xmlas usual. Here you map the servlet to the
/Products path:
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>SimpleRestServlet</servlet-name>
<servlet-class>com.soacookbook.SimpleRestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleRestServlet</servlet-name>
<url-pattern>/Products</url-pattern>
</servlet-mapping>
/...
You can invoke the servlet using a simple JSP that includes a link like this:
<a href="Products">Get Products</a>
When you run the application, you're presented with the index.jsp page at ht-
tp://localhost:8080/SimpleRestServlet/ . Click the link, and the servlet returns the following
response:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<productCatalog>
<products>
<id>123</id>
<name>Shirt</name>
<price>159.95</price>
</products>
<products>
<id>456</id>
<name>Monkey</name>
<price>2500.0</price>
</products>
</productCatalog>
This simple example is a good introduction to REST because it builds on powers that you
already have. But if you've worked on Java web applications of any size, you probably used
some kind of framework, such as Struts. Making services using a simple servlet as shown here
can become inefficient, which is why a framework can be handy. The reason it becomes inef-
ficient specifically for REST is that you need to map multiple URIs to the same servlet as base
paths, and then dispatch specific requests from that servlet to individual services in order to
Search WWH ::




Custom Search