org.glassfish.grizzly.servlet.WebappContext.addServlet()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(12.3k)|赞(0)|评价(0)|浏览(91)

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

WebappContext.addServlet介绍

[英]Adds the servlet with the given name and class type to this servlet context.

The registered servlet may be further configured via the returned ServletRegistration object.

If this WebappContext already contains a preliminary ServletRegistration for a servlet with the given servletName, it will be completed (by assigning the name of the given servletClass to it) and returned.
[中]将具有给定名称和类类型的servlet添加到此servlet上下文。
可以通过返回的ServletRegistration对象进一步配置已注册的servlet。
如果这个WebappContext已经包含了一个具有给定servletName的servlet的初步ServletRegistration,那么它将被完成(通过将给定servletClass的名称分配给它)并返回。

代码示例

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

ServletRegistration registration;
if (c != null) {
  registration = context.addServlet(c.getName(), c);
} else {
  registration = context.addServlet(servlet.getClass().getName(), servlet);

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

final ServletRegistration registration;
if (servletInstance != null) {
  registration = context.addServlet(servletInstance.getClass().getName(), servletInstance);
} else {
  registration = context.addServlet(servletClass.getName(), servletClass);

代码示例来源:origin: uber/AthenaX

public WebServer(URI endpoint) throws IOException {
 this.server = GrizzlyServerFactory.createHttpServer(endpoint, new HttpHandler() {
  @Override
  public void service(Request rqst, Response rspns) throws Exception {
   rspns.setStatus(HttpStatus.NOT_FOUND_404.getStatusCode(), "Not found");
   rspns.getWriter().write("404: not found");
  }
 });
 WebappContext context = new WebappContext("WebappContext", BASE_PATH);
 ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
 registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS,
   PackagesResourceConfig.class.getName());
 StringJoiner sj = new StringJoiner(",");
 for (String s : PACKAGES) {
  sj.add(s);
 }
 registration.setInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES, sj.toString());
 registration.addMapping(BASE_PATH);
 context.deploy(server);
}

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

WebappContext webappContext = new WebappContext("myWebappContext");

webappContext.addListener(new ServletContextListener() {
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    sce.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, MY_SERVICE_LOCATOR);
  }
  @Override
  public void contextDestroyed(ServletContextEvent sce) { }
});

ServletRegistration servlet = webappContext.addServlet("myAppplication", new ServletContainer(resourceConfig));
servlet.addMapping("/application/*");

ServletRegistration hello = webappContext.addServlet("myServlet", MyServlet.class);
hello.addMapping("/servlet/*");

HttpServer createHttpServer = GrizzlyHttpServerFactory.createHttpServer(MY_URI, false);
webappContext.deploy(createHttpServer);
createHttpServer.start();

代码示例来源:origin: com.helger/peppol-smp-server-webapp

@Nonnull
private static WebappContext _createContext (final URI u,
                       final Class <? extends Servlet> aServletClass,
                       final Servlet aServlet,
                       final Map <String, String> aInitParams,
                       final Map <String, String> aContextInitParams)
{
 String path = u.getPath ();
 if (path == null)
  throw new IllegalArgumentException ("The URI path, of the URI " + u + ", must be non-null");
 if (path.isEmpty ())
  throw new IllegalArgumentException ("The URI path, of the URI " + u + ", must be present");
 if (path.charAt (0) != '/')
  throw new IllegalArgumentException ("The URI path, of the URI " + u + ". must start with a '/'");
 path = String.format ("/%s", UriComponent.decodePath (u.getPath (), true).get (1).toString ());
 final WebappContext aContext = new WebappContext ("GrizzlyContext", path);
 ServletRegistration aRegistration;
 if (aServletClass != null)
  aRegistration = aContext.addServlet (aServletClass.getName (), aServletClass);
 else
  aRegistration = aContext.addServlet (aServlet.getClass ().getName (), aServlet);
 aRegistration.addMapping ("/*");
 if (aContextInitParams != null)
  for (final Map.Entry <String, String> e : aContextInitParams.entrySet ())
   aContext.setInitParameter (e.getKey (), e.getValue ());
 if (aInitParams != null)
  aRegistration.setInitParameters (aInitParams);
 return aContext;
}

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

