How to run Servlet program in Eclipse - Java Servlet Example - What is a Java Servlet - Servlet Tutorials, web.xml in servlet

How to run Servlet program in Eclipse - Java Servlet Example - What is a Java Servlet - Servlet Tutorials, web.xml in servlet

Hello and Welcome in Servlet Tutorials Section of this Blog.....

First of all i will tell you about, what are the Java Servlets---- The Java Servlets are the java classes which are executed in Web Server or In Other words the Java Servlet is used to generate Dynamic pages in Websites. The java Servlets receive the request from the clients which are basically the Web Browsers and generate the response according to their need.

The Format of a Java Servlet is follows--


import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet
     public void doGet(HttpServletRequest request,HttpServletResponse response)
         PrintWriter out=response.getWriter();
         out.write("Hello Servlet");
        public void doPost(HttpServletRequest request,HttpServletResponse response)
          PrintWriter out=response.getWriter();
         out.write("Hello Servlet");


In above Exmple I have used 2 methods doGet() and doPost(), The only one method will be executed when this Servlet will be called because the client can only be send the request with only one method whether it is GET or whether it is POST some of the methods are available but those are rarely used so we will discuss about those methods later.

Lets come to the next section of this tutorial, How to run a java Servlet program in Eclipse IDE with one client and a java servlet.

Step 1- In Eclipse IDE choose the New option from toolbar and go to other and then go to the web and then click on Dynamic Web Project then give it to a suitable name and make sure there will be the Target Runtime set as Apache Tomcat v6.0 or other and the Dynamic web module version 2.5. and then finish

Step2- After creating the blank project right click on your project and make a new HTML File and give it the name index.html and the content of this file like this

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<form method="GET" action="table">
Enter a Number<input type="text" name="num"><input type="submit" value="Table">

And you can change the content of the index.html file according to your requirement

Step 3- Now Create a Servlet file by right clicking on your project and select New--> Other..--->Web-->Servlet.

The content of the Servlet file like this...

package W3STutorial;



import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PrintTable extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException
PrintWriter out=response.getWriter();
int num=Integer.parseInt(request.getParameter("num"));
for(int i=1;i<=10;i++)


You can change the content of the PrintTable according to your requirement.

Step 4- Now We need to change the web.xml content which is available under the WEB-INF directory on your Project so the web.xml content should be as follows.

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">




Now your project is ready to deploy

Step 5- Right click on your project and choose Run As -->Run On Server-->Choose your Server and finish then your index.html page's content will be shown on browser.

and pass the some value on your textbox like 12 and click on Table and then the output will be shown as follows


If the above example is helpful for you then please give your comments and advise.


{ 0 comments... read them below or add one }

Post a Comment