javax.servlet.Servlet类的使用及代码示例

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

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

Servlet介绍

[英]Defines methods that all servlets must implement.

A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.

This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:

  1. The servlet is constructed, then initialized with the init method.
  2. Any calls from clients to the service method are handled.
  3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.
[中]定义所有servlet必须实现的方法。
servlet是一个在Web服务器中运行的小型Java程序。Servlet通常通过超文本传输协议HTTP接收和响应来自Web客户端的请求。
为了实现这个接口,您可以编写一个扩展javax.servlet.GenericServlet的通用servlet或一个扩展javax.servlet.http.HttpServlet的HTTP servlet。
该接口定义了初始化servlet、服务请求和从服务器中删除servlet的方法。这些方法称为生命周期方法,按以下顺序调用:
1.构建servlet,然后用init方法初始化。
1.处理客户端对service方法的任何调用。
1.servlet停止服务,然后用destroy方法销毁,然后进行垃圾收集并最终确定。
除了生命周期方法之外,该接口还提供了getServletConfig方法,servlet可以使用该方法获取任何启动信息,以及getServletInfo方法,该方法允许servlet返回关于自身的基本信息,例如作者、版本和版权。

代码示例

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

try {
    filter = filterConfig.getFilter();
    filter.doFilter(request, response, this);
  } catch (IOException e) {
    throw e;
    throw e;
  } catch (Throwable e) {
    throw new ServletException("Throwable", e);
    servlet.service(request, response);
  } else {
    RequestDispatcher rd = configImpl.getServletContext().getNamedDispatcher("default");
    if (rd == null) {
      throw new ServletException("No Servlet Found");
  throw e;
} catch (Throwable e) {
  throw new ServletException("Throwable", e);

代码示例来源: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: spring-projects/spring-framework

@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
  if (bean instanceof Servlet) {
    ((Servlet) bean).destroy();
  }
}

代码示例来源:origin: com.github.rmannibucau/playx-servlet

@Override
public void dispatch(final ServletContext context, final String path) {
  final ServletRequest servletRequest = getRequest();
  if (!HttpServletRequest.class.isInstance(servletRequest)) {
    throw new IllegalStateException("Not a http request: " + servletRequest);
  }
  final HttpServletRequest request = HttpServletRequest.class.cast(servletRequest);
  if (request.getAttribute(ASYNC_REQUEST_URI) == null) {
    request.setAttribute(ASYNC_REQUEST_URI, request.getRequestURI());
    request.setAttribute(ASYNC_CONTEXT_PATH, request.getContextPath());
    request.setAttribute(ASYNC_SERVLET_PATH, request.getServletPath());
    request.setAttribute(ASYNC_PATH_INFO, request.getPathInfo());
    request.setAttribute(ASYNC_QUERY_STRING, request.getQueryString());
  }
  try {
    servlet.getInstance().service(request, response);
  } catch (final ServletException | IOException ioe) {
    onError(ioe);
  }
}

代码示例来源:origin: org.apache.bsf/bsf-utils

public void forward(String relativePath) throws ServletException, IOException {
  ServletContext context =  servlet.getServletConfig().getServletContext();
  String baseURI;
  String requestURI = request.getRequestURI();
  if(relativePath.startsWith("/")){
    baseURI = requestURI.substring(0, request.getContextPath().length());
  }else{
    baseURI = requestURI.substring(0, requestURI.lastIndexOf("/"));
  }
  context.getRequestDispatcher(baseURI+relativePath).forward(request, response);
}

代码示例来源:origin: com.jayway.forest/forest-core

public void init(final ServletConfig servletConfig) throws ServletException {
  final String targetBean = servletConfig.getInitParameter("targetBean");
  final ApplicationContext ctx = getContext(servletConfig.getServletContext());
  if (targetBean == null || !ctx.containsBean(targetBean)) {
    throw new ServletException("targetBean '" + targetBean + "' not found in context.");
  }
  this.delegate = (Servlet) ctx.getBean(targetBean, Servlet.class);
  this.delegate.init(servletConfig);
}

代码示例来源:origin: org.apache.bsf/bsf-utils

public Object getAttribute(String key, Object value, int scope){
  switch (scope) {
    case HttpScriptContext.ENGINE_SCOPE:
      return request.getAttribute(key);
    case HttpScriptContext.SESSION_SCOPE:
      if (useSession()) {
        return request.getSession().getAttribute(key);
      } else {
        return null;
      }
    case HttpScriptContext.APPLICATION_SCOPE:
      return servlet.getServletConfig().getServletContext().getAttribute(key);
    default:
      return null;
  }
}

代码示例来源:origin: at.bestsolution.efxclipse.eclipse/org.eclipse.equinox.http.servlet

public HttpSession getSession() {
  HttpSession session = request.getSession();
  if (session != null) {
    return dispatchTargets.peek().getContextController().getSessionAdaptor(
      session, dispatchTargets.peek().getServletRegistration().getT().getServletConfig().getServletContext());
  }
  return null;
}

代码示例来源:origin: org.eclipse.jetty.aggregate/jetty-all-server

public void init(ServletConfig config) throws ServletException
{
  synchronized(this)
  {
    if(_stack.size()==0)
    {
      try
      {
        Servlet s = newInstance();
        s.init(config);
        _stack.push(s);
      }
      catch (ServletException e)
      {
        throw e;
      }
      catch (Exception e)
      {
        throw new ServletException(e);
      }
    }
  }
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi

private void serviceViaPlugin(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
  // requestPath is the full path minus the JAX-RS prefix (/plugins)
  final String requestPath = req.getServletPath() + req.getPathInfo();
  final Servlet pluginServlet = getPluginServlet(requestPath);
  if (pluginServlet != null) {
    initializeServletIfNeeded(req, pluginServlet);
    final OSGIServletRequestWrapper requestWrapper = new OSGIServletRequestWrapper(req, servletRouter.getPluginPrefixForPath(requestPath));
    pluginServlet.service(requestWrapper, resp);
  } else {
    resp.sendError(404);
  }
}

代码示例来源:origin: org.apache.geronimo.modules/geronimo-webservices

public void init(ServletConfig config) throws ServletException {
  next.init(config);
  try {
    managedService.init(new InstanceContext(config.getServletContext()));
  } catch (ServiceException e) {
    throw new ServletException("Unable to initialize ServiceEndpoint", e);
  }
}

代码示例来源:origin: org.onap.aaf.cadi/cadi-core

public void init(ServletConfig sc) throws ServletException {
  if(delegate == null) throw new ServletException("Invalid Servlet Delegate");
  delegate.init(sc);
}

代码示例来源: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: ru.vyarus/dropwizard-guicey

@Override
  protected void service(final HttpServletRequest req, final HttpServletResponse resp)
      throws ServletException, IOException {
    req.setAttribute(ADMIN_PROPERTY, true);
    restServlet.service(req, resp);
  }
}

代码示例来源:origin: org.wso2.carbon/org.wso2.carbon.component

public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException,
        IOException {
  String pathInfo = request.getPathInfo();
  if (pathInfo != null && pathInfo.startsWith("/WEB-INF/")) { //$NON-NLS-1$
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return;
  }
  ClassLoader original = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(jspLoader);
    jspServlet.service(request, response);
  } finally {
    Thread.currentThread().setContextClassLoader(original);
  }
}

