Context binder : Context « 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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Servlets » Context 




Context binder

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ContextBinder extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    //bind an object to the servlet context
    getServletContext().setAttribute("com.java2s.ContextObject",
        new ContextObject());

    //better display something
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out
        .println("<html><head><title>Servlet Context Attribute</title></head><body>");
    out.println("<h2>Servlet Context Attribute Bound</h2>");
    out.println("Object: "
        + getServletContext().getAttribute("com.java2s.ContextObject"));
    out.println("</body></html>");

  //end doGet

}

//ContextObject.java

class ContextObject {

  private Map map;

  public ContextObject() {

    map = Collections.synchronizedMap(new HashMap());
  }

  public void put(Object key, Object value) {

    if (key == null || value == null)
      throw new IllegalArgumentException(
          "Invalid parameters passed to ContextObject.put");

    map.put(key, value);
  }

  public Map getMap() {

    return map;

  }

  public String getValues() {

    StringBuffer buf = new StringBuffer("");
    Set set = map.keySet();

    synchronized (map) {

      Iterator i = set.iterator();
      while (i.hasNext())
        buf.append((Stringi.next() "<br>");
    }

    return buf.toString();

  }

  public String toString() {

    return getClass().getName() "[ " + map + " ]";
  }

}

           
       














Related examples in the same category
1.Servlets Context Sample
2.Servlets ServletContextListener Demo
3.Context log
4.Context logger
5.Context accessor
6.Set the context parameters in web.xml
7.Log in ServletContext
8.Context Attributes Servlet
9.Using Contexts Servlet
10.Context Parameters Servlet
11.Get settings from ServletContext
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.