August 2013
Intermediate to advanced
720 pages
16h 23m
English
You want to add new servlets to your Scalatra application, and need to know how to add them, including defining their URI namespace.
Scalatra provides a nice way of getting you out of the business of declaring your servlets and servlet mappings in the web.xml file. Simply create a boilerplate web.xml file like this in the src/main/webapp/WEB-INF directory:
<?xml version="1.0" encoding="UTF-8"?><web-appxmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"><listener><listener-class>org.scalatra.servlet.ScalatraListener</listener-class></listener><servlet-mapping><servlet-name>default</servlet-name><url-pattern>/img/*</url-pattern><url-pattern>/css/*</url-pattern><url-pattern>/js/*</url-pattern><url-pattern>/assets/*</url-pattern></servlet-mapping></web-app>
Next, assuming that you’re working with the application created in Recipe 15.5, edit the src/main/scala/ScalatraBootstrap.scala file so that it has these contents:
importorg.scalatra._importjavax.servlet.ServletContextimportcom.alvinalexander.app._classScalatraBootstrapextendsLifeCycle{overridedefinit(context:ServletContext){// created by defaultcontext.mount(newMyScalatraServlet,"/*")// newcontext.mount(newStockServlet,"/stocks/*")context.mount(newBondServlet,"/bonds/*" ...
Read now
Unlock full access