private static HttpServer create(URI u, Servlet servlet) throws IOException {

  String path = u.getPath();
  path = String.format("/%s", UriComponent.decodePath(u.getPath(), true)
         .get(1).toString());

  WebappContext context = new WebappContext("GrizzlyContext", path);
  context.addListener(MyListener.class);
  ServletRegistration registration;
  registration = context.addServlet(servlet.getClass().getName(), servlet);
  registration.addMapping("/*");

  HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u);
  context.deploy(server);
  return server;
}

代码示例来源:origin: phax/peppol-smp-server

@Nonnull
private static WebappContext _createContext (final URI u,
                       final Class <? extends Servlet> aServletClass,
                       final Servlet aServlet,
                       final Map <String, String> aInitParams,
                       final Map <String, String> aContextInitParams)
{
 String path = u.getPath ();
 if (path == null)
  throw new IllegalArgumentException ("The URI path, of the URI " + u + ", must be non-null");
 if (path.isEmpty ())
  throw new IllegalArgumentException ("The URI path, of the URI " + u + ", must be present");
 if (path.charAt (0) != '/')
  throw new IllegalArgumentException ("The URI path, of the URI " + u + ". must start with a '/'");
 path = String.format ("/%s", UriComponent.decodePath (u.getPath (), true).get (1).toString ());
 final WebappContext aContext = new WebappContext ("GrizzlyContext", path);
 ServletRegistration aRegistration;
 if (aServletClass != null)
  aRegistration = aContext.addServlet (aServletClass.getName (), aServletClass);
 else
  aRegistration = aContext.addServlet (aServlet.getClass ().getName (), aServlet);
 aRegistration.addMapping ("/*");
 if (aContextInitParams != null)
  for (final Map.Entry <String, String> e : aContextInitParams.entrySet ())
   aContext.setInitParameter (e.getKey (), e.getValue ());
 if (aInitParams != null)
  aRegistration.setInitParameters (aInitParams);
 return aContext;
}

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

HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI());
WebappContext context = new WebappContext("WebappContext", "/api");
ServletRegistration registration = context.addServlet("ServletContainer",
 new ServletContainer(config));
registration.addMapping("/*");
context.deploy(httpServer);

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

@Before
 public void setup() throws Exception {
   if (server == null) {
     System.out.println("Initializing an instance of Grizzly Container ...");
     final ResourceConfig rc = new ResourceConfig(ResourceEndpointIntegrationTest.class, ..., ..., ...); //update
     WebappContext ctx = new WebappContext("IntegrationTestContext");
           //register your listeners from web.xml in here
     ctx.addListener("com.xxx.yyy.XEndpointServletContextListener");
           //register your applicationContext.xml here
     ctx.addContextInitParameter("contextConfigLocation", "classpath:applicationContext.xml");
           //ServletRegistration is needed to load the ResourceConfig rc inside ServletContainer or you will have no 
           //Servlet-based features available 
     ServletRegistration registration = ctx.addServlet("ServletContainer",
         new ServletContainer(rc));
           //Initialize the Grizzly server passing it base URL
     server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));
           //Deploy the server using our custom context
     ctx.deploy(server);
   }
 }

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

WebappContext context = new WebappContext("context");
 ServletRegistration registration = 
     context.addServlet("ServletContainer", ServletContainer.class);
 registration.setInitParameter("com.sun.jersey.config.property.packages",
     "com.sun.jersey.samples.https_grizzly.resource;com.sun.jersey.samples.https_grizzly.auth");
 // add security filter (which handles http basic authentication)
 registration.setInitParameter(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS,
     "com.sun.jersey.samples.https_grizzly.auth.SecurityFilter;com.sun.jersey.api.container.filter.LoggingFilter");
 registration.setInitParameter(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS,
     LoggingFilter.class.getName());
 try {
   webServer = GrizzlyServerFactory.createHttpServer(
       getBaseURI()
   );
   // start Grizzly embedded server //
   System.out.println("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it...");
   context.deploy(webServer);
   webServer.start();
 } catch (Exception ex) {
   System.out.println(ex.getMessage());
 }

代码示例来源:origin: net.krotscheck/kangaroo-common

context.addServlet(name, new ServletContainer(rc));
registration.setInitParameter("swagger.context.id", name);
registration.addMapping(String.format("%s/*", path));

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

WebappContext webappContext = new WebappContext("grizzly web context", "");
 FilterRegistration testFilterReg = webappContext.addFilter("TestFilter", TestFilter.class);
 testFilterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), "/*");
 ServletRegistration servletRegistration = webappContext.addServlet("Jersey", org.glassfish.jersey.servlet.ServletContainer.class);
 servletRegistration.addMapping("/myapp/*");
 servletRegistration.setInitParameter("jersey.config.server.provider.packages", "com.example");
 HttpServer server = HttpServer.createSimpleServer();
 webappContext.deploy(server);
 server.start();

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

final ServletRegistration reg = ctx.addServlet("spring", new SpringServlet());
reg.addMapping("/*");
ctx.addContextInitParameter("contextConfigLocation", "classpath:spring-context.xml");

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

private static String API_PACKAGE = "package where TestRESTService class";

public static final URI BASE_URI = UriBuilder
    .fromUri("http://localhost/")
    .port(8000)
    .build();

private static HttpServer initServer() throws IOException {
  System.out.println("Starting grizzly... " + BASE_URI);

  HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, new HttpHandler() {
    @Override
    public void service(Request rqst, Response rspns) throws Exception {
      rspns.sendError(404);
    }
  });

  // Initialize and register Jersey Servlet
  WebappContext context = new WebappContext("GrizzlyContext", "/");
  ServletRegistration registration = context.addServlet(
      ServletContainer.class.getName(), ServletContainer.class);
  registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS,
      PackagesResourceConfig.class.getName());
  registration.setInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES, API_PACKAGE);
  registration.addMapping("/*");
  context.deploy(httpServer);

  return httpServer;
}

代码示例来源:origin: com.jpmorgan.quorum/jersey-server

final ServletRegistration registration = ctx.addServlet("ServletContainer", new ServletContainer(config));
registration.addMapping("/*");

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

final DefaultServlet s = new DefaultServlet((StaticHttpHandlerBase) h);
final ServletRegistration registration =
    addServlet("DefaultServlet", s);
registration.addMapping("/");
final ServletConfigImpl sConfig =

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

final DefaultServlet s = new DefaultServlet((StaticHttpHandlerBase) h);
final ServletRegistration registration =
    addServlet("DefaultServlet", s);
registration.addMapping("/");
final ServletConfigImpl sConfig =

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

final DefaultServlet s = new DefaultServlet((StaticHttpHandlerBase) h);
final ServletRegistration registration =
    addServlet("DefaultServlet", s);
registration.addMapping("/");
final ServletConfigImpl sConfig =

代码示例来源:origin: org.glassfish.grizzly/grizzly-websockets-server

final DefaultServlet s = new DefaultServlet((StaticHttpHandlerBase) h);
final ServletRegistration registration =
    addServlet("DefaultServlet", s);
registration.addMapping("/");
final ServletConfigImpl sConfig =

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

.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry", (metricsService.getMetricRegistry()));
adminCtx.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.registry", metricsService.getHealthCheck());
final ServletRegistration sgMetrics = adminCtx.addServlet("admin", new AdminServlet());
sgMetrics.addMapping("/admin/*");
adminCtx.deploy(server);

相关文章

WebappContext类方法