TurboManage

David Chandler's Journal of Java Web and Mobile Development

  • David M. Chandler


    Web app developer since 1994 and former Developer Advocate with Google now residing in Peru. I am currently offering public and private developer training courses in the US and Latin America as well as working on Android, GWT, and App Engine projects.

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 227 other followers

  • Sleepless Nights…

    December 2009
    S M T W T F S
    « Nov   Jan »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Blog Stats

    • 672,648 hits

How to inject Guice objects in a JSP

Posted by David Chandler on December 11, 2009

My current project is written mostly in GWT; however, the GWT module is hosted in a JSP page that needs to obtain the user’s first name from my user service, which is managed by Guice. Since I have only one elementary JSP page, it is not worth introducing Struts or another MVC framework with Guice integration already built in, so I need a way to way to get the Guice context inside the JSP.

It now occurs to me that my login servlet could simply put the needed information into the session, where it could easily be accessed via JSP sessionScope; however, lucky for you, I had to go the long way around before I thought of this. It’s pretty simple, really. In a Web application, Guice gets initialized at application startup in your subclass of GuiceServletContextListener. Normally, you only need to override the getInjector() method of this class, like this:

public class DispatchServletGuiceConfig extends GuiceServletContextListener
{
	@Override
	protected Injector getInjector()
	{
		return Guice.createInjector(new ServerModule(),
			new DispatchServletModule());
	}
}

However, this simple version does not permit direct access to the injector (and therefore instances managed by Guice) except in other classes created by Guice. Since the JSP servlet doesn’t know about Guice, we need to make the Guice injector available ourselves. Fortunately, the GuiceServletContextListener and JSP pages both share access to the ServletContext, so we can override the other two methods in our Guice context listener to put the injector in the ServletContext. The GuiceServletContextListener no doubt does this already, but not where we can predictably find it when a new version comes out, etc. Here’s the expanded implementation:

package com.roa.server.guice;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.turbomanage.gwt.server.guice.DispatchServletModule;

/**
 * This Guice context listener creates the Guice injector. It's a little
 * more complicated than the usual single-method version in order to
 * store the injector in ServletContext for access by JSPs.
 *
 * @author David Chandler
 */
public class DispatchServletGuiceConfig extends GuiceServletContextListener
{
	@Override
	protected Injector getInjector()
	{
		return Guice.createInjector(new ServerModule(),
			new DispatchServletModule());
	}

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent)
	{
		// No call to super as it also calls getInjector()
		ServletContext sc = servletContextEvent.getServletContext();
		sc.setAttribute(Injector.class.getName(), getInjector());
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent)
	{
		ServletContext sc = servletContextEvent.getServletContext();
		sc.removeAttribute(Injector.class.getName());
		super.contextDestroyed(servletContextEvent);
	}

}

Now in our JSP, we can access the injector via the ServletContext:

<%@ page import="com.google.inject.Injector"%>
<%@ page import="com.google.inject.Guice"%>
<%@ page import="com.roa.server.service.common.ROAUserService"%>
<%@ page import="com.roa.common.domain.User"%>
...
<%
	Injector inj = (Injector) pageContext.getServletContext().getAttribute(Injector.class.getName());
	ROAUserService roaUserService = inj.getInstance(ROAUserService.class);
	User user = roaUserService.getLoggedInUser();
%>

The merits of this approach in this case are admittedly debatable. However, it never hurts to have another tool in your toolbox…

One Response to “How to inject Guice objects in a JSP”

  1. [...] How to inject Guice objects in a JSP [...]

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 )

Google+ photo

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

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 227 other followers

%d bloggers like this: