How to create a Servlet in Eclipse IDE

Eclipse is an open-source IDE for developing JavaSE and JavaEE (J2EE) applications. We can download the eclipse IDE from http://www.eclipse.org/downloads/. We need to download the eclipse ide for JavaEE developers.

To develop a Servlet, we have to follow the following steps:

Step-1: First click on File Menu -> New -> dynamic web project -> write your project name e.g. test -> then click next.

Step-2: Check Generate web.xml Deployment Descriptor and click Finish

img

Step-3: Now, the complete directory structure of your Project will be automatically created by Eclipse IDE.

img

Step-4: Click on test project, go to Java Resources -> src. Right click on src select New -> Servlet

Step-5:Give Servlet class name and click Next


img

Step-6: Then click next, then choose doGet() or doPost(), then click finish button

img

img

Step-7:Now our Servlet is created, we will write some code inside it.


img

Step-8: then we will write web.xml(Deployment Descriptor file).

Dear frnds, here i want to inform web.xml file plays a very crucial role for execution of a servlet. So let’s know the detail first:

In Java web-based application, we use a web.xml file which is called as deployment descriptor file which represents how URL is mapping with Servlet. This file is under the WEB-INF/ directory. web.xml which is part of the Servlet standard for web applications. The file is an XML file whose root element is . A web application's deployment descriptor describes the classes, resources and configuration of the application and how the web server uses them to serve web requests.


<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
    < servlet-name> abc </servlet-name>
    <servlet-class> javarace.MyServlet </servlet-class>
</servlet>
    <servlet-mapping>
        <servlet-name> abc </servlet-name>
        <url-pattern> /MyServlet </url-pattern>
    </servlet-mapping>
</web-app>

Step-9: then we will execute the servlet, i mean we will start the server and deploy the project. For starting the server and deploying the project in one step, Right click on your project -> Run As -> Run on Server -> choose tomcat server -> next -> addAll -> finish.

Now tomcat server has been started and project is deployed. To access the servlet write the url pattern name in the URL bar of the browser. In this case we will put MyServlet then enter. Then we will get the output Welcome 2 JavaRace


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext