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

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

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

Servlet.service介绍

[英]Called by the servlet container to allow the servlet to respond to a request.

This method is only called after the servlet's init() method has completed successfully.

The status code of the response always should be set for a servlet that throws or sends an error.

Servlets typically run inside multithreaded servlet containers that can handle multiple requests concurrently. Developers must be aware to synchronize access to any shared resources such as files, network connections, and as well as the servlet's class and instance variables. More information on multithreaded programming in Java is available in the Java tutorial on multi-threaded programming.
[中]由servlet容器调用,以允许servlet响应请求。
只有在servlet的init()方法成功完成后,才会调用此方法。
对于抛出或发送错误的servlet,应始终设置响应的状态代码。
servlet通常在多线程servlet容器中运行,这些容器可以同时处理多个请求。开发人员必须知道同步访问任何共享资源,例如文件、网络连接以及servlet的类和实例变量。有关Java多线程编程的更多信息,请参见{$0$}。

代码示例

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

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  this.delegateServlet.service(request, response);
}

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

@Override
@Nullable
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
  ((Servlet) handler).service(request, response);
  return null;
}

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

/**
 * Invoke the wrapped Servlet instance.
 * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
  Assert.state(this.servletInstance != null, "No Servlet instance");
  this.servletInstance.service(request, response);
  return null;
}

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

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  this.delegateServlet.service(request, response);
}

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

/**
 * Pass the call on to the Filter/Servlet.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
  if (this.filter != null) {
    this.filter.doFilter(request, response, this.nextFilterChain);
  }
  else {
    Assert.state(this.servlet != null, "Neither a Filter not a Servlet set");
    this.servlet.service(request, response);
  }
}

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

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
  wrappedServlet.service(req, res);
}

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

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  this.delegateServlet.service(request, response);
}

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

@Override
@Nullable
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
  ((Servlet) handler).service(request, response);
  return null;
}

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

/**
 * Invoke the wrapped Servlet instance.
 * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
  Assert.state(this.servletInstance != null, "No Servlet instance");
  this.servletInstance.service(request, response);
  return null;
}

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

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  this.invoked = true;
  if (this.servlet != null) {
    this.servlet.service(request, response);
  }
  else {
    chain.doFilter(request, response);
  }
}

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

/**
 * Pass the call on to the Filter/Servlet.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
  if (this.filter != null) {
    this.filter.doFilter(request, response, this.nextFilterChain);
  }
  else {
    Assert.state(this.servlet != null, "Neither a Filter not a Servlet set");
    this.servlet.service(request, response);
  }
}

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

servlet.service(request, response);
} else {
  RequestDispatcher rd = configImpl.getServletContext().getNamedDispatcher("default");

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

@Override
  public void handle(Request baseRequest,
            ServletRequest request,
            ServletResponse response) throws ServletException, IOException {
    final boolean asyncSupported = baseRequest.isAsyncSupported();
    if (!isAsyncSupported()) {
      baseRequest.setAsyncSupported(false, null);
    }
    try {
      servlet.service(request, response);
    } catch (EofException ignored) {
      // Want to ignore the EofException as this signifies the client has disconnected or the
      // response has already been written. The problem with using an ExceptionMapper is that
      // we don't actually want to write a response given that the connection has already been
      // closed
    } finally {
      baseRequest.setAsyncSupported(asyncSupported, null);
    }
  }
}

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

@Test
public void doFilterWithServlet() throws Exception {
  Servlet servlet = mock(Servlet.class);
  MockFilterChain chain = new MockFilterChain(servlet);
  chain.doFilter(this.request, this.response);
  verify(servlet).service(this.request, this.response);
  try {
    chain.doFilter(this.request, this.response);
    fail("Expected Exception");
  }
  catch (IllegalStateException ex) {
    assertEquals("This FilterChain has already been called!", ex.getMessage());
  }
}

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

@Override
public void service(
    ServletRequest servletRequest, ServletResponse servletResponse)
  throws IOException, ServletException {
  servlet.service(servletRequest, servletResponse);
}

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

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    controller.service(request, response);
    return null;
  }
}

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

@Test
public void testProfileServlet() throws Exception {
  Servlet servlet = new DispatcherServlet();
  final CallStackElement total = Profiler.activateProfiling("total");
  servlet.service(new MockHttpServletRequest(), new MockHttpServletResponse());
  Profiler.stop();
  final CallStackElement serviceCall = total.getChildren().iterator().next();
  assertEquals("FrameworkServlet#service", serviceCall.getShortSignature());
}

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

servlet.init(new MockServletConfig(servletContext, "myHandler"));
servlet.service(request, response);
assertEquals("myResponse", response.getContentAsString());
  servlet.service(request, response);
  fail("Should have thrown ServletException");
  servlet.service(request, response);
  fail("Should have thrown IOException");

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

@Test
public void doFilterWithServletAndFilters() throws Exception {
  Servlet servlet = mock(Servlet.class);
  MockFilter filter2 = new MockFilter(servlet);
  MockFilter filter1 = new MockFilter(null);
  MockFilterChain chain = new MockFilterChain(servlet, filter1, filter2);
  chain.doFilter(this.request, this.response);
  assertTrue(filter1.invoked);
  assertTrue(filter2.invoked);
  verify(servlet).service(this.request, this.response);
  try {
    chain.doFilter(this.request, this.response);
    fail("Expected Exception");
  }
  catch (IllegalStateException ex) {
    assertEquals("This FilterChain has already been called!", ex.getMessage());
  }
}

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

@Test
  public void testDontProfileStagemonitorServlet() throws Exception {
    Servlet servlet = new StagemonitorFileServlet();

    final CallStackElement total = Profiler.activateProfiling("total");
    servlet.service(new MockHttpServletRequest(), new MockHttpServletResponse());
    Profiler.stop();

    assertEquals(0, total.getChildren().size());
  }
}

相关文章

微信公众号

最新文章

更多