Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>UPDATE</p> <p>Just as an update and to be more explicit, these are the main differences between servlets 2.5 and 3 (I'm not trying to be exhaustive, I'm just mentioning the most interesting parts):</p> <h2>Annotations to declare servlets, filters and listeners (ease of development)</h2> <p>In servlets 2.5, to declare a servlet with one init parameter you need to add this to <strong>web.xml</strong>:</p> <pre class="lang-xml prettyprint-override"><code>&lt;servlet&gt; &lt;servlet-name&gt;myServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;my.server.side.stuff.MyAwesomeServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;configFile&lt;/param-name&gt; &lt;param-value&gt;config.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;myServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/path/to/my/servlet&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>In servlets 3, <strong>web.xml</strong> is optional and you can use annotations instead of XML. The same example:</p> <pre><code>@WebServlet(name="myServlet", urlPatterns={"/path/to/my/servlet"}, initParams={@InitParam(name="configFile", value="config.xml")}) public class MyAwesomeServlet extends HttpServlet { ... } </code></pre> <p>For filters, you need to add this in <strong>web.xml</strong> in servlets 2.5:</p> <pre class="lang-xml prettyprint-override"><code>&lt;filter&gt; &lt;filter-name&gt;myFilter&lt;/filter-name&gt; &lt;filter-class&gt;my.server.side.stuff.MyAwesomeServlet&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;myFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/path/to/my/filter&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>Equivalent using annotations in servlets 3:</p> <pre><code>@ServletFilter(name="myFilter", urlPatterns={"/path/to/my/filter"}) public class MyAwesomeFilter implements Filter { ... } </code></pre> <p>For a listener (in this case a ServletContextListener), in servlets 2.5:</p> <pre class="lang-xml prettyprint-override"><code>&lt;listener&gt; &lt;listener-class&gt;my.server.side.stuff.MyAwesomeListener&lt;/listener-class&gt; &lt;/listener&gt; </code></pre> <p>The same using annotations:</p> <pre><code>@WebServletContextListener public class MyAwesomeListener implements ServletContextListener { ... } </code></pre> <h2>Modularization of web.xml (Pluggability)</h2> <ul> <li>In servlets 2.5 there is just one monolithic <strong>web.xml</strong> file.</li> <li>In servlets 3, each "loadable" jar can have a <strong>web-fragment.xml</strong> in its <strong>META-INF</strong> directory specifying servlets, filters, etc. This is to allow libraries and frameworks to specify their own servlets or other objects.</li> </ul> <h2>Dynamic registration of servlets, filters and listeners at context initialization time (Pluggability)</h2> <p>In servlets 3, a <code>ServletContextListener</code> can add dynamically servlets, filters and listeners using the following methods added to <code>SevletContext</code>: <code>addServlet()</code>, <code>addFilter()</code> and <code>addListener()</code></p> <h2>Asynchronous support</h2> <p>Example: say that some servlet container has five threads in its thread pool, and there is a time-consuming process to be executed per request (like a complex SQL query).</p> <ul> <li><p>With servlets 2.5 this servlet container would run out of available threads if it receives five requests at the same time and the five available threads start doing the process, because the threads wouldn't return until <code>service()</code> (or <code>doGet()</code>, <code>doPost()</code>, etc.) is executed from start to end and returns a response.</p></li> <li><p>With servlets 3.0, this long-time process can be delegated to another thread and finish <code>service()</code> before sending the response (the response now will be sent by the latest thread). This way the thread is free to receive new responses.</p></li> </ul> <p>An example of asynchronous support:</p> <p>Servlets 2.5:</p> <pre><code>public class MyAwesomeServlet extends HttpSerlvet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) { // ... runSlowProcess(); // no async support, thread will be free when runSlowProcess() and // doGet finish // ... } } </code></pre> <p>Servlets 3:</p> <pre><code>@WebServlet(name="myServlet", urlPatterns={"/mySlowProcess"}, asyncSupported=true) // asyncSupported MUST be specified for // servlets that support asynchronous // processing public class MyAwesomeServlet extends HttpSerlvet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) { // an AsyncContext is created, now the response will be completed // not when doGet finalizes its execution, but when // myAsyncContext.complete() is called. AsyncContext myAsyncContext = request.startAsync(request, response); // ... // myAsyncContext is passed to another thread delegateExecutionToProcessingThread(myAsyncContext); // done, now this thread is free to serve another request } } // ... and somewhere in another part of the code: public class MyProcessingObject { public void doSlowProcess() { // ... runSlowProcess(); myAsyncContext.complete(); // request is now completed. // ... } } </code></pre> <p>The interface <code>AsyncContext</code> also has methods to get the request object, response object and add listeners to notify them when a process has finished.</p> <h2>Programmatic login and logout (security enhancements)</h2> <p>In servlets 3, the interface <code>HttpServletRequest</code> has been added two new methods: <code>login(username, password)</code> and <code>logout()</code>.</p> <p>For more details, have a look at the <a href="http://docs.oracle.com/javaee/6/api/">Java EE 6 API</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload