Modified: deltaspike/site/trunk/content/documentation/servlet.html URL: http://svn.apache.org/viewvc/deltaspike/site/trunk/content/documentation/servlet.html?rev=1873629&r1=1873628&r2=1873629&view=diff ============================================================================== --- deltaspike/site/trunk/content/documentation/servlet.html (original) +++ deltaspike/site/trunk/content/documentation/servlet.html Wed Feb 5 11:59:19 2020 @@ -1,712 +1,712 @@ -<!DOCTYPE html> -<head> - <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> - <meta charset="utf-8"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <meta name="description" content="deltaspike-generate-pages"> - <meta name="author" content="chm"> - <!-- No caching headers --> - <meta http-equiv="cache-control" content="no-cache"/> - <meta http-equiv="pragma" content="no-cache"/> - <meta http-equiv="expires" content="-1"/> - - <title>Servlet Module</title> - - <!-- - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. - --> - - <!-- Styles --> - <link href="https://deltaspike.apache.org/resources/css/bootstrap.css" rel="stylesheet"> - <link href="https://deltaspike.apache.org/resources/css/bootstrap-responsive.css" rel="stylesheet"> - <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.1.0/css/font-awesome.min.css" rel="stylesheet"> - - - - - <!-- Tocify - nice dynamic autoscrolling TOC --> - <link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tocify/1.9.0/stylesheets/jquery.tocify.min.css" rel="stylesheet"> - <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> - <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> - <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tocify/1.9.0/javascripts/jquery.tocify.min.js"></script> - - - <script type="text/javascript"> - $(function () { - $("#toc").tocify({ - scrollTo: 50, - extendPage: true, - context: "#doc-content", - selectors: "h2,h3,h4,h5" - }); - $(".fallback-toc").hide(); - }); - </script> - - - <style type="text/css"> - /* Stylesheet for CodeRay to match GitHub theme | MIT License | http://foundation.zurb.com */ -/*pre.CodeRay {background-color:#f7f7f8;}*/ -.CodeRay .line-numbers{border-right:1px solid #d8d8d8;padding:0 0.5em 0 .25em} -.CodeRay span.line-numbers{display:inline-block;margin-right:.5em;color:rgba(0,0,0,.3)} -.CodeRay .line-numbers strong{font-weight: normal} -table.CodeRay{border-collapse:separate;border-spacing:0;margin-bottom:0;border:0;background:none} -table.CodeRay td{vertical-align: top} -table.CodeRay td.line-numbers{text-align:right} -table.CodeRay td.line-numbers>pre{padding:0;color:rgba(0,0,0,.3)} -table.CodeRay td.code{padding:0 0 0 .5em} -table.CodeRay td.code>pre{padding:0} -.CodeRay .debug{color:#fff !important;background:#000080 !important} -.CodeRay .annotation{color:#007} -.CodeRay .attribute-name{color:#000080} -.CodeRay .attribute-value{color:#700} -.CodeRay .binary{color:#509} -.CodeRay .comment{color:#998;font-style:italic} -.CodeRay .char{color:#04d} -.CodeRay .char .content{color:#04d} -.CodeRay .char .delimiter{color:#039} -.CodeRay .class{color:#458;font-weight:bold} -.CodeRay .complex{color:#a08} -.CodeRay .constant,.CodeRay .predefined-constant{color:#008080} -.CodeRay .color{color:#099} -.CodeRay .class-variable{color:#369} -.CodeRay .decorator{color:#b0b} -.CodeRay .definition{color:#099} -.CodeRay .delimiter{color:#000} -.CodeRay .doc{color:#970} -.CodeRay .doctype{color:#34b} -.CodeRay .doc-string{color:#d42} -.CodeRay .escape{color:#666} -.CodeRay .entity{color:#800} -.CodeRay .error{color:#808} -.CodeRay .exception{color:inherit} -.CodeRay .filename{color:#099} -.CodeRay .function{color:#900;font-weight:bold} -.CodeRay .global-variable{color:#008080} -.CodeRay .hex{color:#058} -.CodeRay .integer,.CodeRay .float{color:#099} -.CodeRay .include{color:#555} -.CodeRay .inline{color:#00} -.CodeRay .inline .inline{background:#ccc} -.CodeRay .inline .inline .inline{background:#bbb} -.CodeRay .inline .inline-delimiter{color:#d14} -.CodeRay .inline-delimiter{color:#d14} -.CodeRay .important{color:#555;font-weight:bold} -.CodeRay .interpreted{color:#b2b} -.CodeRay .instance-variable{color:#008080} -.CodeRay .label{color:#970} -.CodeRay .local-variable{color:#963} -.CodeRay .octal{color:#40e} -.CodeRay .predefined{color:#369} -.CodeRay .preprocessor{color:#579} -.CodeRay .pseudo-class{color:#555} -.CodeRay .directive{font-weight:bold} -.CodeRay .type{font-weight:bold} -.CodeRay .predefined-type{color:inherit} -.CodeRay .reserved,.CodeRay .keyword {color:#000;font-weight:bold} -.CodeRay .key{color:#808} -.CodeRay .key .delimiter{color:#606} -.CodeRay .key .char{color:#80f} -.CodeRay .value{color:#088} -.CodeRay .regexp .delimiter{color:#808} -.CodeRay .regexp .content{color:#808} -.CodeRay .regexp .modifier{color:#808} -.CodeRay .regexp .char{color:#d14} -.CodeRay .regexp .function{color:#404;font-weight:bold} -.CodeRay .string{color:#d20} -.CodeRay .string .string .string{background:#ffd0d0} -.CodeRay .string .content{color:#d14} -.CodeRay .string .char{color:#d14} -.CodeRay .string .delimiter{color:#d14} -.CodeRay .shell{color:#d14} -.CodeRay .shell .delimiter{color:#d14} -.CodeRay .symbol{color:#990073} -.CodeRay .symbol .content{color:#a60} -.CodeRay .symbol .delimiter{color:#630} -.CodeRay .tag{color:#008080} -.CodeRay .tag-special{color:#d70} -.CodeRay .variable{color:#036} -.CodeRay .insert{background:#afa} -.CodeRay .delete{background:#faa} -.CodeRay .change{color:#aaf;background:#007} -.CodeRay .head{color:#f8f;background:#505} -.CodeRay .insert .insert{color:#080} -.CodeRay .delete .delete{color:#800} -.CodeRay .change .change{color:#66f} -.CodeRay .head .head{color:#f4f} - - body { - padding-top: 60px; - padding-bottom: 40px; - } - - .toc-like { - border-radius: 6px; - border: 1px solid #ccc; - } - - .toc-like li { - line-height: 30px; - text-indent: 10px; - } - - .toc-like li.custom-toc-header { - font-weight: bold; - background: #666; - color: white; - cursor: initial !important; - padding: 5px; - } - - .toc-like li.custom-toc-header a { - color: white; - font-style: normal; - text-shadow: none; - padding: 0; - } - - .toc-like li.custom-toc-header:hover a { - background: #666; - } - - .page-title { - text-align: left; - } - - #doc-content h2, - #doc-content h3, - #doc-content h4, - #doc-content h5, - #doc-content h6 { - padding-top: 0; - margin-top: 25px; - margin-bottom: 10px; - line-height: 1.4em; - } - - #doc-content h2 { - border-bottom: 1px solid lightgrey; - } - - - </style> - - <script type="text/javascript"> - - var _gaq = _gaq || []; - _gaq.push(['_setAccount', 'UA-36103647-1']); - _gaq.push(['_trackPageview']); - - (function () { - var ga = document.createElement('script'); - ga.type = 'text/javascript'; - ga.async = true; - ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; - var s = document.getElementsByTagName('script')[0]; - s.parentNode.insertBefore(ga, s); - })(); - - </script> -</head> - -<body> - -<div class="navbar navbar-fixed-top"> - <div class="navbar-inner"> - <div class="container"> - <a class="btn btn-navbar" data-toggle="collapse" - data-target=".nav-collapse"> <span class="icon-bar"></span> <span - class="icon-bar"></span> <span class="icon-bar"></span> - </a> - <a class="brand logocolor" href="../index.html">Apache DeltaSpike</a> - - <div class="nav-collapse"> - - - <ul class="nav"> - <li><a href="../index.html">Home</a></li> - <li class="active"><a href="../documentation">Documentation</a></li> - <li ><a href="../javadoc.html">Javadoc</a></li> - <li ><a href="../source.html">Source</a></li> - <li ><a href="../download.html">Download</a></li> - <li ><a href="../community.html">Community</a></li> - <!-- <li><a href="./support.html">Support</a></li> --> - <li ><a href="../news.html">News</a></li> - </ul> - </div> - <!--/.nav-collapse --> - <form id="search-form" action="https://www.google.com/search" - method="get" class="navbar-search pull-right"> - <input value="deltaspike.apache.org" name="sitesearch" - type="hidden"> <input class="search-query" name="q" - id="query" type="text"> - </form> - </div> - </div> -</div> - -<div class="container"> - <div class="row-fluid"> - - - - <div class="span8"> - <div class="page-title"> - <h1>Servlet Module</h1> - </div> - - <div id="doc-content"> - <div class="sect1"> -<h2 id="_overview">Overview</h2> -<div class="sectionbody"> -<div class="paragraph"> -<p>The Servlet module provides CDI integration with the Java Servlet API. It enables injection of common servlet objects and propagation of servlet events to the CDI event bus.</p> -</div> -</div> -</div> -<div class="sect1"> -<h2 id="_project_setup">Project Setup</h2> -<div class="sectionbody"> -<div class="paragraph"> -<p>The configuration information provided here is for Maven-based projects and it assumes that you have already declared the DeltaSpike version and DeltaSpike Core module for your projects, as detailed in <a href="configure.html">Configure DeltaSpike in Your Projects</a>. For Maven-independent projects, see <a href="configure.html#config-maven-indep">Configure DeltaSpike in Maven-independent Projects</a>.</p> -</div> -<div class="sect2"> -<h3 id="_1_declare_servlet_module_dependencies">1. Declare Servlet Module Dependencies</h3> -<div class="paragraph"> -<p>Add the Servlet module to the list of dependencies in the project <code>pom.xml</code> file using this code snippet:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="xml"><span class="tag"><dependency></span> - <span class="tag"><groupId></span>org.apache.deltaspike.modules<span class="tag"></groupId></span> - <span class="tag"><artifactId></span>deltaspike-servlet-module-api<span class="tag"></artifactId></span> - <span class="tag"><version></span>${deltaspike.version}<span class="tag"></version></span> - <span class="tag"><scope></span>compile<span class="tag"></scope></span> -<span class="tag"></dependency></span> - -<span class="tag"><dependency></span> - <span class="tag"><groupId></span>org.apache.deltaspike.modules<span class="tag"></groupId></span> - <span class="tag"><artifactId></span>deltaspike-servlet-module-impl<span class="tag"></artifactId></span> - <span class="tag"><version></span>${deltaspike.version}<span class="tag"></version></span> - <span class="tag"><scope></span>runtime<span class="tag"></scope></span> -<span class="tag"></dependency></span></code></pre> -</div> -</div> -<div class="paragraph"> -<p>Or if you’re using Gradle, add these dependencies to your <code>build.gradle</code>:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code> runtime 'org.apache.deltaspike.modules:deltaspike-servlet-module-impl' - compile 'org.apache.deltaspike.modules:deltaspike-servlet-module-api'</code></pre> -</div> -</div> -</div> -<div class="sect2"> -<h3 id="_2_configure_listeners_and_filters">2. Configure Listeners and Filters</h3> -<div class="paragraph"> -<p>In most cases there is no need for any additional configuration beside -adding the required dependencies to your project, because all required -listeners and filters are automatically registered in the container.</p> -</div> -<div class="paragraph"> -<p>However there are certain situations in which you will have to manually -register the listeners and filters in your <code>web.xml</code>:</p> -</div> -<div class="ulist"> -<ul> -<li> -<p>Your container does not support Servlet 3.0 or newer.</p> -</li> -<li> -<p>You have set <code>metadata-complete=true</code> in your <code>web.xml</code>.</p> -</li> -<li> -<p>You packaged the servlet module in the <code>lib</code> directory of an EAR archive.</p> -</li> -</ul> -</div> -<div class="paragraph"> -<p>In these cases you will have to add the following section manually to the project <code>web.xml</code>:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="xml"><span class="tag"><listener></span> - <span class="tag"><display-name></span>EventBridgeContextListener<span class="tag"></display-name></span> - <span class="tag"><listener-class></span>org.apache.deltaspike.servlet.impl.event.EventBridgeContextListener<span class="tag"></listener-class></span> -<span class="tag"></listener></span> - -<span class="tag"><listener></span> - <span class="tag"><display-name></span>EventBridgeSessionListener<span class="tag"></display-name></span> - <span class="tag"><listener-class></span>org.apache.deltaspike.servlet.impl.event.EventBridgeSessionListener<span class="tag"></listener-class></span> -<span class="tag"></listener></span> - -<span class="tag"><listener></span> - <span class="tag"><display-name></span>ServletContextHolderListener<span class="tag"></display-name></span> - <span class="tag"><listener-class></span>org.apache.deltaspike.servlet.impl.produce.ServletContextHolderListener<span class="tag"></listener-class></span> -<span class="tag"></listener></span> - -<span class="tag"><listener></span> - <span class="tag"><display-name></span>RequestResponseHolderListener<span class="tag"></display-name></span> - <span class="tag"><listener-class></span>org.apache.deltaspike.servlet.impl.produce.RequestResponseHolderListener<span class="tag"></listener-class></span> -<span class="tag"></listener></span> - -<span class="tag"><filter></span> - <span class="tag"><display-name></span>RequestResponseHolderFilter<span class="tag"></display-name></span> - <span class="tag"><filter-name></span>RequestResponseHolderFilter<span class="tag"></filter-name></span> - <span class="tag"><filter-class></span>org.apache.deltaspike.servlet.impl.produce.RequestResponseHolderFilter<span class="tag"></filter-class></span> -<span class="tag"></filter></span> -<span class="tag"><filter-mapping></span> - <span class="tag"><filter-name></span>RequestResponseHolderFilter<span class="tag"></filter-name></span> - <span class="tag"><url-pattern></span>/*<span class="tag"></url-pattern></span> -<span class="tag"></filter-mapping></span> - -<span class="tag"><filter></span> - <span class="tag"><display-name></span>EventBridgeFilter<span class="tag"></display-name></span> - <span class="tag"><filter-name></span>EventBridgeFilter<span class="tag"></filter-name></span> - <span class="tag"><filter-class></span>org.apache.deltaspike.servlet.impl.event.EventBridgeFilter<span class="tag"></filter-class></span> -<span class="tag"></filter></span> -<span class="tag"><filter-mapping></span> - <span class="tag"><filter-name></span>EventBridgeFilter<span class="tag"></filter-name></span> - <span class="tag"><url-pattern></span>/*<span class="tag"></url-pattern></span> -<span class="tag"></filter-mapping></span></code></pre> -</div> -</div> -</div> -</div> -</div> -<div class="sect1"> -<h2 id="_injectable_servlet_objects">Injectable Servlet Objects</h2> -<div class="sectionbody"> -<div class="paragraph"> -<p>The DeltaSpike Servlet module contains producers for many objects of a -Servlet environment. All produces are using the special qualifier -<code>@DeltaSpike</code> for compatibility with CDI 1.1, which supports the -injection of some Servlet objects out of the box.</p> -</div> -<div class="paragraph"> -<p>The following code shows the general injection pattern to use for all objects.</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> -<span class="directive">private</span> ServletObject servletObject;</code></pre> -</div> -</div> -<div class="sect2"> -<h3 id="_servletcontext">ServletContext</h3> -<div class="paragraph"> -<p>The <code>ServletContext</code> is made available in the application scope. It can -be injected into any CDI bean like this:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> -<span class="directive">private</span> ServletContext servletContext;</code></pre> -</div> -</div> -</div> -<div class="sect2"> -<h3 id="_servletrequest_httpservletrequest">ServletRequest / HttpServletRequest</h3> -<div class="paragraph"> -<p>The <code>ServletRequest</code> is made available in the request scope. The current -request can be injected into a CDI bean like this:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> -<span class="directive">private</span> ServletRequest request;</code></pre> -</div> -</div> -<div class="paragraph"> -<p>In case of HTTP requests you can also inject the <code>HttpServletRequest</code>:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> -<span class="directive">private</span> HttpServletRequest request;</code></pre> -</div> -</div> -</div> -<div class="sect2"> -<h3 id="_servletresponse_httpservletresponse">ServletResponse / HttpServletResponse</h3> -<div class="paragraph"> -<p>The <code>ServletResponse</code> is made available in the request scope. The -current response can be injected into a CDI bean like this:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> -<span class="directive">private</span> ServletResponse response;</code></pre> -</div> -</div> -<div class="paragraph"> -<p>In case of HTTP requests you can also inject the <code>HttpServletResponse</code>:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> -<span class="directive">private</span> HttpServletResponse response;</code></pre> -</div> -</div> -</div> -<div class="sect2"> -<h3 id="_httpsession">HttpSession</h3> -<div class="paragraph"> -<p>The <code>HttpSession</code> is made available in the session scope. You can inject -the current session of a user into a CDI bean like this:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> -<span class="directive">private</span> HttpSession session;</code></pre> -</div> -</div> -<div class="paragraph"> -<p>Please note that injecting the session this way will force the creation -of a session.</p> -</div> -</div> -<div class="sect2"> -<h3 id="_principal">Principal</h3> -<div class="paragraph"> -<p>The <code>Principal</code> is made available in the request scope. The current -principal can be injected into a CDI bean like this:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> -<span class="directive">private</span> <span class="predefined-type">Principal</span> principal;</code></pre> -</div> -</div> -<div class="paragraph"> -<p>The <code>Principal</code> is obtained by calling <code>getUserPrincipal()</code> on the -<code>HttpServletRequest</code>.</p> -</div> -</div> -</div> -</div> -<div class="sect1"> -<h2 id="_servlet_event_propagation">Servlet Event Propagation</h2> -<div class="sectionbody"> -<div class="paragraph"> -<p>The DeltaSpike Servlet module propagates a number of Servlet object -lifecycle events to the CDI event bus. This allows regular CDI beans to -observe these events and react accordingly.</p> -</div> -<div class="paragraph"> -<p>In most cases the event type is the object whose lifecycle is observed. -To distinguish between construction and destruction of the corresponding -object, DeltaSpike uses the qualifiers <code>@Initialized</code> and <code>@Destroyed</code>.</p> -</div> -<div class="paragraph"> -<p>The following sections shows which concrete Servlet objects are -supported and how their lifecycle can be observed.</p> -</div> -<div class="sect2"> -<h3 id="_servlet_context_lifecycle_events">Servlet Context Lifecycle Events</h3> -<div class="paragraph"> -<p>The Servlet module supports initialization and destruction events for -the <code>ServletContext</code>. These events can for example be used to detect -application startup or shutdown. The following code shows how these -events can be observed:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onCreate(<span class="annotation">@Observes</span> <span class="annotation">@Initialized</span> ServletContext context) { - <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Initialized ServletContext: </span><span class="delimiter">"</span></span> + context.getServletContextName()); -} - -<span class="directive">public</span> <span class="type">void</span> onDestroy(<span class="annotation">@Observes</span> <span class="annotation">@Destroyed</span> ServletContext context) { - <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Destroyed ServletContext: </span><span class="delimiter">"</span></span> + context.getServletContextName()); -}</code></pre> -</div> -</div> -<div class="paragraph"> -<p>The events are emitted from a <code>ServletContextListener</code> called -<code>EventBridgeContextListener</code>. You can disable lifecycle events for the -<code>ServletContext</code> by deactivating the following class:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java">org.apache.deltaspike.servlet.impl.event.EventBridgeContextListener</code></pre> -</div> -</div> -<div class="paragraph"> -<p>If you manually registered the required filters and listeners, you can -also simply remove the entry for the <code>EventBridgeContextListener</code> from -your <code>web.xml</code> to disable the events.</p> -</div> -</div> -<div class="sect2"> -<h3 id="_request_and_response_lifecycle_events">Request and Response Lifecycle Events</h3> -<div class="paragraph"> -<p>The Servlet module also supports initialization and destruction events -for the <code>HttpServletRequest</code> and <code>HttpServletResponse</code>. These events can -for example be used for initialization work like invoking -<code>setCharacterEncoding</code> on the request.</p> -</div> -<div class="paragraph"> -<p>The following example shows how to observe lifecycle events for the -request:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onCreate(<span class="annotation">@Observes</span> <span class="annotation">@Initialized</span> HttpServletRequest request) { - <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Starting to process request for: </span><span class="delimiter">"</span></span> + request.getRequestURI()); -} - -<span class="directive">public</span> <span class="type">void</span> onDestroy(<span class="annotation">@Observes</span> <span class="annotation">@Destroyed</span> HttpServletRequest request) { - <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Finished processing request for: </span><span class="delimiter">"</span></span> + request.getRequestURI()); -}</code></pre> -</div> -</div> -<div class="paragraph"> -<p>Observing lifecycle events for the response works the same way:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onCreate(<span class="annotation">@Observes</span> <span class="annotation">@Initialized</span> HttpServletResponse response) { - <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">HttpServletResponse created</span><span class="delimiter">"</span></span>); -} - -<span class="directive">public</span> <span class="type">void</span> onDestroy(<span class="annotation">@Observes</span> <span class="annotation">@Destroyed</span> HttpServletResponse response) { - <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">HttpServletResponse destroyed</span><span class="delimiter">"</span></span>); -}</code></pre> -</div> -</div> -<div class="paragraph"> -<p>All events of this category are emitted from a servlet filter called -<code>EventBridgeFilter</code>. If you want to disable events for this category, -just use DeltaSpike’s deactivation mechanism to deactivate the following -class:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java">org.apache.deltaspike.servlet.impl.event.EventBridgeFilter</code></pre> -</div> -</div> -<div class="paragraph"> -<p>If you manually registered the required filters and listeners you can -also simply remove the entry for the <code>EventBridgeFilter</code> from your -<code>web.xml</code> to disable the events.</p> -</div> -</div> -<div class="sect2"> -<h3 id="_session_lifecycle_events">Session Lifecycle Events</h3> -<div class="paragraph"> -<p>The last category of events supported by the DeltaSpike Servlet module -are the lifecycle events for the user’s HTTP session. The following -example shows how these events can be observed from a regular CDI bean.</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onCreate(<span class="annotation">@Observes</span> <span class="annotation">@Initialized</span> HttpSession session) { - <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Session created: </span><span class="delimiter">"</span></span> + session.getId()); -} - -<span class="directive">public</span> <span class="type">void</span> onDestroy(<span class="annotation">@Observes</span> <span class="annotation">@Destroyed</span> HttpSession session) { - <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Session destroyed: </span><span class="delimiter">"</span></span> + session.getId()); -}</code></pre> -</div> -</div> -<div class="paragraph"> -<p>The lifecycle events for the HTTP session are sent from a -<code>HttpSessionListener</code> called <code>EventBridgeSessionListener</code>. To disable -this event category, deactivate the following class:</p> -</div> -<div class="listingblock"> -<div class="content"> -<pre class="CodeRay highlight"><code data-lang="java">org.apache.deltaspike.servlet.impl.event.EventBridgeSessionListener</code></pre> -</div> -</div> -<div class="paragraph"> -<p>If you manually registered the required filters and listeners you can -also simply remove the entry for the <code>EventBridgeSessionListener</code> from -your <code>web.xml</code> to disable the events.</p> -</div> -</div> -</div> -</div> - </div> - </div> - - - <div class="span4"> - <div id="toc"> - <div class="moduledeps"> - <ul class="toc-like nav nav-list"> - - <li class="custom-toc-header">Depends on</li> - - <li><a href="core.html">Core</a></li> - - - - - <li class="custom-toc-header">Table of Contents</li> - - </ul> - </div> - </div> - - <div class="fallback-toc"> - <ul class="sectlevel1"> -<li><a href="#_overview">Overview</a></li> -<li><a href="#_project_setup">Project Setup</a> -<ul class="sectlevel2"> -<li><a href="#_1_declare_servlet_module_dependencies">1. Declare Servlet Module Dependencies</a></li> -<li><a href="#_2_configure_listeners_and_filters">2. Configure Listeners and Filters</a></li> -</ul> -</li> -<li><a href="#_injectable_servlet_objects">Injectable Servlet Objects</a> -<ul class="sectlevel2"> -<li><a href="#_servletcontext">ServletContext</a></li> -<li><a href="#_servletrequest_httpservletrequest">ServletRequest / HttpServletRequest</a></li> -<li><a href="#_servletresponse_httpservletresponse">ServletResponse / HttpServletResponse</a></li> -<li><a href="#_httpsession">HttpSession</a></li> -<li><a href="#_principal">Principal</a></li> -</ul> -</li> -<li><a href="#_servlet_event_propagation">Servlet Event Propagation</a> -<ul class="sectlevel2"> -<li><a href="#_servlet_context_lifecycle_events">Servlet Context Lifecycle Events</a></li> -<li><a href="#_request_and_response_lifecycle_events">Request and Response Lifecycle Events</a></li> -<li><a href="#_session_lifecycle_events">Session Lifecycle Events</a></li> -</ul> -</li> -</ul> - </div> - - </div> - - - </div> - <div class="row"> - <hr> - <footer> - <p>Copyright © 2011-2016 The Apache Software Foundation, - Licensed under the Apache License, Version 2.0.</p> - - <p>Apache and the Apache feather logo are trademarks of The Apache Software Foundation.</p> - </footer> - </div> -</div> - -</body> +<!DOCTYPE html> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <meta name="description" content="deltaspike-generate-pages"> + <meta name="author" content="chm"> + <!-- No caching headers --> + <meta http-equiv="cache-control" content="no-cache"/> + <meta http-equiv="pragma" content="no-cache"/> + <meta http-equiv="expires" content="-1"/> + + <title>Servlet Module</title> + + <!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + --> + + <!-- Styles --> + <link href="https://deltaspike.apache.org/resources/css/bootstrap.css" rel="stylesheet"> + <link href="https://deltaspike.apache.org/resources/css/bootstrap-responsive.css" rel="stylesheet"> + <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.1.0/css/font-awesome.min.css" rel="stylesheet"> + + + + + <!-- Tocify - nice dynamic autoscrolling TOC --> + <link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tocify/1.9.0/stylesheets/jquery.tocify.min.css" rel="stylesheet"> + <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> + <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> + <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tocify/1.9.0/javascripts/jquery.tocify.min.js"></script> + + + <script type="text/javascript"> + $(function () { + $("#toc").tocify({ + scrollTo: 50, + extendPage: true, + context: "#doc-content", + selectors: "h2,h3,h4,h5" + }); + $(".fallback-toc").hide(); + }); + </script> + + + <style type="text/css"> + /* Stylesheet for CodeRay to match GitHub theme | MIT License | http://foundation.zurb.com */ +/*pre.CodeRay {background-color:#f7f7f8;}*/ +.CodeRay .line-numbers{border-right:1px solid #d8d8d8;padding:0 0.5em 0 .25em} +.CodeRay span.line-numbers{display:inline-block;margin-right:.5em;color:rgba(0,0,0,.3)} +.CodeRay .line-numbers strong{font-weight: normal} +table.CodeRay{border-collapse:separate;border-spacing:0;margin-bottom:0;border:0;background:none} +table.CodeRay td{vertical-align: top} +table.CodeRay td.line-numbers{text-align:right} +table.CodeRay td.line-numbers>pre{padding:0;color:rgba(0,0,0,.3)} +table.CodeRay td.code{padding:0 0 0 .5em} +table.CodeRay td.code>pre{padding:0} +.CodeRay .debug{color:#fff !important;background:#000080 !important} +.CodeRay .annotation{color:#007} +.CodeRay .attribute-name{color:#000080} +.CodeRay .attribute-value{color:#700} +.CodeRay .binary{color:#509} +.CodeRay .comment{color:#998;font-style:italic} +.CodeRay .char{color:#04d} +.CodeRay .char .content{color:#04d} +.CodeRay .char .delimiter{color:#039} +.CodeRay .class{color:#458;font-weight:bold} +.CodeRay .complex{color:#a08} +.CodeRay .constant,.CodeRay .predefined-constant{color:#008080} +.CodeRay .color{color:#099} +.CodeRay .class-variable{color:#369} +.CodeRay .decorator{color:#b0b} +.CodeRay .definition{color:#099} +.CodeRay .delimiter{color:#000} +.CodeRay .doc{color:#970} +.CodeRay .doctype{color:#34b} +.CodeRay .doc-string{color:#d42} +.CodeRay .escape{color:#666} +.CodeRay .entity{color:#800} +.CodeRay .error{color:#808} +.CodeRay .exception{color:inherit} +.CodeRay .filename{color:#099} +.CodeRay .function{color:#900;font-weight:bold} +.CodeRay .global-variable{color:#008080} +.CodeRay .hex{color:#058} +.CodeRay .integer,.CodeRay .float{color:#099} +.CodeRay .include{color:#555} +.CodeRay .inline{color:#00} +.CodeRay .inline .inline{background:#ccc} +.CodeRay .inline .inline .inline{background:#bbb} +.CodeRay .inline .inline-delimiter{color:#d14} +.CodeRay .inline-delimiter{color:#d14} +.CodeRay .important{color:#555;font-weight:bold} +.CodeRay .interpreted{color:#b2b} +.CodeRay .instance-variable{color:#008080} +.CodeRay .label{color:#970} +.CodeRay .local-variable{color:#963} +.CodeRay .octal{color:#40e} +.CodeRay .predefined{color:#369} +.CodeRay .preprocessor{color:#579} +.CodeRay .pseudo-class{color:#555} +.CodeRay .directive{font-weight:bold} +.CodeRay .type{font-weight:bold} +.CodeRay .predefined-type{color:inherit} +.CodeRay .reserved,.CodeRay .keyword {color:#000;font-weight:bold} +.CodeRay .key{color:#808} +.CodeRay .key .delimiter{color:#606} +.CodeRay .key .char{color:#80f} +.CodeRay .value{color:#088} +.CodeRay .regexp .delimiter{color:#808} +.CodeRay .regexp .content{color:#808} +.CodeRay .regexp .modifier{color:#808} +.CodeRay .regexp .char{color:#d14} +.CodeRay .regexp .function{color:#404;font-weight:bold} +.CodeRay .string{color:#d20} +.CodeRay .string .string .string{background:#ffd0d0} +.CodeRay .string .content{color:#d14} +.CodeRay .string .char{color:#d14} +.CodeRay .string .delimiter{color:#d14} +.CodeRay .shell{color:#d14} +.CodeRay .shell .delimiter{color:#d14} +.CodeRay .symbol{color:#990073} +.CodeRay .symbol .content{color:#a60} +.CodeRay .symbol .delimiter{color:#630} +.CodeRay .tag{color:#008080} +.CodeRay .tag-special{color:#d70} +.CodeRay .variable{color:#036} +.CodeRay .insert{background:#afa} +.CodeRay .delete{background:#faa} +.CodeRay .change{color:#aaf;background:#007} +.CodeRay .head{color:#f8f;background:#505} +.CodeRay .insert .insert{color:#080} +.CodeRay .delete .delete{color:#800} +.CodeRay .change .change{color:#66f} +.CodeRay .head .head{color:#f4f} + + body { + padding-top: 60px; + padding-bottom: 40px; + } + + .toc-like { + border-radius: 6px; + border: 1px solid #ccc; + } + + .toc-like li { + line-height: 30px; + text-indent: 10px; + } + + .toc-like li.custom-toc-header { + font-weight: bold; + background: #666; + color: white; + cursor: initial !important; + padding: 5px; + } + + .toc-like li.custom-toc-header a { + color: white; + font-style: normal; + text-shadow: none; + padding: 0; + } + + .toc-like li.custom-toc-header:hover a { + background: #666; + } + + .page-title { + text-align: left; + } + + #doc-content h2, + #doc-content h3, + #doc-content h4, + #doc-content h5, + #doc-content h6 { + padding-top: 0; + margin-top: 25px; + margin-bottom: 10px; + line-height: 1.4em; + } + + #doc-content h2 { + border-bottom: 1px solid lightgrey; + } + + + </style> + + <script type="text/javascript"> + + var _gaq = _gaq || []; + _gaq.push(['_setAccount', 'UA-36103647-1']); + _gaq.push(['_trackPageview']); + + (function () { + var ga = document.createElement('script'); + ga.type = 'text/javascript'; + ga.async = true; + ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; + var s = document.getElementsByTagName('script')[0]; + s.parentNode.insertBefore(ga, s); + })(); + + </script> +</head> + +<body> + +<div class="navbar navbar-fixed-top"> + <div class="navbar-inner"> + <div class="container"> + <a class="btn btn-navbar" data-toggle="collapse" + data-target=".nav-collapse"> <span class="icon-bar"></span> <span + class="icon-bar"></span> <span class="icon-bar"></span> + </a> + <a class="brand logocolor" href="../index.html">Apache DeltaSpike</a> + + <div class="nav-collapse"> + + + <ul class="nav"> + <li><a href="../index.html">Home</a></li> + <li class="active"><a href="../documentation">Documentation</a></li> + <li ><a href="../javadoc.html">Javadoc</a></li> + <li ><a href="../source.html">Source</a></li> + <li ><a href="../download.html">Download</a></li> + <li ><a href="../community.html">Community</a></li> + <!-- <li><a href="./support.html">Support</a></li> --> + <li ><a href="../news.html">News</a></li> + </ul> + </div> + <!--/.nav-collapse --> + <form id="search-form" action="https://www.google.com/search" + method="get" class="navbar-search pull-right"> + <input value="deltaspike.apache.org" name="sitesearch" + type="hidden"> <input class="search-query" name="q" + id="query" type="text"> + </form> + </div> + </div> +</div> + +<div class="container"> + <div class="row-fluid"> + + + + <div class="span8"> + <div class="page-title"> + <h1>Servlet Module</h1> + </div> + + <div id="doc-content"> + <div class="sect1"> +<h2 id="_overview">Overview</h2> +<div class="sectionbody"> +<div class="paragraph"> +<p>The Servlet module provides CDI integration with the Java Servlet API. It enables injection of common servlet objects and propagation of servlet events to the CDI event bus.</p> +</div> +</div> +</div> +<div class="sect1"> +<h2 id="_project_setup">Project Setup</h2> +<div class="sectionbody"> +<div class="paragraph"> +<p>The configuration information provided here is for Maven-based projects and it assumes that you have already declared the DeltaSpike version and DeltaSpike Core module for your projects, as detailed in <a href="configure.html">Configure DeltaSpike in Your Projects</a>. For Maven-independent projects, see <a href="configure.html#config-maven-indep">Configure DeltaSpike in Maven-independent Projects</a>.</p> +</div> +<div class="sect2"> +<h3 id="_1_declare_servlet_module_dependencies">1. Declare Servlet Module Dependencies</h3> +<div class="paragraph"> +<p>Add the Servlet module to the list of dependencies in the project <code>pom.xml</code> file using this code snippet:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="xml"><span class="tag"><dependency></span> + <span class="tag"><groupId></span>org.apache.deltaspike.modules<span class="tag"></groupId></span> + <span class="tag"><artifactId></span>deltaspike-servlet-module-api<span class="tag"></artifactId></span> + <span class="tag"><version></span>${deltaspike.version}<span class="tag"></version></span> + <span class="tag"><scope></span>compile<span class="tag"></scope></span> +<span class="tag"></dependency></span> + +<span class="tag"><dependency></span> + <span class="tag"><groupId></span>org.apache.deltaspike.modules<span class="tag"></groupId></span> + <span class="tag"><artifactId></span>deltaspike-servlet-module-impl<span class="tag"></artifactId></span> + <span class="tag"><version></span>${deltaspike.version}<span class="tag"></version></span> + <span class="tag"><scope></span>runtime<span class="tag"></scope></span> +<span class="tag"></dependency></span></code></pre> +</div> +</div> +<div class="paragraph"> +<p>Or if you’re using Gradle, add these dependencies to your <code>build.gradle</code>:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code> runtime 'org.apache.deltaspike.modules:deltaspike-servlet-module-impl' + compile 'org.apache.deltaspike.modules:deltaspike-servlet-module-api'</code></pre> +</div> +</div> +</div> +<div class="sect2"> +<h3 id="_2_configure_listeners_and_filters">2. Configure Listeners and Filters</h3> +<div class="paragraph"> +<p>In most cases there is no need for any additional configuration beside +adding the required dependencies to your project, because all required +listeners and filters are automatically registered in the container.</p> +</div> +<div class="paragraph"> +<p>However there are certain situations in which you will have to manually +register the listeners and filters in your <code>web.xml</code>:</p> +</div> +<div class="ulist"> +<ul> +<li> +<p>Your container does not support Servlet 3.0 or newer.</p> +</li> +<li> +<p>You have set <code>metadata-complete=true</code> in your <code>web.xml</code>.</p> +</li> +<li> +<p>You packaged the servlet module in the <code>lib</code> directory of an EAR archive.</p> +</li> +</ul> +</div> +<div class="paragraph"> +<p>In these cases you will have to add the following section manually to the project <code>web.xml</code>:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="xml"><span class="tag"><listener></span> + <span class="tag"><display-name></span>EventBridgeContextListener<span class="tag"></display-name></span> + <span class="tag"><listener-class></span>org.apache.deltaspike.servlet.impl.event.EventBridgeContextListener<span class="tag"></listener-class></span> +<span class="tag"></listener></span> + +<span class="tag"><listener></span> + <span class="tag"><display-name></span>EventBridgeSessionListener<span class="tag"></display-name></span> + <span class="tag"><listener-class></span>org.apache.deltaspike.servlet.impl.event.EventBridgeSessionListener<span class="tag"></listener-class></span> +<span class="tag"></listener></span> + +<span class="tag"><listener></span> + <span class="tag"><display-name></span>ServletContextHolderListener<span class="tag"></display-name></span> + <span class="tag"><listener-class></span>org.apache.deltaspike.servlet.impl.produce.ServletContextHolderListener<span class="tag"></listener-class></span> +<span class="tag"></listener></span> + +<span class="tag"><listener></span> + <span class="tag"><display-name></span>RequestResponseHolderListener<span class="tag"></display-name></span> + <span class="tag"><listener-class></span>org.apache.deltaspike.servlet.impl.produce.RequestResponseHolderListener<span class="tag"></listener-class></span> +<span class="tag"></listener></span> + +<span class="tag"><filter></span> + <span class="tag"><display-name></span>RequestResponseHolderFilter<span class="tag"></display-name></span> + <span class="tag"><filter-name></span>RequestResponseHolderFilter<span class="tag"></filter-name></span> + <span class="tag"><filter-class></span>org.apache.deltaspike.servlet.impl.produce.RequestResponseHolderFilter<span class="tag"></filter-class></span> +<span class="tag"></filter></span> +<span class="tag"><filter-mapping></span> + <span class="tag"><filter-name></span>RequestResponseHolderFilter<span class="tag"></filter-name></span> + <span class="tag"><url-pattern></span>/*<span class="tag"></url-pattern></span> +<span class="tag"></filter-mapping></span> + +<span class="tag"><filter></span> + <span class="tag"><display-name></span>EventBridgeFilter<span class="tag"></display-name></span> + <span class="tag"><filter-name></span>EventBridgeFilter<span class="tag"></filter-name></span> + <span class="tag"><filter-class></span>org.apache.deltaspike.servlet.impl.event.EventBridgeFilter<span class="tag"></filter-class></span> +<span class="tag"></filter></span> +<span class="tag"><filter-mapping></span> + <span class="tag"><filter-name></span>EventBridgeFilter<span class="tag"></filter-name></span> + <span class="tag"><url-pattern></span>/*<span class="tag"></url-pattern></span> +<span class="tag"></filter-mapping></span></code></pre> +</div> +</div> +</div> +</div> +</div> +<div class="sect1"> +<h2 id="_injectable_servlet_objects">Injectable Servlet Objects</h2> +<div class="sectionbody"> +<div class="paragraph"> +<p>The DeltaSpike Servlet module contains producers for many objects of a +Servlet environment. All produces are using the special qualifier +<code>@DeltaSpike</code> for compatibility with CDI 1.1, which supports the +injection of some Servlet objects out of the box.</p> +</div> +<div class="paragraph"> +<p>The following code shows the general injection pattern to use for all objects.</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> +<span class="directive">private</span> ServletObject servletObject;</code></pre> +</div> +</div> +<div class="sect2"> +<h3 id="_servletcontext">ServletContext</h3> +<div class="paragraph"> +<p>The <code>ServletContext</code> is made available in the application scope. It can +be injected into any CDI bean like this:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> +<span class="directive">private</span> ServletContext servletContext;</code></pre> +</div> +</div> +</div> +<div class="sect2"> +<h3 id="_servletrequest_httpservletrequest">ServletRequest / HttpServletRequest</h3> +<div class="paragraph"> +<p>The <code>ServletRequest</code> is made available in the request scope. The current +request can be injected into a CDI bean like this:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> +<span class="directive">private</span> ServletRequest request;</code></pre> +</div> +</div> +<div class="paragraph"> +<p>In case of HTTP requests you can also inject the <code>HttpServletRequest</code>:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> +<span class="directive">private</span> HttpServletRequest request;</code></pre> +</div> +</div> +</div> +<div class="sect2"> +<h3 id="_servletresponse_httpservletresponse">ServletResponse / HttpServletResponse</h3> +<div class="paragraph"> +<p>The <code>ServletResponse</code> is made available in the request scope. The +current response can be injected into a CDI bean like this:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> +<span class="directive">private</span> ServletResponse response;</code></pre> +</div> +</div> +<div class="paragraph"> +<p>In case of HTTP requests you can also inject the <code>HttpServletResponse</code>:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> +<span class="directive">private</span> HttpServletResponse response;</code></pre> +</div> +</div> +</div> +<div class="sect2"> +<h3 id="_httpsession">HttpSession</h3> +<div class="paragraph"> +<p>The <code>HttpSession</code> is made available in the session scope. You can inject +the current session of a user into a CDI bean like this:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> +<span class="directive">private</span> HttpSession session;</code></pre> +</div> +</div> +<div class="paragraph"> +<p>Please note that injecting the session this way will force the creation +of a session.</p> +</div> +</div> +<div class="sect2"> +<h3 id="_principal">Principal</h3> +<div class="paragraph"> +<p>The <code>Principal</code> is made available in the request scope. The current +principal can be injected into a CDI bean like this:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="annotation">@Inject</span> <span class="annotation">@DeltaSpike</span> +<span class="directive">private</span> <span class="predefined-type">Principal</span> principal;</code></pre> +</div> +</div> +<div class="paragraph"> +<p>The <code>Principal</code> is obtained by calling <code>getUserPrincipal()</code> on the +<code>HttpServletRequest</code>.</p> +</div> +</div> +</div> +</div> +<div class="sect1"> +<h2 id="_servlet_event_propagation">Servlet Event Propagation</h2> +<div class="sectionbody"> +<div class="paragraph"> +<p>The DeltaSpike Servlet module propagates a number of Servlet object +lifecycle events to the CDI event bus. This allows regular CDI beans to +observe these events and react accordingly.</p> +</div> +<div class="paragraph"> +<p>In most cases the event type is the object whose lifecycle is observed. +To distinguish between construction and destruction of the corresponding +object, DeltaSpike uses the qualifiers <code>@Initialized</code> and <code>@Destroyed</code>.</p> +</div> +<div class="paragraph"> +<p>The following sections shows which concrete Servlet objects are +supported and how their lifecycle can be observed.</p> +</div> +<div class="sect2"> +<h3 id="_servlet_context_lifecycle_events">Servlet Context Lifecycle Events</h3> +<div class="paragraph"> +<p>The Servlet module supports initialization and destruction events for +the <code>ServletContext</code>. These events can for example be used to detect +application startup or shutdown. The following code shows how these +events can be observed:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onCreate(<span class="annotation">@Observes</span> <span class="annotation">@Initialized</span> ServletContext context) { + <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Initialized ServletContext: </span><span class="delimiter">"</span></span> + context.getServletContextName()); +} + +<span class="directive">public</span> <span class="type">void</span> onDestroy(<span class="annotation">@Observes</span> <span class="annotation">@Destroyed</span> ServletContext context) { + <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Destroyed ServletContext: </span><span class="delimiter">"</span></span> + context.getServletContextName()); +}</code></pre> +</div> +</div> +<div class="paragraph"> +<p>The events are emitted from a <code>ServletContextListener</code> called +<code>EventBridgeContextListener</code>. You can disable lifecycle events for the +<code>ServletContext</code> by deactivating the following class:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java">org.apache.deltaspike.servlet.impl.event.EventBridgeContextListener</code></pre> +</div> +</div> +<div class="paragraph"> +<p>If you manually registered the required filters and listeners, you can +also simply remove the entry for the <code>EventBridgeContextListener</code> from +your <code>web.xml</code> to disable the events.</p> +</div> +</div> +<div class="sect2"> +<h3 id="_request_and_response_lifecycle_events">Request and Response Lifecycle Events</h3> +<div class="paragraph"> +<p>The Servlet module also supports initialization and destruction events +for the <code>HttpServletRequest</code> and <code>HttpServletResponse</code>. These events can +for example be used for initialization work like invoking +<code>setCharacterEncoding</code> on the request.</p> +</div> +<div class="paragraph"> +<p>The following example shows how to observe lifecycle events for the +request:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onCreate(<span class="annotation">@Observes</span> <span class="annotation">@Initialized</span> HttpServletRequest request) { + <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Starting to process request for: </span><span class="delimiter">"</span></span> + request.getRequestURI()); +} + +<span class="directive">public</span> <span class="type">void</span> onDestroy(<span class="annotation">@Observes</span> <span class="annotation">@Destroyed</span> HttpServletRequest request) { + <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Finished processing request for: </span><span class="delimiter">"</span></span> + request.getRequestURI()); +}</code></pre> +</div> +</div> +<div class="paragraph"> +<p>Observing lifecycle events for the response works the same way:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onCreate(<span class="annotation">@Observes</span> <span class="annotation">@Initialized</span> HttpServletResponse response) { + <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">HttpServletResponse created</span><span class="delimiter">"</span></span>); +} + +<span class="directive">public</span> <span class="type">void</span> onDestroy(<span class="annotation">@Observes</span> <span class="annotation">@Destroyed</span> HttpServletResponse response) { + <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">HttpServletResponse destroyed</span><span class="delimiter">"</span></span>); +}</code></pre> +</div> +</div> +<div class="paragraph"> +<p>All events of this category are emitted from a servlet filter called +<code>EventBridgeFilter</code>. If you want to disable events for this category, +just use DeltaSpike’s deactivation mechanism to deactivate the following +class:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java">org.apache.deltaspike.servlet.impl.event.EventBridgeFilter</code></pre> +</div> +</div> +<div class="paragraph"> +<p>If you manually registered the required filters and listeners you can +also simply remove the entry for the <code>EventBridgeFilter</code> from your +<code>web.xml</code> to disable the events.</p> +</div> +</div> +<div class="sect2"> +<h3 id="_session_lifecycle_events">Session Lifecycle Events</h3> +<div class="paragraph"> +<p>The last category of events supported by the DeltaSpike Servlet module +are the lifecycle events for the user’s HTTP session. The following +example shows how these events can be observed from a regular CDI bean.</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onCreate(<span class="annotation">@Observes</span> <span class="annotation">@Initialized</span> HttpSession session) { + <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Session created: </span><span class="delimiter">"</span></span> + session.getId()); +} + +<span class="directive">public</span> <span class="type">void</span> onDestroy(<span class="annotation">@Observes</span> <span class="annotation">@Destroyed</span> HttpSession session) { + <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">Session destroyed: </span><span class="delimiter">"</span></span> + session.getId()); +}</code></pre> +</div> +</div> +<div class="paragraph"> +<p>The lifecycle events for the HTTP session are sent from a +<code>HttpSessionListener</code> called <code>EventBridgeSessionListener</code>. To disable +this event category, deactivate the following class:</p> +</div> +<div class="listingblock"> +<div class="content"> +<pre class="CodeRay highlight"><code data-lang="java">org.apache.deltaspike.servlet.impl.event.EventBridgeSessionListener</code></pre> +</div> +</div> +<div class="paragraph"> +<p>If you manually registered the required filters and listeners you can +also simply remove the entry for the <code>EventBridgeSessionListener</code> from +your <code>web.xml</code> to disable the events.</p> +</div> +</div> +</div> +</div> + </div> + </div> + + + <div class="span4"> + <div id="toc"> + <div class="moduledeps"> + <ul class="toc-like nav nav-list"> + + <li class="custom-toc-header">Depends on</li> + + <li><a href="core.html">Core</a></li> + + + + + <li class="custom-toc-header">Table of Contents</li> + + </ul> + </div> + </div> + + <div class="fallback-toc"> + <ul class="sectlevel1"> +<li><a href="#_overview">Overview</a></li> +<li><a href="#_project_setup">Project Setup</a> +<ul class="sectlevel2"> +<li><a href="#_1_declare_servlet_module_dependencies">1. Declare Servlet Module Dependencies</a></li> +<li><a href="#_2_configure_listeners_and_filters">2. Configure Listeners and Filters</a></li> +</ul> +</li> +<li><a href="#_injectable_servlet_objects">Injectable Servlet Objects</a> +<ul class="sectlevel2"> +<li><a href="#_servletcontext">ServletContext</a></li> +<li><a href="#_servletrequest_httpservletrequest">ServletRequest / HttpServletRequest</a></li> +<li><a href="#_servletresponse_httpservletresponse">ServletResponse / HttpServletResponse</a></li> +<li><a href="#_httpsession">HttpSession</a></li> +<li><a href="#_principal">Principal</a></li> +</ul> +</li> +<li><a href="#_servlet_event_propagation">Servlet Event Propagation</a> +<ul class="sectlevel2"> +<li><a href="#_servlet_context_lifecycle_events">Servlet Context Lifecycle Events</a></li> +<li><a href="#_request_and_response_lifecycle_events">Request and Response Lifecycle Events</a></li> +<li><a href="#_session_lifecycle_events">Session Lifecycle Events</a></li> +</ul> +</li> +</ul> + </div> + + </div> + + + </div> + <div class="row"> + <hr> + <footer> + <p>Copyright © 2011-2016 The Apache Software Foundation, + Licensed under the Apache License, Version 2.0.</p> + + <p>Apache and the Apache feather logo are trademarks of The Apache Software Foundation.</p> + </footer> + </div> +</div> + +</body> </html> \ No newline at end of file
