Servlet : session filter : Session « Servlets « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Servlets » SessionScreenshots 
Servlet : session filter
  
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionFilter implements Filter {

  private FilterConfig config;

  /** Creates new SessionFilter */
  public SessionFilter() {
  }

  public void init(FilterConfig filterConfigthrows ServletException {

    System.out.println("Instance created of " + getClass().getName());
    this.config = filterConfig;
  }

  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chainthrows java.io.IOException, ServletException {

    HttpSession session = ((HttpServletRequestrequest).getSession();
    ServletContext context = config.getServletContext();
    /*
     * use the ServletContext.log method to log filter messages
     */
    context.log("doFilter called in: " + config.getFilterName() " on "
        (new java.util.Date()));

    // log the session ID
    context.log("session ID: " + session.getId());

    // Find out whether the logged-in session attribute is set
    String logged = (Stringsession.getAttribute("logged-in");
    if (logged == null)
      session.setAttribute("logged-in""no");

    //log a message about the log-in status
    context.log("log-in status: "
        (Stringsession.getAttribute("logged-in"));
    context.log("");
    chain.doFilter(request, response);
  }

  public void destroy() {
    /*
     * called before the Filter instance is removed from service by the web
     * container
     */
  }
}

           
         
    
  
Related examples in the same category
1.Using Sessions in Servlet
2.Session Tracker
3.Servlet: simple session
4.Session logger
5.Servlet: Session display
6.Servlet: session listener
7.Servlet: session attribute listener
8.Servlet: Session bind listener
9.Servlet Session Example
10.Use cookie to save session data
11.Use hidden fields to save session data
12.Use URL rewrite to save session data
13.Session Events: implements HttpSessionBindingListener
14.Session Expiration Filter
15.Map adaptor for HttpSession objects
16.Fake session
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.