Hi Dhanji,
Thank you very much. After adding the DefaultServlet, I had it working
by modifiying Main.java to look like the following:
public class Main {
public static void main(String... args) {
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector.setHost("0.0.0.0");
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
ServletContextHandler context = new
ServletContextHandler(server,
"/", ServletContextHandler.SESSIONS);
context.addFilter(GuiceFilter.class, "/*", 0);
context.addEventListener(new GuiceServletConfig());
context.addServlet(DefaultServlet.class, "/");
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Again, Dhanji, thank you very very much!
Best regards,
Jean-Philippe.
On Jul 7, 9:01 am, "Dhanji R. Prasanna" <[email protected]> wrote:
> Does Jetty install the DefaultServlet without web.xml (I don't think so)?
> If it doesn't you have to apply GuiceFilter specifically to GuiceServlet.
>
> Dhanji.
>
> On Tue, Jul 7, 2009 at 10:28 PM, Jean-Philippe Couture
> <[email protected]>wrote:
>
>
>
>
>
> > Hi,
>
> > I have an issue when trying to use Google Guice 2.0 (and the Guice-
> > Servlet 2.0 extension) with Jetty 7.0.0M4. Basically, I want to let
> > Guice handle the servlet injection, but for some reason, it doesn't
> > seem to work.
>
> > I tried using a WebApp instead (along with a web.xml descriptor file),
> > with the same setup and it works as expected. I would prefer to avoid
> > depending on an external file to setup my Jetty Server.
>
> > Upon startup, the message console shows:
>
> > GuiceServletConfig.contextInitialized
> > GuiceServlet.init
>
> > So it appears to set everything up as it should. Although, when I
> > browse tohttp://localhost:8080/index.html, I am shown a 404 - Not
> > Found error.
>
> > I would really appreciate if you could give me a hint or two on how to
> > properly set things up. What I want to achieve, in a nutshell, is the
> > same result as with the web.xml descriptor file, without it.
> > (Everything done programmatically...)
>
> > Best regards,
>
> > Jean-Philippe Couture, [email protected]
>
> > Here is a listing of the classes I use. And the web.xml descriptor
> > file I used when it worked.
>
> > GuiceServletConfig.java:
> > ------------------------
> > public class GuiceServletConfig extends GuiceServletContextListener {
> > �...@override
> > public void contextInitialized(ServletContextEvent
> > servletContextEvent) {
> > super.contextInitialized(servletContextEvent);
> > System.out.println("GuiceServletConfig.contextInitialized");
> > }
>
> > �...@override
> > public void contextDestroyed(ServletContextEvent
> > servletContextEvent)
> > {
> > super.contextDestroyed(servletContextEvent);
> > System.out.println("GuiceServletConfig.contextDestroyed");
> > }
>
> > �...@override
> > protected Injector getInjector() {
> > return Guice.createInjector(new ServletModule() {
> > �...@override
> > protected void configureServlets() {
> > super.configureServlets();
>
> > this.serve("*.html").with(GuiceServlet.class);
> > }
> > });
> > }
> > }
>
> > GuiceServlet.java:
> > ------------------
> > @Singleton
> > public class GuiceServlet extends HttpServlet {
> > �...@override
> > public void init() throws ServletException {
> > super.init();
> > System.out.println("GuiceServlet.init");
> > }
>
> > �...@override
> > protected void doGet(HttpServletRequest request, HttpServletResponse
> > response) throws ServletException, IOException {
> > System.out.println("GuiceServlet.doGet");
> > }
> > }
>
> > Main.java:
> > ----------
> > public class Main {
> > public static void main(String... args) {
> > Server server = new Server();
> > Connector connector = new SelectChannelConnector();
> > connector.setHost("0.0.0.0");
> > connector.setPort(8080);
> > server.setConnectors(new Connector[]{connector});
>
> > ServletContextHandler context = new
> > ServletContextHandler(server,
> > "/", ServletContextHandler.SESSIONS);
> > context.addEventListener(new GuiceServletConfig());
> > context.addFilter(GuiceFilter.class, "/*", 0);
>
> > try {
> > server.start();
> > server.join();
> > } catch (Exception e) {
> > e.printStackTrace();
> > }
> > }
> > }
>
> > web.xml:
> > --------
> > <?xml version="1.0" encoding="UTF-8"?>
> > <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://
> >www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://
> > java.sun.com/xml/ns/javaee
> >http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
> > version="2.5">
> > <filter>
> > <filter-name>guiceFilter</filter-name>
>
> > <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
> > </filter>
>
> > <filter-mapping>
> > <filter-name>guiceFilter</filter-name>
> > <url-pattern>/*</url-pattern>
> > </filter-mapping>
>
> > <listener>
> > <listener-class>jettyguice.GuiceServletConfig</listener-class>
> > </listener>
> > </web-app>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---