Servlet 与 Spring(转摘)

Servlet 与 Spring(转摘)

1. web.xml中还是

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/conf/spring/**/*-context.xml</param-value>
 </context-param>

2. portal-context.xml中的一段

<bean
      id="customerInfoServlet"
      class="chances.card.portal.customer.CustomerInfoServlet"
  >

    <property name="infoPage">

      <value>${portal.card.info.index}</value>
    </property>
    <property name="customerLoginUrl">

      <value>${portal.card.loginUrl}</value>
    </property>
    <property name="cardSearcher">

      <ref bean="cardSearcher"/>
    </property>
    <property name="cardManager">

      <ref bean="cardManager"/>
    </property>

  </bean>

3. web.xml 中的一段配置

<servlet>
  <servlet-name>customerLogin</servlet-name>
  <servlet-class>
   chances.card.commons.servlet.proxy.SpringServletProxy
  </servlet-class>
  <init-param>
   <param-name>beanName</param-name>
   <param-value>customerLoginServlet</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>customerLogin</servlet-name>
  <url-pattern>/customerLogin</url-pattern>
 </servlet-mapping>

----------------------------最早的servlet 的配置方法

<servlet>
<servlet-name>Test</servlet-name>
<display-name>Test</display-name>
<description>A test Servlet</description>
<servlet-class>test.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>

4. SpringServletProxy .java 类的代码

/**
 * Created on 2007-08-16
 *
 * $Id $
 * $Source $
 * $Revision $
 */
package chances.card.commons.servlet.proxy;

import java.io.IOException;

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

import chances.commons.web.SpringWebUtils;

/**
 * Servlet代理
 *
 * @author <a href="mailto:[email protected]">Alpha</a>
 * @version 1.0
 *
 */
public class SpringServletProxy extends HttpServlet {

    private static final long serialVersionUID = -5712473045972662420L;

    Servlet target;

    /**
     * (non-javadoc)
     *
     * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
            throws ServletException, IOException {
        target.service(arg0, arg1);
    }

    /**
     * (non-javadoc)
     *
     * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        String name = config.getInitParameter("beanName");
        Object obj = SpringWebUtils.getBean(config.getServletContext(), name);
        if (obj instanceof Servlet) {
            target = (Servlet) obj;
        } else {
            throw new IllegalArgumentException(name);
        }
        target.init(config);
    }

    /**
     * (non-javadoc)
     *
     * @see javax.servlet.GenericServlet#destroy()
     */
    public void destroy() {
        super.destroy();
        target.destroy();
    }
}

你可能感兴趣的:(Servlet 与 Spring(转摘))