{"id":15201,"date":"2013-07-05T16:00:29","date_gmt":"2013-07-05T13:00:29","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=15201"},"modified":"2014-01-29T15:15:34","modified_gmt":"2014-01-29T13:15:34","slug":"java-ee-ejb-interceptors-tutorial-and-example","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html","title":{"rendered":"Java EE EJB Interceptors tutorial and example"},"content":{"rendered":"<p>In this example we are going to see how to use Interceptors in an EJB and test it using a simple Web Application.<\/p>\n<h2>1. Introduction<\/h2>\n<p>Interceptors are used, as the name suggests, when you want to intercept calls to EJB methods. If you declare an Interceptor for a Bean, every time a method of that Bean is invoked, it will be intercepted with one method of the Interceptor. That means that the execution goes straight to the Interceptor&#8217;s method. The intercepting method then, can decide whether to call the intercepted EJB method or simply replace it.<\/p>\n<p>You might find the above behavior resembling the Aspect Oriented Programming philosophy, and you&#8217;d be correct. Despite the fact that the implementation of the two technologies is completely different, the truth is they can be used for the same purposes. For example, when you want to log something before of after a Beans method is executed. Or when you want to enforce a specific policy concerning method calls, e.g. authentication, input checking etc. Of course an EJB can have a chain of Interceptors that will intercept the method in a specific order.<\/p>\n<p>In this example we are going to create an EAR Project and an EJB Project that will host our EJBs and Interceptors and a Dynamic Web Application that will host a Servlet to test the aforementioned behavior. We are going to use Eclipse Java EE IDE 4,3 Kepler and Glassfish 4.0 as our container.<\/p>\n<h2>2. Create a new Enterprise Application Project<\/h2>\n<p>Create a new Enterprise Application Project named\u00a0<code>EJBInterceptorEAR<\/code>\u00a0.In Eclipse IDE select File -&gt; New -&gt; Enterprise Application Project and fill in the form and click Finish:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-ear-project.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20496\" alt=\"new-ear-project\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-ear-project.png\" width=\"464\" height=\"550\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-ear-project.png 464w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-ear-project-253x300.png 253w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/a><\/p>\n<h2>3. Create a new EJB Projet<\/h2>\n<p>Create a new EJB Project called <code>InterceptorsEJB<\/code>. We are going to create our session bean on this. Go to File -&gt; New -&gt; EJB Project and fill out the form. Be careful to select \u201cAdd EAR Project\u201d and Select \u201c<code>EJBInterceptorEAR<\/code>\u201d as EAR project name:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-ejb-projecct.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20497\" alt=\"new-ejb-projecct\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-ejb-projecct.png\" width=\"516\" height=\"618\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-ejb-projecct.png 516w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-ejb-projecct-250x300.png 250w\" sizes=\"(max-width: 516px) 100vw, 516px\" \/><\/a><\/p>\n<p>Click Next twice and choose to create EJB Client JAR, as well as to generate the\u00a0<code>ejb-jar.xml<\/code>\u00a0deployment descriptor :<\/p>\n<h2>4. Create a simple Interceptor class<\/h2>\n<p>We are going to define a simple Interceptor, which has only one method. In <code>InterceptorsEJB<\/code> project under <code>ejbModule<\/code> folder create a new package called <code>com.javacodegeeks.enterprise.ejb.interceptor<\/code> and create the following class:<\/p>\n<p><em><span style=\"text-decoration: underline;\">SimpleInterceptor.java:<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.enterprise.ejb.interceptor;\r\n\r\nimport javax.interceptor.AroundInvoke;\r\nimport javax.interceptor.InvocationContext;\r\n\r\npublic class SimpleInterceptor {\r\n\r\n\t@AroundInvoke\r\n\tpublic Object intercept(InvocationContext context) throws Exception {\r\n\r\n\t\tSystem.out.println(\"SimpleInterceptor - Logging BEFORE calling method :\"+context.getMethod().getName() );\r\n\r\n\t\tObject result = context.proceed();\r\n\r\n\t\tSystem.out.println(\"SimpleInterceptor - Logging AFTER calling method :\"+context.getMethod().getName() );\r\n\r\n\t\treturn result;\r\n\t}\r\n}<\/pre>\n<p>Note that <code>public Object intercept<\/code> in annotated with <code>@AroundInvoke<\/code>. This means that this specific method will intercept on the EJB method call. It is important to address the the fact that an interceptor class can have any number of methods, but only one can be annotated with <code>@AroundInvoke<\/code>.<\/p>\n<p>You can use the <code><a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/interceptor\/InvocationContext.html\">InvocationContext<\/a><\/code> argument of the <code>intercept<\/code> method for two purposes. You can either extract useful information concerning the EJB method that is being intercepted (for example we used <code>getMethod().getName()<\/code> API call chain to obtain the name of the intercepted method), or you can continue the execution using <code>proceed()<\/code> API method. This method will switch the execution flow to the next interceptor in the chain, or to the actual intercepted EJB method, if there are no interceptors left in the chain. This method will return the result of the EJB method call. But we don&#8217;t know the returning type, thus <code>proceed()<\/code> returns an <code>Object<\/code> instance. If you do know the returning type of the EJB method you can cast the result of <code>proceed()<\/code> to that particular type, and then use that instance as you wish. Notice that <code>intercept<\/code> method also returns the result of the actual EJB call. This will be either passed to the next interceptor in the Interceptor chain, or to the client if there is no other interceptors left.<\/p>\n<p>So, any business logic you want to execute before calling the actual EJB method should be placed before calling <code>proceed()<\/code>. Consequently, you put the code you want to execute after the execution of the actual EJB method, after calling <code>proceed()<\/code>. Of course you could bypass the normal execution of the EJB method all together if you want.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h2>4. Create a simple EJB<\/h2>\n<p>Here is the EJB that will use the above Interceptor to intercept it&#8217;s methods. In <code>InterceptorsEJB<\/code> project under <code>ejbModule<\/code> folder create a new package called <code>com.javacodegeeks.enterprise.ejb<\/code> and create the following class:<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.enterprise.ejb;\r\n\r\nimport javax.ejb.Stateless;\r\nimport javax.interceptor.Interceptors;\r\n\r\nimport com.javacodegeeks.enterprise.ejb.interceptor.SimpleInterceptor;\r\n\r\n@Stateless\r\n@Interceptors(SimpleInterceptor.class)\r\npublic class SimpleEJB {\r\n\r\n\tpublic String printMessage(String message) {\r\n\r\n\t\tSystem.out.println(\" Executing method : printMessage\" + message);\r\n\t\treturn \"Message is \"+message;\r\n\t}\r\n\r\n}<\/pre>\n<p>As you can see we&#8217;ve marked the class with <code>@Interceptors(SimpleInterceptor.class)<\/code> annotation. This means that all methods of this class will be intercepted by <code>SimpleInterceptor<\/code><\/p>\n<p>Let&#8217;s create a simple <code>Servlet<\/code> to test the desired functionality.<\/p>\n<h2>5. Create a new Dynamic Web Project<\/h2>\n<p>Go to File -&gt; New -&gt; Dynamic Web Project. Fill out the form and make sure you check \u201cAdd project to an EAR\u201d and put <code>EJBInterceptorEAR<\/code> as the \u201cEAR project name\u201d:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-dynamic-web-application.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20500\" alt=\"new-dynamic-web-application\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-dynamic-web-application.png\" width=\"523\" height=\"620\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-dynamic-web-application.png 523w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-dynamic-web-application-253x300.png 253w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/a><\/p>\n<p>After clicking \u201cFinish\u201d, go to the project Explorer and Right click on the Project\u00a0<code>InterceptorTesting<\/code>\u00a0and go to Properties-&gt; Deployment Assembly -&gt; Add -&gt; Porject -&gt; <code>EJBInterceptorEAR<\/code>\u00a0:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/deployment-assembly.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20501\" alt=\"deployment-assembly\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/deployment-assembly.png\" width=\"774\" height=\"405\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/deployment-assembly.png 774w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/deployment-assembly-300x156.png 300w\" sizes=\"(max-width: 774px) 100vw, 774px\" \/><\/a><\/p>\n<h2>6. Create new \u00a0Servlet<\/h2>\n<p><span style=\"line-height: 1.5em;\">Go to\u00a0<\/span><code style=\"line-height: 1.5em;\">InterceptorTesting<\/code><span style=\"line-height: 1.5em;\">\u00a0Web project and create a new Servlet named\u00a0<\/span><code style=\"line-height: 1.5em;\">TestSerlvet<\/code><span style=\"line-height: 1.5em;\">:<\/span><\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-serlvet.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20502\" alt=\"new-serlvet\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-serlvet.png\" width=\"477\" height=\"428\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-serlvet.png 477w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-serlvet-300x269.png 300w\" sizes=\"(max-width: 477px) 100vw, 477px\" \/><\/a><\/p>\n<p>Let&#8217;s see the code of that Servlet:<\/p>\n<p><em><span style=\"text-decoration: underline;\">TestServlet.java:<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.enterprise.servlet;\r\n\r\nimport java.io.IOException;\r\n\r\nimport javax.naming.InitialContext;\r\nimport javax.naming.NamingException;\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.annotation.WebServlet;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\nimport com.javacodegeeks.enterprise.ejb.SimpleEJB;\r\n\r\n@WebServlet(\"\/TestSerlvet\")\r\npublic class TestSerlvet extends HttpServlet {\r\n\tprivate static final long serialVersionUID = 1L;\r\n\r\n\tpublic TestSerlvet() {\r\n\t\tsuper();\r\n\r\n\t}\r\n\r\n\tprotected void doGet(HttpServletRequest request,\r\n\t\t\tHttpServletResponse response) throws ServletException, IOException {\r\n\t\tSystem.out.println(\"Hello from Servlet\");\r\n\r\n\t\tInitialContext ic;\r\n\t\tSimpleEJB bean;\r\n\r\n\t\tString message = request.getParameter(\"printMessage\");\r\n\r\n\t\tif (message != null) {\r\n\r\n\t\t\ttry {\r\n\r\n\t\t\t\tic = new InitialContext();\r\n\t\t\t\tbean = (SimpleEJB) ic\r\n\t\t\t\t\t\t.lookup(\"java:global\/EJBInterceptorEAR\/InterceptorTesting\/SimpleEJB!\"\r\n\t\t\t\t\t\t\t\t+ \"com.javacodegeeks.enterprise.ejb.SimpleEJB\");\r\n\r\n\t\t\t\tbean.printMessage(message);\r\n\r\n\t\t\t} catch (NamingException e) {\r\n\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p>As you can see we simple parse a <code>printMessage<\/code> query parameter and we pass its value to the <code>printMessage<\/code> method of <code>SimpleEJB<\/code>.<\/p>\n<p><em><strong>Tip:<\/strong> <span style=\"text-decoration: underline;\">If you are having trouble figuring out the Portable JNDI names for EJB PassivationObject look at the logs or output of Glassfish when deploying the project and you will find a line like this :2014-01-09T15:14:14.627+0200|INFO: EJB5181:Portable JNDI names for EJB SimpleEJB:java:global\/EJBInterceptorEAR\/InterceptorTesting\/SimpleEJB!<br \/>\ncom.javacodegeeks.enterprise.ejb.SimpleEJB,java:global\/EJBInterceptorEAR\/InterceptorTesting\/SimpleEJB)<\/span><\/em><\/p>\n<h2>7. Test<\/h2>\n<p>You can deploy your Application on Glassfish and issue the following request:<\/p>\n<pre class=\"brush:bash\">http:\/\/localhost:8080\/InterceptorTesting\/TestSerlvet?printMessage=Hello%20From%20JCG<\/pre>\n<p>If you watch the output of Glassfish on the console you will see:<\/p>\n<pre style=\"background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;\"><code style=\"color: black; word-wrap: normal;\">2014-01-09T17:43:14.356+0200|INFO: Hello from Servlet\r\n2014-01-09T17:43:14.357+0200|INFO: Logging BEFORE calling method :printMessage\r\n2014-01-09T17:43:14.357+0200|INFO: Executing method : printMessage : Hello From JCG\r\n2014-01-09T17:43:14.357+0200|INFO: Logging AFTER calling method :printMessage\r\n<\/code><\/pre>\n<h2>8. Multiple Interceptors<\/h2>\n<p>Go ahead and create another new Interceptor in <code>InterceptorsEJB<\/code> project under <code>com.javacodegeeks.enterprise.ejb.interceptor<\/code> package.<\/p>\n<p>Here it is :<\/p>\n<p><em><span style=\"text-decoration: underline;\">SecondInterceptor.java:<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.enterprise.ejb.interceptor;\r\n\r\nimport javax.interceptor.AroundInvoke;\r\nimport javax.interceptor.InvocationContext;\r\n\r\npublic class SecondInterceptor {\r\n\r\n\t@AroundInvoke\r\n\tpublic Object intercept(InvocationContext context) throws Exception {\r\n\r\n\t\tSystem.out.println(\"SecondInterceptor - Logging BEFORE calling method :\"+context.getMethod().getName() );\r\n\r\n\t\tObject result = context.proceed();\r\n\r\n\t\tSystem.out.println(\"SecondInterceptor  -Logging AFTER calling method :\"+context.getMethod().getName() );\r\n\r\n\t\treturn result;\r\n\t}\r\n}<\/pre>\n<p>And here is <code>SimpleEJB<\/code>.<\/p>\n<p><em><span style=\"text-decoration: underline;\">SimpleEJB.java:<\/span><\/em><\/p>\n<pre class=\"brush:java; highlight:[10]\">package com.javacodegeeks.enterprise.ejb;\r\n\r\nimport javax.ejb.Stateless;\r\nimport javax.interceptor.Interceptors;\r\n\r\nimport com.javacodegeeks.enterprise.ejb.interceptor.SecondInterceptor;\r\nimport com.javacodegeeks.enterprise.ejb.interceptor.SimpleInterceptor;\r\n\r\n@Stateless\r\n@Interceptors({SimpleInterceptor.class, SecondInterceptor.class})\r\npublic class SimpleEJB {\r\n\r\n\tpublic String printMessage(String message) {\r\n\r\n\t\tSystem.out.println(\" Executing method : printMessage\" + message);\r\n\t\treturn \"Message is \"+message;\r\n\t}\r\n\r\n}<\/pre>\n<p>Now, if we issue the same request again:<\/p>\n<pre class=\"brush:bash\">http:\/\/localhost:8080\/InterceptorTesting\/TestSerlvet?printMessage=Hello%20From%20JCG<\/pre>\n<p>If you watch the output of Glassfish on the console you will see:<\/p>\n<pre style=\"background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;\"><code style=\"color: black; word-wrap: normal;\">2014-01-09T17:59:55.647+0200|INFO: Hello from Servlet\r\n2014-01-09T17:59:55.659+0200|INFO: SimpleInterceptor - Logging BEFORE calling method :printMessage\r\n2014-01-09T17:59:55.659+0200|INFO: SecondInterceptor - Logging BEFORE calling method :printMessage\r\n2014-01-09T17:59:55.660+0200|INFO: Executing method : printMessageHello From JCG\r\n2014-01-09T17:59:55.660+0200|INFO: SecondInterceptor  -Logging AFTER calling method :printMessage\r\n2014-01-09T17:59:55.660+0200|INFO: SimpleInterceptor  -Logging AFTER calling method :printMessage\r\n<\/code><\/pre>\n<h2>9. Method level interceptors<\/h2>\n<p>Sometimes it&#8217;s possible that you don&#8217;t want all of your bean methods to get intercepted. You can choose which methods to intercept by annotating them, and not the entire class.<\/p>\n<p>Let&#8217;s see how:<\/p>\n<p><em><span style=\"text-decoration: underline;\">SimpleEJB.java:<\/span><\/em><\/p>\n<pre class=\"brush:java; highlight:[11]\">package com.javacodegeeks.enterprise.ejb;\r\n\r\nimport javax.ejb.Stateless;\r\nimport javax.interceptor.Interceptors;\r\n\r\nimport com.javacodegeeks.enterprise.ejb.interceptor.SimpleInterceptor;\r\n\r\n@Stateless\r\npublic class SimpleEJB {\r\n\r\n\t@Interceptors(SimpleInterceptor.class)\r\n\tpublic String printMessage(String message) {\r\n\r\n\t\tSystem.out.println(\" Executing method : printMessage : \" + message);\r\n\t\treturn \"Message is \" + message;\r\n\t}\r\n\r\n\tpublic String printSomething(String message) {\r\n\r\n\t\tSystem.out.println(\" Executing method : printSomething :\" + message);\r\n\t\treturn \"Message is \" + message;\r\n\t}\r\n\r\n}<\/pre>\n<p><em><span style=\"text-decoration: underline;\">TestServlet.java:<\/span><\/em><\/p>\n<pre class=\"brush:java; highlight:[44]\">package com.javacodegeeks.enterprise.servlet;\r\n\r\nimport java.io.IOException;\r\n\r\nimport javax.naming.InitialContext;\r\nimport javax.naming.NamingException;\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.annotation.WebServlet;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\nimport com.javacodegeeks.enterprise.ejb.SimpleEJB;\r\n\r\n@WebServlet(\"\/TestSerlvet\")\r\npublic class TestSerlvet extends HttpServlet {\r\n\tprivate static final long serialVersionUID = 1L;\r\n\r\n\tpublic TestSerlvet() {\r\n\t\tsuper();\r\n\r\n\t}\r\n\r\n\tprotected void doGet(HttpServletRequest request,\r\n\t\t\tHttpServletResponse response) throws ServletException, IOException {\r\n\t\tSystem.out.println(\"Hello from Servlet\");\r\n\r\n\t\tInitialContext ic;\r\n\t\tSimpleEJB bean;\r\n\r\n\t\tString message = request.getParameter(\"printMessage\");\r\n\r\n\t\tif (message != null) {\r\n\r\n\t\t\ttry {\r\n\r\n\t\t\t\tic = new InitialContext();\r\n\t\t\t\tbean = (SimpleEJB) ic\r\n\t\t\t\t\t\t.lookup(\"java:global\/EJBInterceptorEAR\/InterceptorTesting\/SimpleEJB!\"\r\n\t\t\t\t\t\t\t\t+ \"com.javacodegeeks.enterprise.ejb.SimpleEJB\");\r\n\r\n\t\t\t\tbean.printMessage(message);\r\n\r\n\t\t\t\tbean.printSomething(\"This method is not intercepted\");\r\n\r\n\t\t\t} catch (NamingException e) {\r\n\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p>Now, if we issue the same request again:<\/p>\n<pre class=\"brush:bash\">http:\/\/localhost:8080\/InterceptorTesting\/TestSerlvet?printMessage=Hello%20From%20JCG<\/pre>\n<p>If you watch the output of Glassfish on the console you will see:<\/p>\n<pre style=\"background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;\"><code style=\"color: black; word-wrap: normal;\">2014-01-09T19:52:00.909+0200|INFO: Hello from Servlet\r\n2014-01-09T19:52:00.920+0200|INFO: SimpleInterceptor - Logging BEFORE calling method :printMessage\r\n2014-01-09T19:52:00.921+0200|INFO: Executing method : printMessage : Hello From JCG\r\n2014-01-09T19:52:00.921+0200|INFO: SimpleInterceptor  -Logging AFTER calling method :printMessage\r\n2014-01-09T19:52:00.921+0200|INFO: Executing method : printSomething :This method is not intercepted\r\n<\/code><\/pre>\n<h2>Download Eclipse Project<\/h2>\n<p>This was an example on EJB Interceptors. Download the Eclipse Projects of this tutorial :\u00a0<a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/EJBInterceptor.zip\">EJBInterceptor.zip<\/a><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/EJBPassivationActivation.zip\"><br \/>\n<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example we are going to see how to use Interceptors in an EJB and test it using a simple Web Application. 1. Introduction Interceptors are used, as the name suggests, when you want to intercept calls to EJB methods. If you declare an Interceptor for a Bean, every time a method of that &hellip;<\/p>\n","protected":false},"author":7,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[90,289],"class_list":["post-15201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-ejb","tag-java-ee6"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java EE EJB Interceptors tutorial and example<\/title>\n<meta name=\"description\" content=\"In this example we are going to see how to use Interceptors in an EJB and test it using a simple Web Application. 1. Introduction Interceptors are used,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java EE EJB Interceptors tutorial and example\" \/>\n<meta property=\"og:description\" content=\"In this example we are going to see how to use Interceptors in an EJB and test it using a simple Web Application. 1. Introduction Interceptors are used,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2013-07-05T13:00:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-01-29T13:15:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Nikos Maravitsas\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nikos Maravitsas\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html\"},\"author\":{\"name\":\"Nikos Maravitsas\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/aca65ecdda8de63ce629d2754da61f11\"},\"headline\":\"Java EE EJB Interceptors tutorial and example\",\"datePublished\":\"2013-07-05T13:00:29+00:00\",\"dateModified\":\"2014-01-29T13:15:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html\"},\"wordCount\":1038,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"EJB\",\"Java EE6\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html\",\"name\":\"Java EE EJB Interceptors tutorial and example\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2013-07-05T13:00:29+00:00\",\"dateModified\":\"2014-01-29T13:15:34+00:00\",\"description\":\"In this example we are going to see how to use Interceptors in an EJB and test it using a simple Web Application. 1. Introduction Interceptors are used,\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/07\\\/java-ee-ejb-interceptors-tutorial-and-example.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Java EE EJB Interceptors tutorial and example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/aca65ecdda8de63ce629d2754da61f11\",\"name\":\"Nikos Maravitsas\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g\",\"caption\":\"Nikos Maravitsas\"},\"description\":\"Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/nikos-maravitsas\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java EE EJB Interceptors tutorial and example","description":"In this example we are going to see how to use Interceptors in an EJB and test it using a simple Web Application. 1. Introduction Interceptors are used,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html","og_locale":"en_US","og_type":"article","og_title":"Java EE EJB Interceptors tutorial and example","og_description":"In this example we are going to see how to use Interceptors in an EJB and test it using a simple Web Application. 1. Introduction Interceptors are used,","og_url":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-07-05T13:00:29+00:00","article_modified_time":"2014-01-29T13:15:34+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Nikos Maravitsas","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Nikos Maravitsas","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html"},"author":{"name":"Nikos Maravitsas","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/aca65ecdda8de63ce629d2754da61f11"},"headline":"Java EE EJB Interceptors tutorial and example","datePublished":"2013-07-05T13:00:29+00:00","dateModified":"2014-01-29T13:15:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html"},"wordCount":1038,"commentCount":3,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["EJB","Java EE6"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html","url":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html","name":"Java EE EJB Interceptors tutorial and example","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2013-07-05T13:00:29+00:00","dateModified":"2014-01-29T13:15:34+00:00","description":"In this example we are going to see how to use Interceptors in an EJB and test it using a simple Web Application. 1. Introduction Interceptors are used,","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/07\/java-ee-ejb-interceptors-tutorial-and-example.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Java EE EJB Interceptors tutorial and example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/aca65ecdda8de63ce629d2754da61f11","name":"Nikos Maravitsas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3969f52bbf541170fe1ae074a6848e1085b8180438db323f626a436a7d19b23f?s=96&d=mm&r=g","caption":"Nikos Maravitsas"},"description":"Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/nikos-maravitsas"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/15201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=15201"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/15201\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=15201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=15201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=15201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}