As we have seen in our previous post how easy it is to configure the DI (Dependency Injection) using Google Guice. In this post we will see how we can take the advantage of Google Guice Servlet Module to integrate / setup Web Application.
Guice Servlet Module allows us to completely eliminate the web.xml from Web Application Project and configure Servlets / Filters using the Java Configuration File itself.
Benefits – Google Guice provides us Following benefits –
1) Constructor Injection, Setter, Field/member variable Injection
2) Type Safe Configuration ( eliminate web.xml file)
4) Can Inject the objects like request, response, session, requestParameters inside the servlet using DI.
5) Can define the Scope like Request Scope/ Session Scope using Annotation
6) It’s easy to use and integrate and no need to change the existing file/ servlets /jsp etc.
You can download the required Google Guice API from Google-Guice-3.0.zip. You need to extract .zip file and add all .jar files in the build path / .classpath of the project.
Let’s see the Components –
1. Configure GuiceFilter in web.xml
Need to Add the GuiceFilter in web.xml. It tells ServletContainer to re-route all the request through GuiceFilter. The Good thing is that all the existing Servlets/ JSP/ Filters will continue to function as normal and we can migrate it later.
2. Install a Servlet Module / Enable Google Guice Servlet Module
In Order to install Servlet Module, we need to take 3 steps –
i. Create a WebServletModule which Extends ServletModule
ii. Create ServletConfig which extends GuiceServletContextListener.
iii. Configure GuiceServletContextListener inside the web.xml
Let’s Create a WebServletModule - First, in Servlet Module we can specify the Servlet / Filters mapping which we use to define inside web.xml. we can think of it as replacement of web.xml with enabled Guice DI capability.
ServletConfig – ServletConfig Extends GuiceServletContextListener. Here we need to define all the modules that we want to install. In order to enable the Google Servlet Module we need to install WebServletModule and to define the binding we can have WebAppModule.
Just for Reference WebAppModule.java–
Configure Listener inside the Web.xml – In order to install the modules we need to make a entry to ServletConfig in web.xml.
Now we are done with all the configuration. Lets Create the Some html files and Servlets to run web Application using Google Guice.
User Case Scenario – A User will provide UserName and Email as Input and after submitting the data will be able to see the processes output.
Let’s see the Components Involved –
1. Entry.html – Take a input as username and Email and a submit button.
As you can see, after clicking on Submit it will call abc.html and abc.html has been mapped with EntryServlet.java in WebServletModule.java.
Below is the much awaited EntryServlet.java –
Servlet Should apply @Singleton annotation.
Instantiate Object using injector.getInstance(User.class); don’t use new operator
This servlet Instantiate the User Object and call the processRequest() of User. We have bind the User.java in WebAppModule.java.
Finally User.Java - Just to Keep it simple I have kept all objects inside one class only.
Now you can see, Since we have enabled Guice Servlet Module inside this web Application, I am able to inject request, response, session and requestParameters in User.java.
It’s quite easy to use and handle. Inside the process methods I fetched all the parameters passed from UI and set it into the UserObject itself.
Finally The User Object will be rendered to UI via EntryServlet.java
Wrap Up – Using the above Configuration we can set up google Guice Servlet Module in Web Application and take the advantages of easy to use, modularize and DI enabled code.
This post is written by
Soumitra Pathak - Linkedin, Facebook
He is a freelance writer, loves to explore latest features in Java technology.