{"id":1959,"date":"2011-04-13T15:23:55","date_gmt":"2011-04-13T09:53:55","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1959"},"modified":"2011-04-13T15:23:55","modified_gmt":"2011-04-13T09:53:55","slug":"create-servlet-using-annotation","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/create-servlet-using-annotation\/","title":{"rendered":"Create Servlet using Annotation &#8211; Servlet 3.0"},"content":{"rendered":"<p><a title=\"Version table of J2EE\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/servlet\/j2ee-servlet-jsp-jsf-and-jms-version-table\/\" target=\"_blank\">Before reading this article, i will suggest readers to go through the version table of J2EE.<\/a><\/p>\n<p>One of the most appraised feature of Servlet 3.0 is the support of the annotations to create the Servlet. Before\u00a0 this the Deployment Descriptor (web.xml) was used to create the servlet mapping , defining init parameters etc. All the settings can be now achieved without any entry in web.xml.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Prerequisite:<\/strong><\/span><\/p>\n<ol>\n<li>JDK 5.0 and above.<\/li>\n<li>Tomcat 7<\/li>\n<li>Eclipse 3.6 &#8211; Helios (as of now, it only supports Tomcat 7)<\/li>\n<\/ol>\n<p>We will start with creating the Servlet class :<!--more--><\/p>\n<pre class=\"brush: java; highlight: [13,14,15,16,17,18,19,20,21]; title: ; notranslate\" title=\"\">\npackage com.g2.servlet;\n\nimport java.io.IOException;\nimport java.io.PrintWriter;\n\nimport javax.servlet.ServletException;\nimport javax.servlet.annotation.WebInitParam;\nimport javax.servlet.annotation.WebServlet;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\n\n@WebServlet(\n\t\tname=&quot;TestServlet&quot;,\n\t\turlPatterns={&quot;\/test&quot;,&quot;\/testServlet&quot;},\n\t\tinitParams={\n\t\t\t\t@WebInitParam(name=&quot;db&quot;,value=&quot;Oracle&quot;),\n\t\t\t\t@WebInitParam(name=&quot;jdk&quot;,value=&quot;6&quot;),\n\t\t\t\t@WebInitParam(name=&quot;tomcat&quot;,value=&quot;7&quot;)\n\t\t\t\t}\n\t\t\t)\npublic class ServletUsingAnnotation extends HttpServlet{\n\n\t@Override\n\tprotected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {\n\t\tPrintWriter pw = res.getWriter();\n\n\t\tres.setContentType(&quot;text\/html&quot;);\n\t\tpw.write(&quot;Running Servlet using Annotation..&quot;);\n\t\tpw.write(&quot;&lt;br \/&gt;&quot;);\n\t\tpw.write(&quot;-- Init parameters --&quot;);\n\t\tpw.write(&quot;&lt;br \/&gt;&quot;);\n\t\tpw.write(&quot;db : &quot;+this.getInitParameter(&quot;db&quot;));\n\t\tpw.write(&quot;&lt;br \/&gt;&quot;);\n\t\tpw.write(&quot;jdk : &quot;+this.getInitParameter(&quot;jdk&quot;));\n\t\tpw.write(&quot;&lt;br \/&gt;&quot;);\n\t\tpw.write(&quot;tomcat : &quot;+this.getInitParameter(&quot;tomcat&quot;));\n\t}\n}\n<\/pre>\n<p>As you can see in above code, we have used annotation &#8220;<strong>@WebServlet<\/strong>&#8221; to declare this class as servlet. to pass the init parameter to servlet, we have used &#8220;<strong>@WebInitParam<\/strong>&#8220;. Before Servlet 2.5 we were not able to specify multiple url for the same servlet. As you can see in above code, we have given two URLs for the same servlet.<\/p>\n<p>Create &#8220;index.jsp&#8221; page :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;%@ page language=&quot;java&quot; contentType=&quot;text\/html; charset=ISO-8859-1&quot;\n    pageEncoding=&quot;ISO-8859-1&quot;%&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=ISO-8859-1&quot;&gt;\n&lt;title&gt;Creating servlet using Annotation&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;a href=&quot;..\/test&quot;&gt;Servlet using Annotation&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>After clicking on hyperlink on page &#8220;index.jsp&#8221; Servlet will open with following output.<\/p>\n<blockquote><p>Running Servlet using Annotation..<br \/>\n&#8212; Init parameters &#8212;<br \/>\ndb : Oracle<br \/>\njdk : 6<br \/>\ntomcat : 7<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Create the Servlet without any deployment descriptor. Using Annotations<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[125],"class_list":["post-1959","post","type-post","status-publish","format-standard","hentry","category-servlet","tag-j2ee"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1482,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/how-container-handles-the-servlet-request\/","url_meta":{"origin":1959,"position":0},"title":"How container handles the Servlet request","author":"Jitendra","date":"February 12, 2011","format":false,"excerpt":"How container handles the Servlet request or How the apache Tomcat works","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"Client Browse Servlet URL","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Client-Browse-Servlet-URL.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1467,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/difference-between-servletcontext-and-servletconfig\/","url_meta":{"origin":1959,"position":1},"title":"Difference between servletContext and ServletConfig","author":"Jitendra","date":"February 8, 2011","format":false,"excerpt":"What is difference between servletContext and ServletConfig?","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1586,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/read-file-present-inside-java-and-j2ee-project\/","url_meta":{"origin":1959,"position":2},"title":"Read File Present inside Java and J2EE Project","author":"Jitendra","date":"February 24, 2011","format":false,"excerpt":"How to Read File Present inside Java and J2EE Project","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"Read File Present inside Java and J2EE Project","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Read-File-Present-inside-Java-and-J2EE-Project.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1464,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/what-is-preinitialization-of-servlet\/","url_meta":{"origin":1959,"position":3},"title":"What is preinitialization of servlet","author":"Jitendra","date":"February 8, 2011","format":false,"excerpt":"What is preinitialization of servlet","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1460,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/setting-session-timeout-in-jsp-servlet\/","url_meta":{"origin":1959,"position":4},"title":"Setting Session Timeout in JSP Servlet","author":"Jitendra","date":"February 7, 2011","format":false,"excerpt":"How to set the Session Time out in JSP and Servlet","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1494,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/life-cycle-of-servlet\/","url_meta":{"origin":1959,"position":5},"title":"Life Cycle of Servlet","author":"Jitendra","date":"February 12, 2011","format":false,"excerpt":"Explain Life Cycle of Servlet","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"Servlet Life Cycle","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Servlet-Life-Cycle.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1959"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1959\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}