Skip to content

Groovy and Tomcat, Pt2 – Groovy extends HttpServlet

August 20, 2012

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.

About these ads

From → groovy, programming

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: