Getting Started With GServlet
Is GServlet right for your app? Let's find out.
Join the DZone community and get the full member experience.Join For Free
GServlet is an open-source project inspired by Groovlets, which aims to use the Groovy language and its provided modules to simplify Servlet API web development. Groovlets are Groovy scripts executed by a servlet. They are run on request, having the whole web context (request, response, etc.) bound to the evaluation context. They are much more suitable for smaller web applications. Compared to Java Servlets, coding in Groovy can be much simpler. It has a couple of implicit variables we can use, for example, request, response to access the HttpServletRequest, and HttpServletResponse objects. We have access to the HttpSession with the session variable. If we want to output data, we can use out, sout, and html. This is more like a script as it does not have a class wrapper:
The same philosophy has been followed in the design of this API while maintaining a class-based programming approach.
- Servlet 3.1+ Support
- Groovy Scripting and Hot Reloading
- JSON, XML, HTML and JDBC Support
- Java 8+
- Java IDE (Eclipse, IntelliJ IDEA, NetBeans..)
- Java EE 7+ compliant WebServer (Tomcat, Wildfly, Glassfish, Payara..)
Five steps take you from writing your first Groovy servlet to running it. Using Maven, these steps are as follows:
- Create a Maven webapp project
- Create the groovy folder inside your webapp directory
- Write the servlet source code
- Run your Java web server
- Call your servlet from a web browser
To get started, today we are going to rewrite the classical Java Hello World Servlet to make you perceive the difference in terms of simplicity and clarity.
Let's start by setting things up.
That's all for now and in the next articles, we will cover more ground and even, how this API can be integrated into your Spring Boot applications. This example is available for download on GitHub and you can read as well the developer guide for in-depth knowledge of this project.
Opinions expressed by DZone contributors are their own.