4. Annotations
- Use annotations to declare servlet, filter, listener and specify metadata for the
declared component
@WebServlet
@WebFilter
@WebInitParam
@WebListener
@ServletSecurity
@HttpConstraint
- Servlet container detects annotated class and injects metadata into declared
components at deploy time
www.exoplatform.com - Copyright 2012 eXo Platform 4
8. Annotations
Declare filter via annotated class
@WebFilter
(
name = “hello”,
urlPatterns = {“/greeting/*“, “/sayhello/*“}
)
public class HelloFilter implements Filter {
}
www.exoplatform.com - Copyright 2012 eXo Platform 8
9. Annotations
- Type safe and less error-prone
- Facilitate work of developers
- No need to master XSD of deployment descriptor
BUT
- Frequent recompilation is a big obstacle
www.exoplatform.com - Copyright 2012 eXo Platform 9
10. Web Fragment
- Divide WEB-INF/web.xml into multiples META-INF/web-fragment.xml :
1. Each web-fragment.xml is packed in a .jar artifact under WEB-INF/lib
2. Web application components could be declared in web-fragment.xml
3. Order of fragment processing is manageable
www.exoplatform.com - Copyright 2012 eXo Platform 10
11. Web Fragment
Filter configuration in a web.xml
<filter><filter-name>filterA</filter-name><filter-class>FilterA</filter-class></filter>
<filter><filter-name>filterB</filter-name><filter-class>FilterB</filter-class></filter>
<filter-mapping>
<filter-name>filterA</filter-name>
<url-pattern>/testWebFragment/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filterB</filter-name>
<url-pattern>/testWebFragment/*</url-pattern>
</filter-mapping>
www.exoplatform.com - Copyright 2012 eXo Platform 11
12. Web Fragment
META-INF/web-fragment.xml under filterA.jar
<web-fragment>
<name>A</name>
<filter><filter-name>filterA</filter-name><filter-class>FilterA</filter-class></filter>
<filter-mapping>
<filter-name>filterA</filter-name>
<url-pattern>/testWebFragment/*</url-pattern>
</filter-mapping>
<ordering>
<before>
<others/>
</before>
</ordering>
</web-fragment>
www.exoplatform.com - Copyright 2012 eXo Platform 12
13. Web Fragment
META-INF/web-fragment.xml under filterB.jar
<web-fragment>
<name>B</name>
<filter><filter-name>filterB</filter-name><filter-class>FilterB</filter-class></filter>
<filter-mapping>
<filter-name>filterB</filter-name>
<url-pattern>/testWebFragment/*</url-pattern>
</filter-mapping>
<ordering>
<after>
<name>A</name>
</after>
</ordering>
</web-fragment>
www.exoplatform.com - Copyright 2012 eXo Platform 13
14. Web Fragment
- Base to implement pluggable/extensible configuration
BUT
- Quite limit as relevant .jar artifacts must be under WEB-INF/lib
www.exoplatform.com - Copyright 2012 eXo Platform 14
15. Dynamic Registration
- Inject dynamically components, callbacks to ServletContext object on starting
the context
Use cases:
1. Inject a PortletProducerServlet servlet to ServletContext object of each web-
application containing WEB-INF/portlet.xml
2. Register callbacks for deploy/undeploy events on web applications containing
gatein-resources.xml
- Prior to Servlet 3.0, dynamic registration has to handler native work to
containers (WCI project)
- Doable in a portable manner with Servlet 3.0
www.exoplatform.com - Copyright 2012 eXo Platform 15
16. Dynamic Registration
- As container starts, it detects all implementations of ServletContainerInitializer
thanks to java.util.ServiceLoader mechanism, then invokes the method onStartup
on each initializing ServletContext
public interface ServletContainerInitializer
{
public void onStartup(Set<Class<?>> c, ServletContext ctx);
}
- Complex dynamic registration could be plugged to onStartup entry point
www.exoplatform.com - Copyright 2012 eXo Platform 16
17. Dynamic Registration
- Simple implementation for dynamic registration
public SimpleInitializer implements ServletContainerInitializer{
public void onStartup(Set<Class<?>> c, ServletContext ctx)
{
if(“/sample”.equals(ctx.getContextPath())
{
//Code handling ClassLoader elided from example
Class clazz = ctx.getClassLoader().loadClass(“abc.DynamicServlet”);
ServletRegistration.Dynamic reg = ctx.addServlet(“dynamicServlet”, clazz);
reg.addMapping(“/dynamic/*”);
}
}
}
- Declare fully-qualified name of implementation class in META-
INF/services/javax.servlet.ServletContainerInitializer
www.exoplatform.com - Copyright 2012 eXo Platform 17
18. Dynamic Registration
- Portable code for dynamic registration
BUT
- Class loading issues must be handled gracefully
- Container detects ServletContainerInitializer via service loader, so the .jar
containing ServletContainerInitializer must be visible to container 's bootstrap
class loader
www.exoplatform.com - Copyright 2012 eXo Platform 18
19. Programmatic Login
- Prior to Servlet 3.0, any authentication flow must pass through a redirect request
with predefined params
1. j_security_check
2. j_username
3. j_password
Container intercepts that request and delegates to JAAS
- From Servlet 3.0
request.login(“root”, “gtn”);
The whole authentication process could be managed in the scope of a single
request
www.exoplatform.com - Copyright 2012 eXo Platform 19
20. Asynchronous Servlet
- Use in server-push communication model:
1. Client sends request for real time data such as: livescore, exchange rate
2. Server queues the request as there is no event on real time data
3. As there is update on real time data, server loops through queued request
and respond to client
- Prior to Servlet 3.0, implementation of such communication model (using Servlet
API) is not scalable as request handling thread is not released during request
lifecycle.
- In Servlet 3.0, request handling thread could be released before the end of
request lifecycle
www.exoplatform.com - Copyright 2012 eXo Platform 20
21. Asynchronous Servlet
@WebServlet(
name=”livescore”,
asyncSupported=true,
urlPatterns = {“/livescore/*”}
)
public class LivescoreServlet extends HttpServlet
{
//Synchronization code elided from example
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
AsyncContext ctx = req.startAsync(req, res);
//Thread handling request is released here but the res object is still alive
queue.add(ctx);
}
//Typically event from a messaging service
public void onServerEvent(EventObject obj)
{
//Loop over queue and send response to client
}
}
www.exoplatform.com - Copyright 2012 eXo Platform 21
22. Asynchronous Servlet
<script type=”text/javascript”>
var url = //url intercepted by livescore servlet
var updateScore = function()
{
$.getJSON(url, function(data)
{
//Update HTML with received data
setTimeout(updateScore, 1000);
});
};
updateScore();
</script>
www.exoplatform.com - Copyright 2012 eXo Platform 22
23. Asynchronous Servlet
- Scalable threading model
BUT
- Header fields of HTTP protocol results in unnecessary network throughput
- WebSocket – better solution is going to appear in future version of Servlet
Specification
www.exoplatform.com - Copyright 2012 eXo Platform 23
24. File Upload
@WebServlet(
name = “uploadServlet”, urlPatterns = {“/uploadFile/*”}
)
@MultipartConfig
public class UploadServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
Collection<Part> parts = req.getParts();
for(Part part : parts)
{
….......
part.write(System.getProperty(“java.io.tmpdir”) + “/” + part.getName());
….......
}
}
}
www.exoplatform.com - Copyright 2012 eXo Platform 24
26. File Upload
- Nice API for uploading files
BUT
- Lack of public API to monitor upload progress
www.exoplatform.com - Copyright 2012 eXo Platform 26