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 these web artifacts and provides runtime environments in which these artifacts 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 mayor webcontainer, e.g. Jetty and Apache Tomcat as well as the mayor 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 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 finish button, then after click 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.

import java.io.IOException;
import
java.io.PrintWriter;
import
javax.servlet.*;
import
javax.servlet.http.*;
public
class SerLifeCycle extends HttpServlet
{

      int count=0;
     
public void init(ServletConfig config)throws ServletException
      {

           
super.init(config);
     
}

     
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {

           
response.setContentType(
"text/html");
           
PrintWriter pw=response.getWriter();

           
count++;
           
java.util.Date d =
new java.util.Date();
           
pw.println(
"<html><body>");
           
pw.println(
"Since loading, this servlet has been accessed " + count + " times.");
           
pw.println(
" the current time and date is " +d.toString());
           
pw.println(
"</body></html>");
     
}

}

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

Up Next
    Ebook Download
    View all
    Learn
    View all