Java Code Examples for javax.servlet.ServletContext#removeAttribute()

The following examples show how to use javax.servlet.ServletContext#removeAttribute() . 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: ContextLoader.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Close Spring's web application context for the given servlet context.
 * <p>If overriding {@link #loadParentContext(ServletContext)}, you may have
 * to override this method as well.
 * @param servletContext the ServletContext that the WebApplicationContext runs in
 */
public void closeWebApplicationContext(ServletContext servletContext) {
	servletContext.log("Closing Spring root WebApplicationContext");
	try {
		if (this.context instanceof ConfigurableWebApplicationContext) {
			((ConfigurableWebApplicationContext) this.context).close();
		}
	}
	finally {
		ClassLoader ccl = Thread.currentThread().getContextClassLoader();
		if (ccl == ContextLoader.class.getClassLoader()) {
			currentContext = null;
		}
		else if (ccl != null) {
			currentContextPerThread.remove(ccl);
		}
		servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	}
}
 
Example 2
Source File: EurekaStore.java    From qconfig with MIT License 6 votes vote down vote up
/**
 * Handles Eureka cleanup, including shutting down all monitors and yielding all EIPs.
 *
 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
 */
@PreDestroy
public void destroy() {
    try {
        logger.info("{} Shutting down Eureka Server..", new Date().toString());
        ServletContext sc = context.getServletContext();
        sc.removeAttribute(EurekaServerContext.class.getName());

        DiscoveryManager.getInstance().shutdownComponent();
        destroyEurekaServerContext();
        destroyEurekaEnvironment();

    } catch (Throwable e) {
        logger.error("Error shutting down eureka", e);
    }
    logger.info("{} Eureka Service is now shutdown...", new Date().toString());
}
 
Example 3
Source File: PortalStartupListener.java    From portals-pluto with Apache License 2.0 6 votes vote down vote up
/**
 * Destroyes the portal admin config and removes it from servlet context.
 *
 * @param servletContext the servlet context.
 */
private void destroyAdminConfiguration(ServletContext servletContext)
{
    AdminConfiguration adminConfig = (AdminConfiguration)
            servletContext.getAttribute(ADMIN_CONFIG_KEY);
    if (adminConfig != null)
    {
        try
        {
            adminConfig.destroy();
            if (LOG.isInfoEnabled())
            {
                LOG.info("Pluto Portal Admin Config destroyed.");
            }
        } catch (DriverConfigurationException ex)
        {
            LOG.error("Unable to destroy portal admin config: "
                    + ex.getMessage(), ex);
        } finally
        {
            servletContext.removeAttribute(ADMIN_CONFIG_KEY);
        }
    }
}
 
Example 4
Source File: ContextLoader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Close Spring's web application context for the given servlet context. If
 * the default {@link #loadParentContext(ServletContext)} implementation,
 * which uses ContextSingletonBeanFactoryLocator, has loaded any shared
 * parent context, release one reference to that shared parent context.
 * <p>If overriding {@link #loadParentContext(ServletContext)}, you may have
 * to override this method as well.
 * @param servletContext the ServletContext that the WebApplicationContext runs in
 */
public void closeWebApplicationContext(ServletContext servletContext) {
	servletContext.log("Closing Spring root WebApplicationContext");
	try {
		if (this.context instanceof ConfigurableWebApplicationContext) {
			((ConfigurableWebApplicationContext) this.context).close();
		}
	}
	finally {
		ClassLoader ccl = Thread.currentThread().getContextClassLoader();
		if (ccl == ContextLoader.class.getClassLoader()) {
			currentContext = null;
		}
		else if (ccl != null) {
			currentContextPerThread.remove(ccl);
		}
		servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		if (this.parentContextRef != null) {
			this.parentContextRef.release();
		}
	}
}
 
Example 5
Source File: UserServlet.java    From mytwitter with Apache License 2.0 6 votes vote down vote up
private void toExit(HttpServletRequest request, HttpServletResponse response) throws IOException {
	HttpSession session = request.getSession();
	if (session.getAttribute("admin") == null) {
		session.invalidate();
		response.sendRedirect("index.jsp");
		return;
	}
	Users user = (Users) session.getAttribute("user");
	ServletContext application = session.getServletContext();
	application.removeAttribute(((Users) session.getAttribute("user")).getUname());
	Integer onlineNum = (Integer) application.getAttribute("onlineNum");
	if (onlineNum > 0) {
		application.setAttribute("onlineNum", onlineNum - 1);
	}
	Object signinid = session.getAttribute("signinid");
	int uid = user.getUid();
	Timestamp sdtime = Times.getSystemTime();
	usersDao.updateOnline(0, uid);
	signinDao.updateSignin((Integer) signinid, sdtime);
	response.sendRedirect("index.jsp");
}
 
Example 6
Source File: ContextPathListener.java    From zxl with Apache License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent event) {
	ServletContext servletContext = event.getServletContext();
	if (servletContext.getAttribute(CONTEXT_PATH_ATTRIBUTE_NAME) != null) {
		servletContext.removeAttribute(CONTEXT_PATH_ATTRIBUTE_NAME);
	}
}
 
Example 7
Source File: WebappLoader.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Stop associated {@link ClassLoader} and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void stopInternal() throws LifecycleException {

    if (log.isDebugEnabled())
        log.debug(sm.getString("webappLoader.stopping"));

    setState(LifecycleState.STOPPING);

    // Remove context attributes as appropriate
    ServletContext servletContext = context.getServletContext();
    servletContext.removeAttribute(Globals.CLASS_PATH_ATTR);

    // Throw away our current class loader if any
    if (classLoader != null) {
        try {
            classLoader.stop();
        } finally {
            classLoader.destroy();
        }

        // classLoader must be non-null to have been registered
        try {
            String contextName = context.getName();
            if (!contextName.startsWith("/")) {
                contextName = "/" + contextName;
            }
            ObjectName cloname = new ObjectName(context.getDomain() + ":type=" +
                    classLoader.getClass().getSimpleName() + ",host=" +
                    context.getParent().getName() + ",context=" + contextName);
            Registry.getRegistry(null, null).unregisterComponent(cloname);
        } catch (Exception e) {
            log.warn("LifecycleException ", e);
        }
    }


    classLoader = null;
}
 
Example 8
Source File: PortalStartupListener.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
/**
 * Destroyes the portal driver config and removes it from servlet context.
 *
 * @param servletContext the servlet context.
 */
private void destroyDriverConfiguration(ServletContext servletContext)
{
    DriverConfiguration driverConfig = (DriverConfiguration)
            servletContext.getAttribute(DRIVER_CONFIG_KEY);
    if (driverConfig != null)
    {
        servletContext.removeAttribute(DRIVER_CONFIG_KEY);
    }
}
 
Example 9
Source File: WebappLoader.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Stop associated {@link ClassLoader} and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void stopInternal() throws LifecycleException {

    if (log.isDebugEnabled())
        log.debug(sm.getString("webappLoader.stopping"));

    setState(LifecycleState.STOPPING);

    // Remove context attributes as appropriate
    if (container instanceof Context) {
        ServletContext servletContext =
            ((Context) container).getServletContext();
        servletContext.removeAttribute(Globals.CLASS_PATH_ATTR);
    }

    // Throw away our current class loader
    if (classLoader != null) {
        ((Lifecycle) classLoader).stop();
        DirContextURLStreamHandler.unbind(classLoader);
    }

    try {
        StandardContext ctx=(StandardContext)container;
        String contextName = ctx.getName();
        if (!contextName.startsWith("/")) {
            contextName = "/" + contextName;
        }
        ObjectName cloname = new ObjectName
            (MBeanUtils.getDomain(ctx) + ":type=WebappClassLoader,context="
             + contextName + ",host=" + ctx.getParent().getName());
        Registry.getRegistry(null, null).unregisterComponent(cloname);
    } catch (Exception e) {
        log.error("LifecycleException ", e);
    }

    classLoader = null;
}
 
Example 10
Source File: MyServlet.java    From journaldev with MIT License 5 votes vote down vote up
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext ctx = request.getServletContext();
		ctx.setAttribute("User", "Pankaj");
		String user = (String) ctx.getAttribute("User");
		ctx.removeAttribute("User");
		
		HttpSession session = request.getSession();
		session.invalidate();
		
		PrintWriter out = response.getWriter();
		out.write("Hi "+user);
}
 
Example 11
Source File: PasswordResetServlet.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
	ServletContext sc = getServletContext();
	sc.removeAttribute("forgot");
	sc.removeAttribute("failed");
	response.sendRedirect("login.jsp");
}
 
