Servlet 3.0

1,661 views

Published on

Published in: Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
1,661
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
75
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Servlet 3.0

  1. 1. Servlet 3.0 Minh Hoang TO Portal Team
  2. 2. Servlet 3.0 Specification - Java Specification Request JSR-315 - Part of Java Enterprise Edition 6 - Compliant servers: Apache Tomcat 7, JBoss AS 7, Jetty 8, GlassFish 3, ... www.exoplatform.com - Copyright 2012 eXo Platform 2
  3. 3. Agenda- Annotations- Web Fragment- Dynamic Registration- Programmatic Login- Asynchronous Servlet- File Upload www.exoplatform.com - Copyright 2012 eXo Platform 3
  4. 4. Annotations- Use annotations to declare servlet, filter, listener and specify metadata for thedeclared component@WebServlet@WebFilter@WebInitParam@WebListener@ServletSecurity@HttpConstraint- Servlet container detects annotated class and injects metadata into declaredcomponents at deploy time www.exoplatform.com - Copyright 2012 eXo Platform 4
  5. 5. AnnotationsDeclare servlet via deployment descriptor:<servlet> <servlet-name>hello</servlet-name> <servlet-class>com.mto.presentation.HelloServlet</servlet-class></servlet><servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/greeting/*</url-pattern></servlet-mapping><servlet-mapping> <servlet-name>hello</servlet-name><url-pattern>/sayhello/*</url-pattern></servlet-mapping> www.exoplatform.com - Copyright 2012 eXo Platform 5
  6. 6. AnnotationsDeclare servlet via annotated class@WebServlet( name = “hello”, urlPatterns = {“/greeting/*“, “/sayhello/*“})public class HelloServlet extends HttpServlet {} www.exoplatform.com - Copyright 2012 eXo Platform 6
  7. 7. AnnotationsDeclare filter via deployment descriptor:<filter> <filter-name>hello</filter-name> <filter-class>com.mto.presentation.HelloFilter</filter-class></filter><filter-mapping> <filter-name>hello</filter-name> <url-pattern>/greeting/*</url-pattern></filter-mapping><filter-mapping> <filter-name>hello</filter-name><url-pattern>/sayhello/*</url-pattern></filter-mapping> www.exoplatform.com - Copyright 2012 eXo Platform 7
  8. 8. AnnotationsDeclare filter via annotated class@WebFilter( name = “hello”, urlPatterns = {“/greeting/*“, “/sayhello/*“})public class HelloFilter implements Filter {} www.exoplatform.com - Copyright 2012 eXo Platform 8
  9. 9. Annotations- Type safe and less error-prone- Facilitate work of developers- No need to master XSD of deployment descriptorBUT- Frequent recompilation is a big obstacle www.exoplatform.com - Copyright 2012 eXo Platform 9
  10. 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. 11. Web FragmentFilter 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. 12. Web FragmentMETA-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. 13. Web FragmentMETA-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. 14. Web Fragment- Base to implement pluggable/extensible configurationBUT- Quite limit as relevant .jar artifacts must be under WEB-INF/lib www.exoplatform.com - Copyright 2012 eXo Platform 14
  15. 15. Dynamic Registration- Inject dynamically components, callbacks to ServletContext object on startingthe 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 tocontainers (WCI project)- Doable in a portable manner with Servlet 3.0 www.exoplatform.com - Copyright 2012 eXo Platform 15
  16. 16. Dynamic Registration- As container starts, it detects all implementations of ServletContainerInitializerthanks to java.util.ServiceLoader mechanism, then invokes the method onStartupon each initializing ServletContextpublic 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. 17. Dynamic Registration- Simple implementation for dynamic registrationpublic 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. 18. Dynamic Registration- Portable code for dynamic registrationBUT- Class loading issues must be handled gracefully- Container detects ServletContainerInitializer via service loader, so the .jarcontaining ServletContainerInitializer must be visible to container s bootstrapclass loader www.exoplatform.com - Copyright 2012 eXo Platform 18
  19. 19. Programmatic Login- Prior to Servlet 3.0, any authentication flow must pass through a redirect requestwith predefined params 1. j_security_check 2. j_username 3. j_passwordContainer 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 singlerequest www.exoplatform.com - Copyright 2012 eXo Platform 19
  20. 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 ServletAPI) is not scalable as request handling thread is not released during requestlifecycle.- In Servlet 3.0, request handling thread could be released before the end ofrequest lifecycle www.exoplatform.com - Copyright 2012 eXo Platform 20
  21. 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. 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. 23. Asynchronous Servlet- Scalable threading modelBUT- Header fields of HTTP protocol results in unnecessary network throughput- WebSocket – better solution is going to appear in future version of ServletSpecification www.exoplatform.com - Copyright 2012 eXo Platform 23
  24. 24. File Upload@WebServlet( name = “uploadServlet”, urlPatterns = {“/uploadFile/*”})@MultipartConfigpublic 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
  25. 25. File Upload <form action=”/uploadFile/ method=”post”> <input type=”file” name=”part_name”/> <input type=”submit” value=”Upload”/> </form> www.exoplatform.com - Copyright 2012 eXo Platform 25
  26. 26. File Upload- Nice API for uploading filesBUT- Lack of public API to monitor upload progress www.exoplatform.com - Copyright 2012 eXo Platform 26

×