Groovy and Tomcat, Pt2 – Groovy extends HttpServlet

Continuing where we left off…

The jars you’ll need for this part are the same as the ones for part 1:

- groovy.jar
- antlr.jar
- asm.jar

GroovyHttpServlet


package net.mymilkedeek.tomcat

import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class GroovyHttpServlet extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
 req.session.setAttribute("language", "groovy")
 req.session.setAttribute"sentiment", "awesome")
 req.session.setAttribute("message", JavaGroovy.message())
 resp.sendRedirect("index.jsp")
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
 super.doPost(req, resp)
 }
}

And add it to the web.xml

</pre>
<servlet>
 <servlet-name>GroovyHttpServlet</servlet-name>
 <servlet-class>net.mymilkedeek.tomcat.GroovyHttpServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>GroovyHttpServlet</servlet-name>
 <url-pattern>/groovyhttpservlet</url-pattern>
 </servlet-mapping>
<pre>

Now, navigate to the Servlet url and watch Groovy take care of everything:

So, in short, what we did was replace a Java HttpServlet with a Groovy HttpServlet.

For my next blog post, I’ll show you how to set up a Groovy Script in a webapplication.

Stay tuned,
Eek.

Leave a Reply