All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.directwebremoting.servlet.DwrServlet Maven / Gradle / Ivy

Go to download

DWR is easy Ajax for Java. It makes it simple to call Java code directly from Javascript. It gets rid of almost all the boilerplate code between the web browser and your Java code. This version 4.0.2 works with Jakarta Servlet 4.0.2.

There is a newer version: 5.0.0
Show newest version
package org.directwebremoting.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.Container;
import org.directwebremoting.WebContextFactory.WebContextBuilder;
import org.directwebremoting.impl.StartupUtil;

/**
 * This is the main servlet that handles all the requests to DWR.
 * 

It is on the large side because it can't use technologies like JSPs etc * since it all needs to be deployed in a single jar file, and while it might be * possible to integrate Velocity or similar I think simplicity is more * important, and there are only 2 real pages both script heavy in this servlet * anyway.

*

There are 5 things to do, in the order that you come across them:

*
    *
  • The index test page that points at the classes
  • *
  • The class test page that lets you execute methods
  • *
  • The interface javascript that uses the engine to send requests
  • *
  • The engine javascript to form the iframe request and process replies
  • *
  • The exec 'page' that executes the method and returns data to the iframe
  • *
* @author Joe Walker [joe at getahead dot ltd dot uk] */ public class DwrServlet extends HttpServlet { @Override public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); try { StartupUtil.logStartup(getClass().getSimpleName(), servletConfig); container = createContainer(servletConfig); webContextBuilder = container.getBean(WebContextBuilder.class); if (webContextBuilder != null) { webContextBuilder.engageThread(container, null, null); } configureContainer(container, servletConfig); } catch (ServletException ex) { throw ex; } catch (Exception ex) { log.fatal("init failed", ex); throw new ServletException(ex); } finally { if (webContextBuilder != null) { webContextBuilder.disengageThread(); } } } /** * Specializations of DwrServlet might have an alternate implementation * of Container. This allows subclasses to override the implementation * method. * Part of {@link #init(ServletConfig)}. * @param servletConfig ... * @return ... * @throws ServletException Children might need to throw even if we don't */ protected Container createContainer(ServletConfig servletConfig) throws ServletException { return StartupUtil.createAndSetupDefaultContainer(servletConfig); } /** * Specializations of DwrServlet might want to configure it differently * from the default * Part of {@link #init(ServletConfig)}. * @param defaultContainer ... * @param servletConfig ... * @throws ServletException ... * @throws IOException ... */ protected void configureContainer(Container defaultContainer, ServletConfig servletConfig) throws ServletException, IOException { try { StartupUtil.configureContainerFully(defaultContainer, servletConfig); } catch (IOException ex) { throw ex; } catch (Exception ex) { throw new ServletException(ex); } } @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { doPost(req, resp); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { webContextBuilder.engageThread(container, request, response); UrlProcessor processor = container.getBean(UrlProcessor.class); processor.handle(request, response); } finally { webContextBuilder.disengageThread(); } } @Override public void destroy() { webContextBuilder = container.getBean(WebContextBuilder.class); if (webContextBuilder != null) { webContextBuilder.engageThread(container, null, null); } container.destroy(); if (webContextBuilder != null) { webContextBuilder.disengageThread(); } super.destroy(); } /** * Accessor for the IoC container. * @return ... */ public Container getContainer() { return container; } /** * Our IoC container */ private Container container = null; /** * The WebContext that keeps http objects local to a thread */ protected WebContextBuilder webContextBuilder = null; /** * The log stream */ private static final Log log = LogFactory.getLog(DwrServlet.class); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy