Servlet example eclipse: This tutorial will give you insight into how to run a servlet class that prints the “Hello World” String in the Browser. Here we are going to use the Eclipse IDE to develop code and will run the code in Tomcat Webserver. Creating Servlet in IDE Environment will save you from doing a lot of work. We have outlined the steps to be done in order to create a Hello World Servlet Example using Eclipse IDE in detail.
Tomcat Servlet using Eclipse IDE
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 developers.
Download Eclipse IDE: eclipse ide for java ee developer
ii) Download and install the Apache Tomcat server
Download Tomcat Server:
After download and install Eclipse IDE for Java EE developer and Tomcat Server, you will be able to create a Hello World Servlet Example using Eclipse IDE.
Do Refer:
- Session Management in Servlet Using HttpSession
- Session Management Using URL Rewriting in Servlet
- Cookies Class in Servlet Explained with Example
- Session Management Using Hidden Form Field in Servlet with Example
1. Create a Dynamic Web Project
To create a Dynamic web project open eclipse ide → Go to menu → New → Dynamic web project.
Write your project name and click on next.
Click on the next button
check on generate web.xml and click on the finish button.
Once you click on the finish button, your Eclipse IDE will automatically create a dynamic web project as follows
2. Create a Servlet Class
After creating a dynamic web project the next step is to create a servlet class. right-click on your project → New → Servlet
Write the package name in the java package field and class name in the class name field and then click on next.
You can mention the URL mapping in the following screen(web.xml) and click on next.
Click on the finish button.
On Clicking the finish button, your Eclipse IDE will generate a servlet class depending on the configuration provided in earlier steps as under.
3. Write your Custom Code
Write down your code in 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. Later use println() to write some HTML code as HTTP response.
4. Run your Servlet Code
In Order to run your Servlet code right-click on your project → Run As → 1 Run on Server.
Choose the existing Tomcat Server and then Click on the finish button.
5. The Output on Browser
Your Eclipse IDE will generate output on your own browser and print “HELLO WORLD” on the browser.