Servelt Application using Eclipse IDE

Required component
  1. Eclipse Platform
  2. Java 6 SDK
  3. Apache Tomcat
The Eclipse IDE is an open-source IDE. It provides tools for developing standard Java web applications and Java EE applications. Eclipse is great tool for creating HTML, JSPs, and servlets. Eclipse WTP simplifies the creation of these web artefacts and provides runtime environments in which these artefacts can be deployed, started and debugged. In Eclipse WTP you create "Dynamic Web Projects". These projects provide the necessary functionality to run, debug and deploy Java web applications.
 
Eclipse WTP supports all major webcontainer, e.g. Jetty and Apache Tomcat as well as the major Java EE application server. This article uses Apache Tomcat as a webcontainer.
 
You need the Java SDK to run servlets on your machine.
 
Apache Tomcat is an open-source Web and servlet container, used in the official reference implementations for Java Servlet and Java Server Pages. To install Apache Tomcat, extract the files from the downloaded archive and place them into a directory. I put them in my C:\Program Files directory to make them easy to locate later. That's it for now.
 
Create a new web project
 
Step 1: From the Eclipse IDE, select a workspace after that click the ok button.
 
workspace
 
Step 2: Set the Perspective to Java EE (if not set by default).
 
Perspective  
 
SavePerspective
 
Click on the ok button.
 
Step 3: Create a new project from File->New->Project to view the project wizards. From project wizards select web->dynamic web project. Click on next button.
 
Serproject
 
selectwizard
 
Step 4: Enter the details for the project for e.g. Project Name, Target runtime, Configuration etc. For now, we can concentrate on Project Name (Here project name WecomeServlet) and target Runtime. Target Runtime is used to set the runtime environment for your servlet programs.
 
ProjectName
 
Click on the finish button.
 
Step 5: Setting up the Target Runtime: Select Windows -> Preferences -> Server -> Runtime Environments to configure WTP to use Tomcat Press Add.
 
RuntimeEnviroment
 
Click next button
 
Then Specify the installation directory of Tomcat directory.
 
runonserver
 
Press the Finish button, then after clicking on ok button.
 
Step 6: At this stage, the Project is created in eclipse. Now right click on the project and create new Servlet.
 
ServletName
 
Step 7: Enter the details of your servlet and click finish. Write the  business logic in the service() or doGet() or doPost() method.
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import javax.servlet.*;  
  4. import javax.servlet.http.*;  
  5. public class SerLifeCycle extends HttpServlet  
  6. {  
  7.  int count = 0;  
  8.  public void init(ServletConfig config) throws ServletException  
  9.  {  
  10.   super.init(config);  
  11.  }  
  12.  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  
  13.  {  
  14.   response.setContentType("text/html");  
  15.   PrintWriter pw = response.getWriter();  
  16.   count++;  
  17.   java.util.Date d = new java.util.Date();  
  18.   pw.println("<html><body>");  
  19.   pw.println("Since loading, this servlet has been accessed " + count + " times.");  
  20.   pw.println(" the current time and date is " + d.toString());  
  21.   pw.println("</body></html>");  
  22.  }  
  23. }  
 
Step 8: Select your servlet, right-click on it and select Run As -> Run on Server. The server will start by itself and will run the program for you if there is no error.
  
RunServlet
  
In this case, the output will be printed as
  
output