Example 12
Source File: TestServletContainerInitializer.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent sce) {
    ServletContext servletContext = sce.getServletContext();
    servletContext.removeAttribute(INJECTOR_ATTRIBUTE_NAME);
    servletContext.removeAttribute(KERNEL_ATTRIBUTE_NAME);
    if (kernel != null) {
        try {
            Seed.disposeKernel(kernel);
        } finally {
            kernel = null;
        }
    }
}
 
Example 13
Source File: ContextListener.java    From jerseyoauth2 with MIT License 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent)
{
	ServletContext sc = servletContextEvent.getServletContext();
	sc.removeAttribute(Injector.class.getName());
	super.contextDestroyed(servletContextEvent);
}
 
Example 14
Source File: EverrestInitializedListener.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent sce) {
    makeFileCollectorDestroyer().stopFileCollector();
    ServletContext servletContext = sce.getServletContext();
    EverrestProcessor processor = (EverrestProcessor)servletContext.getAttribute(EverrestProcessor.class.getName());
    if (processor != null) {
        processor.stop();
    }
    servletContext.removeAttribute(EverrestProcessor.class.getName());
    servletContext.removeAttribute(EverrestConfiguration.class.getName());
    servletContext.removeAttribute(Application.class.getName());
    servletContext.removeAttribute(DependencySupplier.class.getName());
    servletContext.removeAttribute(ResourceBinder.class.getName());
    servletContext.removeAttribute(ApplicationProviderBinder.class.getName());
}
 
