javax.servlet.Servlet.init()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(184)

本文整理了Java中javax.servlet.Servlet.init()方法的一些代码示例,展示了Servlet.init()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Servlet.init()方法的具体详情如下:
包路径:javax.servlet.Servlet
类名称:Servlet
方法名:init

Servlet.init介绍

[英]Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.

The servlet container cannot place the servlet into service if the init method

  1. Throws a ServletException
  2. Does not return within a time period defined by the Web server
    [中]由servlet容器调用,以向servlet指示servlet正在投入服务。
    servlet容器在实例化servlet之后只调用init方法一次。init方法必须成功完成,servlet才能接收任何请求。
    如果init方法
    1.抛出ServletException
    1.在Web服务器定义的时间段内不返回

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Initialize the wrapped Servlet instance.
 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
 */
@Override
public void afterPropertiesSet() throws Exception {
  if (this.servletClass == null) {
    throw new IllegalArgumentException("'servletClass' is required");
  }
  if (this.servletName == null) {
    this.servletName = this.beanName;
  }
  this.servletInstance = ReflectionUtils.accessibleConstructor(this.servletClass).newInstance();
  this.servletInstance.init(new DelegatingServletConfig());
}

代码示例来源:origin: jersey/jersey

@Override
public void init(ServletConfig config) throws ServletException {
  wrappedServlet.init(new ServletConfigWrapper(config));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  if (bean instanceof Servlet) {
    ServletConfig config = this.servletConfig;
    if (config == null || !this.useSharedServletConfig) {
      config = new DelegatingServletConfig(beanName, this.servletContext);
    }
    try {
      ((Servlet) bean).init(config);
    }
    catch (ServletException ex) {
      throw new BeanInitializationException("Servlet.init threw exception", ex);
    }
  }
  return bean;
}

代码示例来源:origin: org.springframework/spring-webmvc

/**
 * Initialize the wrapped Servlet instance.
 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
 */
@Override
public void afterPropertiesSet() throws Exception {
  if (this.servletClass == null) {
    throw new IllegalArgumentException("'servletClass' is required");
  }
  if (this.servletName == null) {
    this.servletName = this.beanName;
  }
  this.servletInstance = ReflectionUtils.accessibleConstructor(this.servletClass).newInstance();
  this.servletInstance.init(new DelegatingServletConfig());
}

代码示例来源:origin: Atmosphere/atmosphere

/**
 * Initialize the {@link Filter}
 *
 * @throws javax.servlet.ServletException
 */
public void init() throws ServletException {
  for (FilterConfigImpl f : filters) {
    if (f != null) {
      f.getFilter().init(f);
    }
  }
  if (servlet != null) {
    servlet.init(configImpl);
  }
}

代码示例来源:origin: org.springframework/spring-webmvc

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  if (bean instanceof Servlet) {
    ServletConfig config = this.servletConfig;
    if (config == null || !this.useSharedServletConfig) {
      config = new DelegatingServletConfig(beanName, this.servletContext);
    }
    try {
      ((Servlet) bean).init(config);
    }
    catch (ServletException ex) {
      throw new BeanInitializationException("Servlet.init threw exception", ex);
    }
  }
  return bean;
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

@Override
protected void doPortalInit() throws Exception {
  ServletContext servletContext = servletConfig.getServletContext();
  ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
    PluginContextListener.PLUGIN_CLASS_LOADER);
  String servletClass = servletConfig.getInitParameter("servlet-class");
  servlet = (Servlet)InstanceFactory.newInstance(
    classLoader, servletClass);
  servlet.init(servletConfig);
}

代码示例来源:origin: org.apache.cxf/cxf-rt-frontend-jaxws

public void setServletConfig(ServletConfig servletConfig) {
  try {
    shadowCxfServlet = (Servlet)servletClass.newInstance();
  } catch (InstantiationException | IllegalAccessException e) {
    throw new RuntimeException(e);
  }
  try {
    shadowCxfServlet.init(servletConfig);
  } catch (ServletException ex) {
    throw new RuntimeException(ex.getMessage(), ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

servlet.init(new MockServletConfig(servletContext, "myHandler"));

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

@Override
protected void doPortalInit() throws Exception {
  ServletContext servletContext = servletConfig.getServletContext();
  ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
    PluginContextListener.PLUGIN_CLASS_LOADER);
  String servletClass = servletConfig.getInitParameter("servlet-class");
  _subcontext = servletConfig.getInitParameter("sub-context");
  if (_subcontext == null) {
    _subcontext = getServletName();
  }
  servlet = (Servlet)InstanceFactory.newInstance(
    classLoader, servletClass);
  if (!(servlet instanceof HttpServlet)) {
    throw new IllegalArgumentException(
      "servlet-class is not an instance of " +
        HttpServlet.class.getName());
  }
  servlet.init(servletConfig);
  PortalDelegatorServlet.addDelegate(_subcontext, (HttpServlet)servlet);
}

代码示例来源:origin: pentaho/mondrian

private static Servlet getServlet(
  String cbClassName,
  String dataSourceText,
  Map<List<String>, Servlet> cache)
  throws ServletException
{
  final List<String> key =
    Collections.singletonList(
      dataSourceText);
  Servlet servlet = cache.get(key);
  if (servlet != null) {
    return servlet;
  }
  MockServletContext servletContext = new MockServletContext();
  MockServletConfig servletConfig = new MockServletConfig(servletContext);
  servletConfig.addInitParameter(
    XmlaServlet.PARAM_CALLBACKS, cbClassName);
  servletConfig.addInitParameter(
    XmlaServlet.PARAM_CHAR_ENCODING, "UTF-8");
  servletConfig.addInitParameter(
    XmlaServlet.PARAM_DATASOURCES_CONFIG,
    "inline:" + dataSourceText);
  servlet = new MondrianXmlaServlet();
  servlet.init(servletConfig);
  if (cache != null) {
    cache.put(key, servlet);
  }
  return servlet;
}

代码示例来源:origin: com.atlassian.jira/jira-core

@Override
public void init(ServletConfig config) throws ServletException
{
  displayChart.init(config);
}

代码示例来源:origin: apache/cxf

public void setServletConfig(ServletConfig servletConfig) {
  try {
    shadowCxfServlet = (Servlet)servletClass.newInstance();
  } catch (InstantiationException | IllegalAccessException e) {
    throw new RuntimeException(e);
  }
  try {
    shadowCxfServlet.init(servletConfig);
  } catch (ServletException ex) {
    throw new RuntimeException(ex.getMessage(), ex);
  }
}

代码示例来源:origin: apache/felix

@Override
public void init(final ServletConfig config) throws ServletException
{
  super.init(config);
  dispatcherServlet.init(config);
  getHttpServiceController().register(config.getServletContext(), serviceRegProps);
}

代码示例来源:origin: io.github.factoryfx/jettyFactory

public void update(Servlet servlet, Collection<BasicRequestHandler> basicRequestHandlers){
  try {
    servlet.init(this.servlet.getServletConfig());
  } catch (ServletException e) {
    throw new RuntimeException(e);
  }
  this.servlet=servlet;
  this.basicRequestHandler = basicRequestHandlers==null? Collections.emptyList():new ArrayList<>(basicRequestHandlers);
}

代码示例来源:origin: httpunit/httpunit

synchronized Servlet getServlet() throws ClassNotFoundException, InstantiationException, IllegalAccessException, ServletException {
  if (_servlet == null) {
    Class servletClass = Class.forName( getClassName() );
    _servlet = (Servlet) servletClass.newInstance();
    String servletName = _servletName != null ? _servletName : _servlet.getClass().getName();
    _servlet.init( new ServletUnitServletConfig( servletName, WebApplication.this, getInitParams() ) );
  }
  return _servlet;
}

代码示例来源:origin: org.kohsuke.httpunit/httpunit

synchronized Servlet getServlet() throws ClassNotFoundException, InstantiationException, IllegalAccessException, ServletException {
  if (_servlet == null) {
    Class servletClass = Class.forName( getClassName() );
    _servlet = (Servlet) servletClass.newInstance();
    String servletName = _servletName != null ? _servletName : _servlet.getClass().getName();
    _servlet.init( new ServletUnitServletConfig( servletName, WebApplication.this, getInitParams() ) );
  }
  return _servlet;
}

代码示例来源:origin: javaee/grizzly

/**
 * Starts {@link Servlet} instance of this {@link OSGiServletHandler}.
 *
 * @throws ServletException If {@link Servlet} startup failed.
 */
public void startServlet() throws ServletException {
  configureServletEnv();
  servletInstance.init(getServletConfig());
}

代码示例来源:origin: stackoverflow.com

String jspClassName = findJspClassForJSP("your.jsp");
Class jspClass = Class.forName(jspClassName);
Servlet jspServlet = (Servlet)jspClass.newInstance();
MyServletRequest req = new MyServletRequest();
MyServletResponse resp = new MyServletResponse();
jspServlet.init();
jspServlet.service(req, resp);
jspServlet.destroy();
String results = reps.getContent();

代码示例来源:origin: OpenNMS/opennms

@Override
public void init()
  throws ServletException
{
  String name = "servlet_" + getId();
  ServletConfig config = new ServletConfigImpl(name, getContext(), getInitParams());
  this.servlet.init(config);
}

相关文章

微信公众号

最新文章

更多