| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Using Annotations with Jetty Embedded</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, spdy, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="annotations.html" title="Chapter 12. Annotations"><link rel="prev" href="using-annotations.html" title="Working with Annotations"><link rel="next" href="jmx-chapter.html" title="Chapter 13. JMX"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small"> |
| Version: 9.2.21.v20170120</span></td><td style="width: 50%"><script type="text/javascript"> (function() { |
| var cx = '016459005284625897022:obd4lsai2ds'; |
| var gcse = document.createElement('script'); |
| gcse.type = 'text/javascript'; |
| gcse.async = true; |
| gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + |
| '//www.google.com/cse/cse.js?cx=' + cx; |
| var s = document.getElementsByTagName('script')[0]; |
| s.parentNode.insertBefore(gcse, s); |
| })(); |
| </script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Using Annotations with Jetty Embedded</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="using-annotations.html"><i class="icon-chevron-left"></i> Previous</a> </td><th width="60%" align="center">Chapter 12. Annotations<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="jmx-chapter.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at |
| <span class="website">www.webtide.com</span></a></h5><p> |
| private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... |
| scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery |
| </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="using-annotations-embedded"></a>Using Annotations with Jetty Embedded</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="using-annotations-embedded.html#d0e11053">Setting up the Classpath</a></span></dt><dt><span class="section"><a href="using-annotations-embedded.html#d0e11074">Example</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="d0e11053"></a>Setting up the Classpath</h3></div></div></div><p>You will need to place the following jetty jars onto the classpath |
| of your application. You can obtain them from the <a class="link" href="http://download.eclipse.org/jetty/stable-9/dist/" target="_top">jetty |
| distribution</a>, or the <a class="link" href="http://central.maven.org/maven2/org/eclipse/jetty/jetty-annotations" target="_top">maven |
| repository</a>:</p><script type="syntaxhighlighter" class="brush: plain;toolbar: false"> |
| <![CDATA[jetty-plus.jar |
| jetty-annotations.jar]]> |
| </script><p>You will also need the <a class="link" href="http://asm.ow2.org/" target="_top">asm</a> jar, which you can obtain from |
| the<a class="link" href="http://download.eclipse.org/jetty/orbit/" target="_top"> Jetty |
| dependencies site</a>.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="d0e11074"></a>Example</h3></div></div></div><p>Here's an example application that sets up a Jetty server, does some |
| setup to ensure that annotations are scanned and deploys a webapp that |
| uses annotations. This example also uses the @Resource annotation which |
| involves JNDI, so we would also <a class="link" href="jndi-embedded.html" title="Using JNDI with Jetty Embedded">add the |
| necessary jndi jars to the classpath</a>., and we also add in the |
| configuration classes that are responsible for JNDI (see line 19).</p><p>Here is the embedding code:</p><script type="syntaxhighlighter" class="brush: java;toolbar: false; highlight: [18,30,33,37]"> |
| <![CDATA[import org.eclipse.jetty.security.HashLoginService; |
| import org.eclipse.jetty.server.Server; |
| import org.eclipse.jetty.webapp.WebAppContext; |
| |
| /** |
| * ServerWithAnnotations |
| * |
| * |
| */ |
| public class ServerWithAnnotations |
| { |
| public static final void main(String args[]) throws Exception |
| { |
| //Create the server |
| Server server = new Server(8080); |
| |
| //Enable parsing of jndi-related parts of web.xml and jetty-env.xml |
| org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server); |
| classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration"); |
| classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration"); |
| |
| //Create a WebApp |
| WebAppContext webapp = new WebAppContext(); |
| webapp.setContextPath("/"); |
| webapp.setWar("../../tests/test-webapps/test-servlet-spec/test-spec-webapp/target/test-spec-webapp-9.0.4-SNAPSHOT.war"); |
| server.setHandler(webapp); |
| |
| //Register new transaction manager in JNDI |
| //At runtime, the webapp accesses this as java:comp/UserTransaction |
| org.eclipse.jetty.plus.jndi.Transaction transactionMgr = new org.eclipse.jetty.plus.jndi.Transaction(new com.acme.MockUserTransaction()); |
| |
| //Define an env entry with webapp scope. |
| org.eclipse.jetty.plus.jndi.EnvEntry maxAmount = new org.eclipse.jetty.plus.jndi.EnvEntry (webapp, "maxAmount", new Double(100), true); |
| |
| |
| // Register a mock DataSource scoped to the webapp |
| org.eclipse.jetty.plus.jndi.Resource mydatasource = new org.eclipse.jetty.plus.jndi.Resource(webapp, "jdbc/mydatasource", new com.acme.MockDataSource()); |
| |
| // Configure a LoginService |
| HashLoginService loginService = new HashLoginService(); |
| loginService.setName("Test Realm"); |
| loginService.setConfig("src/test/resources/realm.properties"); |
| server.addBean(loginService); |
| |
| |
| server.start(); |
| server.join(); |
| } |
| |
| }]]> |
| </script><p>On line 19 we add in the configuration classes responsible for |
| setting up JNDI and java:comp/env.</p><p>On line 20 we add in the configuration class that ensures |
| annotations are inspected.</p><p>On lines 30, 33 and 37 we set up some JNDI resources that we will be |
| able to reference with @Resource annotations.</p><p>With the setup above, we can create a servlet that uses annotations |
| and Jetty will honour the annotations when the webapp is deployed:</p><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[import javax.annotation.security.DeclareRoles; |
| import javax.annotation.security.RunAs; |
| import javax.servlet.ServletConfig; |
| import javax.servlet.ServletException; |
| import javax.servlet.ServletOutputStream; |
| import javax.servlet.annotation.WebInitParam; |
| import javax.servlet.annotation.WebServlet; |
| import javax.servlet.http.HttpServlet; |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| import javax.sql.DataSource; |
| import javax.transaction.UserTransaction; |
| |
| /** |
| * AnnotationTest |
| * |
| * Use servlet 3.0 annotations from within Jetty. |
| * |
| * Also uses servlet 2.5 resource injection and lifecycle callbacks |
| */ |
| |
| @RunAs("special") |
| @WebServlet(urlPatterns = {"/","/test/*"}, name="AnnotationTest", initParams={@WebInitParam(name="fromAnnotation", value="xyz")}) |
| @DeclareRoles({"user","client"}) |
| public class AnnotationTest extends HttpServlet |
| { |
| private DataSource myDS; |
| |
| @Resource(mappedName="UserTransaction") |
| private UserTransaction myUserTransaction; |
| |
| @Resource(mappedName="maxAmount") |
| private Double maxAmount; |
| |
| |
| @Resource(mappedName="jdbc/mydatasource") |
| public void setMyDatasource(DataSource ds) |
| { |
| myDS=ds; |
| } |
| |
| |
| @PostConstruct |
| private void myPostConstructMethod () |
| { |
| System.err.println("PostConstruct called"); |
| } |
| |
| |
| @PreDestroy |
| private void myPreDestroyMethod() |
| { |
| System.err.println("PreDestroy called"); |
| } |
| |
| public void init(ServletConfig config) throws ServletException |
| { |
| super.init(config); |
| } |
| |
| |
| public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| { |
| doGet(request, response); |
| } |
| |
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| { |
| try |
| { |
| response.setContentType("text/html"); |
| ServletOutputStream out = response.getOutputStream(); |
| out.println("<html>"); |
| out.println("<body>"); |
| out.println("<h1>Results</h1>"); |
| out.println(myDS.toString()); |
| out.println("<br/>"); |
| out.println(maxAmount.toString()); |
| out.println("</body>"); |
| out.println("</html>"); |
| out.flush(); |
| } |
| catch (Exception e) |
| { |
| throw new ServletException(e); |
| } |
| } |
| } |
| |
| ]]> |
| </script><p></p></div></div><script type="text/javascript"> |
| SyntaxHighlighter.all() |
| </script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="using-annotations.html"><i class="icon-chevron-left"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="annotations.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="jmx-chapter.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Working with Annotations </td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top"> Chapter 13. JMX</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout"> |
| See an error or something missing? |
| <span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at |
| <span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2015-03-13T11:47:51-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript"> |
| var _gaq = _gaq || []; |
| _gaq.push(['_setAccount', 'UA-1149868-7']); |
| _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></body></html> |