Java Code Examples for javax.servlet.ServletRegistration#Dynamic

The following examples show how to use javax.servlet.ServletRegistration#Dynamic . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebAppInitializer.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) {
    servletContext.setInitParameter("applicationName", APPLICATION_NAME);
    System.setProperty("applicationName", APPLICATION_NAME);
    ProfileUtil.setActiveProfile(servletContext);
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setDisplayName(APPLICATION_NAME);
    rootContext.register(ApplicationConfiguration.class);
    servletContext.addListener(new ContextLoaderListener(rootContext));

    // MVC Context
    AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
    mvcContext.register(WebMvcAppContext.class);

    ServletRegistration.Dynamic restServlet = servletContext.addServlet("restServlet", new DispatcherServlet(mvcContext));
    restServlet.setLoadOnStartup(1);
    restServlet.addMapping("/*");
}
 
Example 2
Source File: MainWebAppInitializer.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public void onStartup(final ServletContext sc) throws ServletException {

    // Create the 'root' Spring application context
    final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
    root.register(WebConfig.class);

    // Manages the lifecycle of the root application context
    sc.addListener(new ContextLoaderListener(root));

    // Handles requests into the application
    final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
    appServlet.setLoadOnStartup(1);

    final Set<String> mappingConflicts = appServlet.addMapping("/");
    if (!mappingConflicts.isEmpty()) {
        throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
    }
}
 
Example 3
Source File: MyWebAppInitializer.java    From spring-rest-server with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void addServiceDispatcherServlet(ServletContext container, AnnotationConfigWebApplicationContext rootContext) {
    final String SERVICES_MAPPING = "/";

    ServletRegistration.Dynamic dispatcher = container.addServlet("servicesDispatcher", new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    Set<String> mappingConflicts = dispatcher.addMapping(SERVICES_MAPPING);

    if (!mappingConflicts.isEmpty()) {
        for (String s : mappingConflicts) {
            LOG.error("Mapping conflict: " + s);
        }
        throw new IllegalStateException("'ServicesDispatcher' could not be mapped to '" + SERVICES_MAPPING + "'");
    }
}
 
Example 4
Source File: TestStandardWrapper.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx)
        throws ServletException {
    Servlet s;

    if (createServlet) {
        s = ctx.createServlet(servlet.getClass());
    } else {
        s = servlet;
    }
    ServletRegistration.Dynamic r = ctx.addServlet("servlet", s);
    r.addMapping("/");
}
 
Example 5
Source File: ServletContextImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public ServletRegistration.Dynamic addJspFile(String servletName, String jspFile) {
    if(servletName == null || servletName.isEmpty()) {
        throw UndertowServletMessages.MESSAGES.paramCannotBeNull("servletName");
    }
    return addServlet(servletName, "org.apache.jasper.servlet.JspServlet", Collections.singletonList(handler -> exchange -> {
        ServletRequest request = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletRequest();
        request.setAttribute(System.getProperty("org.apache.jasper.Constants.JSP_FILE", "org.apache.catalina.jsp_file"), jspFile);
        handler.handleRequest(exchange);
    }));
}
 
Example 6
Source File: ApplicationContextFacade.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(String servletName,
        Class<? extends Servlet> servletClass) {
    if (SecurityUtil.isPackageProtectionEnabled()) {
        return (ServletRegistration.Dynamic) doPrivileged("addServlet",
                new Class[]{String.class, Class.class},
                new Object[]{servletName, servletClass});
    } else {
        return context.addServlet(servletName, servletClass);
    }
}
 
Example 7
Source File: TesterServletContainerInitializer2.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx)
        throws ServletException {
    Servlet s = new TesterServlet();
    ServletRegistration.Dynamic r = ctx.addServlet("TesterServlet2", s);
    r.addMapping("/TesterServlet2");
}
 
Example 8
Source File: SpringWebInitializer.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
private void addDispatcherContext(ServletContext container) {
  // Create the dispatcher servlet's Spring application context
  AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
  dispatcherContext.register(SpringDispatcherConfig.class); 
	 
  // Declare  <servlet> and <servlet-mapping> for the DispatcherServlet
  ServletRegistration.Dynamic dispatcher = container.addServlet("ch02-servlet", 
  		new DispatcherServlet(dispatcherContext));
  dispatcher.addMapping("*.html");
  dispatcher.setLoadOnStartup(1);
}
 
Example 9
Source File: StudentControllerConfig.java    From tutorials with MIT License 5 votes vote down vote up
public void onStartup(ServletContext sc) throws ServletException {
    AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
    root.register(WebConfig.class);
    root.setServletContext(sc);
    sc.addListener(new ContextLoaderListener(root));

    DispatcherServlet dv = new DispatcherServlet(root);
    
    ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv);
    appServlet.setLoadOnStartup(1);
    appServlet.addMapping("/test/*");
}
 
Example 10
Source File: ApplicationContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private ServletRegistration.Dynamic addServlet(String servletName, String servletClass,
        Servlet servlet, Map<String,String> initParams) throws IllegalStateException {

    if (servletName == null || servletName.equals("")) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.invalidServletName", servletName));
    }

    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        //TODO Spec breaking enhancement to ignore this restriction
        throw new IllegalStateException(
                sm.getString("applicationContext.addServlet.ise",
                        getContextPath()));
    }

    Wrapper wrapper = (Wrapper) context.findChild(servletName);

    // Assume a 'complete' ServletRegistration is one that has a class and
    // a name
    if (wrapper == null) {
        wrapper = context.createWrapper();
        wrapper.setName(servletName);
        context.addChild(wrapper);
    } else {
        if (wrapper.getName() != null &&
                wrapper.getServletClass() != null) {
            if (wrapper.isOverridable()) {
                wrapper.setOverridable(false);
            } else {
                return null;
            }
        }
    }

    ServletSecurity annotation = null;
    if (servlet == null) {
        wrapper.setServletClass(servletClass);
        Class<?> clazz = Introspection.loadClass(context, servletClass);
        if (clazz != null) {
            annotation = clazz.getAnnotation(ServletSecurity.class);
        }
    } else {
        wrapper.setServletClass(servlet.getClass().getName());
        wrapper.setServlet(servlet);
        if (context.wasCreatedDynamicServlet(servlet)) {
            annotation = servlet.getClass().getAnnotation(ServletSecurity.class);
        }
    }

    if (initParams != null) {
        for (Map.Entry<String, String> initParam: initParams.entrySet()) {
            wrapper.addInitParameter(initParam.getKey(), initParam.getValue());
        }
    }

    ServletRegistration.Dynamic registration =
            new ApplicationServletRegistration(wrapper, context);
    if (annotation != null) {
        registration.setServletSecurity(new ServletSecurityElement(annotation));
    }
    return registration;
}
 
Example 11
Source File: MockServletContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(String servletName, String className) {
	throw new UnsupportedOperationException();
}
 
Example 12
Source File: MockServletContext.java    From knox with Apache License 2.0 4 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet( String s, String s1 ) {
  return null;
}
 
Example 13
Source File: MockServletContext.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) {
	throw new UnsupportedOperationException();
}
 
Example 14
Source File: ThreadLocalServletContext.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(final String servletName, final String className) throws IllegalArgumentException, IllegalStateException {
    return get().addServlet(servletName, className);
}
 
Example 15
Source File: MockServletContext.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) {
    throw new UnsupportedOperationException("not supported");
}
 
Example 16
Source File: StartupContext.java    From vertx-vaadin with MIT License 4 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) {
    return null;
}
 
Example 17
Source File: MockServletContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) {
	throw new UnsupportedOperationException();
}
 
Example 18
Source File: ServletContextImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public ServletRegistration.Dynamic addServlet(final String servletName, final String className) {
    return addServlet(servletName, className, Collections.emptyList());
}
 
Example 19
Source File: WebInitializer.java    From tutorials with MIT License 3 votes vote down vote up
public void onStartup(ServletContext container) throws ServletException {

		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.register(ApplicationConfiguration.class);
		ctx.setServletContext(container);

		// Manage the lifecycle of the root application context
		container.addListener(new ContextLoaderListener(ctx));

		ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));

		servlet.setLoadOnStartup(1);
		servlet.addMapping("/");

	}
 
Example 20
Source File: Context.java    From Tomcat8-Source-Read with MIT License 2 votes vote down vote up
/**
 * Notification that Servlet security has been dynamically set in a
 * {@link javax.servlet.ServletRegistration.Dynamic}
 * @param registration Servlet security was modified for
 * @param servletSecurityElement new security constraints for this Servlet
 * @return urls currently mapped to this registration that are already
 *         present in web.xml
 */
Set<String> addServletSecurity(ServletRegistration.Dynamic registration,
        ServletSecurityElement servletSecurityElement);