Example 15
Source File: MySessionListener.java    From mytwitter with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent event) {
	HttpSession session = event.getSession();
	Users user = (Users) session.getAttribute("user");
	if (user == null) {
		return;
	}
	ServletContext servletContext = event.getSession().getServletContext();
	servletContext.removeAttribute(((Users) session.getAttribute("user")).getUname());
	Integer onlineNum = (Integer) servletContext.getAttribute("onlineNum");
	if (onlineNum > 0) {
		servletContext.setAttribute("onlineNum", onlineNum - 1);
	}
	Object signinid = session.getAttribute("signinid");

	if (user == null || signinid == null) {
		return;
	}
	int uid = user.getUid();
	Timestamp sdtime = Times.getSystemTime();
	usersDao.updateOnline(0, uid);
	signinDao.updateSignin((Integer) signinid, sdtime);

	Object adid = session.getAttribute("adid");
	Admins admin = (Admins) session.getAttribute("admin");
	if (admin == null || adid == null) {
		return;
	}

	int aid = admin.getAid();
	adminsDao.updateOnline(0, aid);
	adloginDao.updateSignin((Integer) adid, sdtime);
}
 
Example 16
Source File: WebappLoader.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Stop associated {@link ClassLoader} and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void stopInternal() throws LifecycleException {

    if (log.isDebugEnabled())
        log.debug(sm.getString("webappLoader.stopping"));

    setState(LifecycleState.STOPPING);

    // Remove context attributes as appropriate
    if (container instanceof Context) {
        ServletContext servletContext =
            ((Context) container).getServletContext();
        servletContext.removeAttribute(Globals.CLASS_PATH_ATTR);
    }

    // Throw away our current class loader
    if (classLoader != null) {
        ((Lifecycle) classLoader).stop();
        DirContextURLStreamHandler.unbind(classLoader);
    }

    try {
        StandardContext ctx=(StandardContext)container;
        String contextName = ctx.getName();
        if (!contextName.startsWith("/")) {
            contextName = "/" + contextName;
        }
        ObjectName cloname = new ObjectName
            (MBeanUtils.getDomain(ctx) + ":type=WebappClassLoader,context="
             + contextName + ",host=" + ctx.getParent().getName());
        Registry.getRegistry(null, null).unregisterComponent(cloname);
    } catch (Exception e) {
        log.error("LifecycleException ", e);
    }

    classLoader = null;
}
 
Example 17
Source File: BootstrapContextListener.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
private static void disposeApplicationContext(ServletContext servletContext, ApplicationContext applicationContext) {
	if(applicationContext==null) {
		return;
	}
	servletContext.removeAttribute(ServerFrontend.LDP4J_APPLICATION_CONTEXT);
	try {
		ApplicationEngine.engine().dispose(applicationContext);
		LOGGER.info("LDP4j Application '{}' ({}) shutdown.",applicationContext.applicationName(),applicationContext.applicationClassName());
	} catch (ApplicationContextTerminationException e) {
		LOGGER.error(String.format("Could not shutdown LDP4j Application '%s' (%s) due to an unexpected context failure. Full stacktrace follows:",applicationContext.applicationName(),applicationContext.applicationClassName()),e);
	}
}
 
Example 18
Source File: EurekaServerBootstrap.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
public void contextDestroyed(ServletContext context) {
	try {
		log.info("Shutting down Eureka Server..");
		context.removeAttribute(EurekaServerContext.class.getName());

		destroyEurekaServerContext();
		destroyEurekaEnvironment();

	}
	catch (Throwable e) {
		log.error("Error shutting down eureka", e);
	}
	log.info("Eureka Service is now shutdown...");
}
 
Example 19
Source File: BaseGuiceServletContextListener.java    From jerseyoauth2 with MIT License 4 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
	ServletContext sc = servletContextEvent.getServletContext();
	sc.removeAttribute(Injector.class.getName());
	super.contextDestroyed(servletContextEvent);
}
 
Example 20
Source File: DaggerServletContextListener.java    From dagger-servlet with Apache License 2.0 4 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();
    servletContext.removeAttribute(OBJECT_GRAPH_NAME);
}