public class KeyServiceServlet extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
Now we need a WEB-INF/web.xml that first defines the Servlet and does a mapping to one or multiple URLs
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.0" metadata-complete="true">
<display-name>Key Service</display-name>
<description>
Simple frontend for the test service
</description>
<servlet>
<servlet-name>KeyService</servlet-name>
<servlet-class>com.prodyna.demo.key.web.KeyServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>KeyService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
With Servlet 3.0 we now can use annotations to do the same. We simply take the @WebServlet annotation
@WebServlet(urlPatterns = { "/*" })
public class KeyServiceServlet extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
I personally love the approach of removing unneeded XML and this an example where it really makes sense.
No comments:
Post a Comment