The J2EETM Tutorial
Accessing the Web Context

The context in which Web components execute is an object that implements the "http://java.sun/j2ee/tutorial/api/javax/servlet/ServletContextTARGET=_blank>ServletContext interface. You retrieve the Web context with the getServletContext method. The Web context provides methods for accessing:

The Web context is used by the Duke's Bookstore filters examples/src/web/bookstore1/filters/HitCounterFilter.javaTARGET=_blank>filters.HitCounterFilter and OrderFilter, discussed in the section "Servlets8103101Filtering Requests and Responses. The filters store a counter as a context attribute. Recall from "Servlets5102985Controlling Concurrent Access to Shared Resources that the counter's access methods are synchronized to prevent incompatible operations by servlets that are running concurrently. A filter retrieves the counter object with the context's getAttribute method. The incremented value of the counter is recorded with the context's log method.

public final class HitCounterFilter implements Filter { private FilterConfig filterConfig = null; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ... StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); ServletContext context = filterCongetServletContext(); Counter counter = (Counter)context. getAttribute("hitCounter); ... writer.println("The number of hits is: + counter.incCounter()); ... context.log(sw.getBuffer().toString()); ... } }

Bhopal news
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india