How to create Hello World Servlet example using eclipse IDE with Tomcat 7?

For creating a Hello World Servlet Example using eclipse IDE with tomcat 7, you just need two things:

i) Download and install Eclipse IDE for Java EE developer.

Download Eclipse IDE: eclipse ide for java ee developer

ii) Download and install the Apache Tomcat server

Download Tomcat Server: Click here to download
After download and install Eclipse IDE for Java EE developer and Tomcat Server, you will be able to create Hello World Servlet Example using Eclipse IDE.

1) Create a Dynamic web project: 

To create Dynamic web project open eclipse ide -> Go to menu -> New -> Dynamic web project.
Hello World Servlet example using eclipse IDE


Write your project name and click on next.

Hello World Servlet example using eclipse IDE
click on next button
Hello World Servlet example using eclipse IDE
check on generate web.xml and click on finish button.
Hello World Servlet example using eclipse IDE

After clicking on the finish button, your Eclipse IDE will automatically create a dynamic web project as shown below.


Hello World Servlet example using eclipse IDE

2) Create a servlet class:

Once you create a dynamic web project your next step is to create a servlet class.  right click on your project -> New -> Servlet
Hello World Servlet example using eclipse IDE

Write your package name in java package field and class name in the class name field and click on next.


Hello World Servlet example using eclipse IDE

We can specify the URL mapping in the following screen(web.xml) and click on next.

Hello World Servlet example using eclipse IDE

Click on the finish button.


Hello World Servlet example using eclipse IDE

After clicking on the finish button, your Eclipse IDE will generate a servlet class based on the configuration provided in previous steps as shown below.


Hello World Servlet example using eclipse IDE


3) Write your custom code:

Write your code into doGet() method and call to setContentType() establishes the MIME types of the HTTP response.In this program,the MIME type is text/html.Next the getWriter() method obtains a PrintWriter.Then println() is used to write some HTML code as the HTTP response.

Read more about HTTP Request Methods: HTTP Requests Methods
Hello World Servlet example using eclipse IDE

4) Run your Servlet Code:

To run your Servlet code right click on your project -> Run As -> 1 Run on Server.


Hello World Servlet example using eclipse IDE
Select the existing Tomcat Server and Click on the finish button.

Hello World Servlet example using eclipse IDE
5) The output on Browser.
Your Eclipse IDE will generate output on own browser and print "HELLO WORLD" on the browser.


Hello World Servlet example using eclipse IDE



You may also like:
JSP Life Cycle
MVC Architecture in JSP with Example
Custom Tags in JSP
Session Tracking in Servlet and its technique
Servlet Life Cycle in Java
How to create Hello World Servlet example using eclipse IDE with Tomcat 7? How to create Hello World Servlet example using eclipse IDE with Tomcat 7? Reviewed by Prashant Srivastava on October 13, 2018 Rating: 5

No comments:

Powered by Blogger.