I'm rolling J2EE code that adheres to Servlet 2.5 and I'm wondering what are the major differences between 2.5 and 3. Pointers to official Sun docs and personal experiences are most appreciated.
If I shouldn't be concerning myself with 3 for the time being, just say so. Thanks!
The current version of Servlet is 5.0.
A controller is a part of an architectural pattern. A servlet is a part of a server (usually, a web container).
A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.
UPDATE
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):
In servlets 2.5, to declare a servlet with one init parameter you need to add this to web.xml:
<servlet> <servlet-name>myServlet</servlet-name> <servlet-class>my.server.side.stuff.MyAwesomeServlet</servlet-class> <init-param> <param-name>configFile</param-name> <param-value>config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/path/to/my/servlet</url-pattern> </servlet-mapping>
In servlets 3, web.xml is optional and you can use annotations instead of XML. The same example:
@WebServlet(name="myServlet", urlPatterns={"/path/to/my/servlet"}, initParams={@InitParam(name="configFile", value="config.xml")}) public class MyAwesomeServlet extends HttpServlet { ... }
For filters, you need to add this in web.xml in servlets 2.5:
<filter> <filter-name>myFilter</filter-name> <filter-class>my.server.side.stuff.MyAwesomeServlet</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/path/to/my/filter</url-pattern> </filter-mapping>
Equivalent using annotations in servlets 3:
@ServletFilter(name="myFilter", urlPatterns={"/path/to/my/filter"}) public class MyAwesomeFilter implements Filter { ... }
For a listener (in this case a ServletContextListener), in servlets 2.5:
<listener> <listener-class>my.server.side.stuff.MyAwesomeListener</listener-class> </listener>
The same using annotations:
@WebServletContextListener public class MyAwesomeListener implements ServletContextListener { ... }
In servlets 3, a ServletContextListener
can add dynamically servlets, filters and listeners using the following methods added to SevletContext
: addServlet()
, addFilter()
and addListener()
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).
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 service()
(or doGet()
, doPost()
, etc.) is executed from start to end and returns a response.
With servlets 3.0, this long-time process can be delegated to another thread and finish service()
before sending the response (the response now will be sent by the latest thread). This way the thread is free to receive new responses.
An example of asynchronous support:
Servlets 2.5:
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 // ... } }
Servlets 3:
@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. // ... } }
The interface AsyncContext
also has methods to get the request object, response object and add listeners to notify them when a process has finished.
In servlets 3, the interface HttpServletRequest
has been added two new methods: login(username, password)
and logout()
.
For more details, have a look at the Java EE 6 API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With