代码示例来源:origin: org.apache.commons/commons-configuration2

/**
 * Create a ServletContextConfiguration using the context of
 * the specified servlet.
 *
 * @param servlet the servlet
 */
public ServletContextConfiguration(final Servlet servlet)
{
  this.context = servlet.getServletConfig().getServletContext();
}

代码示例来源:origin: org.jboss.as/jboss-as-osgi-http

@Override
  public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
      HttpServletRequest httpRequest = (HttpServletRequest) request;
      HttpServletResponse httpResponse = (HttpServletResponse) response;
      if (!httpContext.handleSecurity(httpRequest, httpResponse))
        return;
      Object u = httpRequest.getAttribute(HttpContext.REMOTE_USER);
      String remoteUser = u instanceof String ? (String) u : null;
      Object a = httpRequest.getAttribute(HttpContext.AUTHENTICATION_TYPE);
      String authType = a instanceof String ? (String) a : null;
      if (remoteUser != null || authType != null) {
        request = new SecurityRequestWrapper(remoteUser, authType, httpRequest);
      }
    }
    delegate.service(request, response);
  }
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi

private void initializeServletIfNeeded(final HttpServletRequest req, final Servlet pluginServlet) throws ServletException {
  if (!initializedServlets.contains(pluginServlet)) {
    synchronized (servletsMonitor) {
      if (!initializedServlets.contains(pluginServlet)) {
        final ServletConfig servletConfig = (ServletConfig) req.getAttribute("killbill.osgi.servletConfig");
        if (servletConfig != null) {
          // TODO PIERRE The servlet will never be destroyed!
          pluginServlet.init(servletConfig);
          initializedServlets.add(pluginServlet);
        }
      }
    }
  }
}

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

public void handle(final ServletRequest req, final ServletResponse res)
    throws ServletException, IOException
{
  final Servlet local = this.servlet;
  if ( local != null )
  {
    local.service(req, res);
  }
  else
  {
    throw new ServletException("Servlet has been unregistered");
  }
}

代码示例来源:origin: org.codehaus.fabric3.tomcat/fabric3-tomcat-extension

public Servlet unregisterMapping(String path) throws ServletException {
  Servlet servlet = servlets.remove(path);
  if (servlet == null) {
    throw new ServletException("Servlet not registered: " + path);
  }
  servlet.destroy();
  return servlet;
}

相关文章

微信公众号

最新文章

更多