SIGN UP MEMBER LOGIN:    
ARTICLE

Servelt Application using Eclipse IDE

Posted by Mohan Rathour Articles | Java November 01, 2011
This article is regarding development of a Servlet applications on Eclipse IDE.
Reader Level:
Download Files:
 

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

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor