{"id":1015,"date":"2018-05-07T09:13:32","date_gmt":"2018-05-07T09:13:32","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1015"},"modified":"2024-12-21T03:29:05","modified_gmt":"2024-12-21T03:29:05","slug":"how-to-create-a-sample-servlet-using-eclipse","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-create-a-sample-servlet-using-eclipse\/","title":{"rendered":"How to create a Sample Servlet using Eclipse?"},"content":{"rendered":"<h2>How to create a Sample Servlet using Eclipse?<\/h2>\r\n<p>Let us create a sample servlet and deploy it on the Tomcat server. First of all, create a Web Project in Eclipse. For instructions for creating a web project in eclipse, please go through the following question.<\/p>\r\n<p>(<a href=\"http:\/\/www.testingdocs.com\/questions\/how-to-deploy-a-war-file-in-tomcat-using-eclipse\/\"> http:\/\/www.testingdocs.com\/questions\/how-to-deploy-a-war-file-in-tomcat-using-eclipse\/<\/a> )<\/p>\r\n<p>&nbsp;<\/p>\r\n<h3><strong>Create Sample Servlet<\/strong><\/h3>\r\n<p>Right-click the source folder and choose <strong>New &gt;&gt; Servlet<\/strong> as shown in below picture.<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-867\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Create-Sample-Servlet.jpeg\" alt=\"Create Sample Servlet\" width=\"1365\" height=\"698\" title=\"\"><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>Give the package name and the Servlet class name of your choice.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-868\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Create-Servlet-Window.jpeg\" alt=\"Create Servlet Window\" width=\"1365\" height=\"650\" title=\"\"><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>In the next wizard screen, enter servlet deployment descriptor-specific information.<\/p>\r\n<h3>Create Servlet<\/h3>\r\n<p>&nbsp;<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Servlet-Deployment-Descriptor-Information.jpeg\" alt=\"Servlet Deployment Descriptor Information\" width=\"858\" height=\"666\" title=\"\"><\/p>\r\n<p>In the next screen, choose the auto-generated method stubs. For simple servlets like ours <strong>doGet<\/strong> and <strong>doPost<\/strong> would suffice. Click on the Finish button to complete the process.<\/p>\r\n<p>Give some implementation in the method stubs. Let us just say hello world and tell visitor to visit the website for QA articles as shown :<\/p>\r\n<p>&nbsp;<\/p>\r\n<pre>protected void doGet(HttpServletRequest request, HttpServletResponse response)<br \/>        throws ServletException, IOException {<br \/>    \/\/ TODO Auto-generated method stub<br \/>   response.getWriter().append(\"Served at: \").append(request.getContextPath()); {<br \/>       response.setContentType(\"text\/html\");<br \/>       PrintWriter out = response.getWriter();<br \/>       out.println(\"&lt;html&gt;\");<br \/>       out.println(\"&lt;head&gt;\");<br \/>       out.println(\"&lt;title&gt;Hello World!&lt;\/title&gt;\");<br \/>       out.println(\"&lt;\/head&gt;\");<br \/>       out.println(\"&lt;body&gt;\");<br \/>       out.println(\"&lt;h1&gt;Hello World!&lt;\/h1&gt;\");<br \/>       out.println(\"Visit www.TestingDocs.com for interesting QA stuff..!!\");<br \/>       out.println(\"&lt;\/body&gt;\");<br \/>       out.println(\"&lt;\/html&gt;\");<br \/>   }<br \/>}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>Save the changes and deploy the application in the Tomcat server as shown in the above link.<\/p>\r\n<p>Note that trying to visit the URL before deploying the servlet, would display 404 not found error.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-870\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/goGet-method-stub.jpeg\" alt=\"goGet method stub\" width=\"1365\" height=\"696\" title=\"\"><\/p>\r\n<h2>Run the Servlet<\/h2>\r\n<p>Visit the servlet URL or run the servlet on the server :<\/p>\r\n<ul>\r\n<li><strong>http:\/\/localhost:8080\/MyWebProject\/HelloWorldExample<\/strong><\/li>\r\n<\/ul>\r\n<p>&nbsp;<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-875\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Servlet-Running-on-Tomcat.jpeg\" alt=\"Servlet Running on Tomcat\" width=\"1365\" height=\"723\" title=\"\"><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>You are done with creating, deploying, and running a sample Servlet in the Tomcat server using Eclipse.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>How to create a Sample Servlet using Eclipse? Let us create a sample servlet and deploy it on the Tomcat server. First of all, create a Web Project in Eclipse. For instructions for creating a web project in eclipse, please go through the following question. ( http:\/\/www.testingdocs.com\/questions\/how-to-deploy-a-war-file-in-tomcat-using-eclipse\/ ) &nbsp; Create Sample Servlet Right-click the source [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[22],"class_list":["post-1015","post","type-post","status-publish","format-standard","hentry","category-java","tag-eclipse","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1015"}],"version-history":[{"count":9,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1015\/revisions"}],"predecessor-version":[{"id":26501,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1015\/revisions\/26501"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}