Servlet 2.5 – Hello World Tutorial

In this tutorial, we provide a step-by-step guide for creating a hello world web application using Servlet 2.x technology.

Prerequisites:

  • Eclipse IDE (Mars release)
  • Java 1.7
  • Apache tomcat 7

1. Create dynamic web project

Open eclipse, then select File -> New -> Dynamic Web Project.

Servlet 2 Hello World

In the next screen, fill the mandatory fields as above, in case you didn’t previously link apache tomcat to eclipse, then just click on “New Runtime” button and link your tomcat, in this tutorial i use Apache tomcat 7.

Click “Next”.
Servlet 2 Hello World

Click “Next”.

Servlet 2 Hello World

in the final screen, make sure to check ‘Generate web.xml deployment descriptor’ checkbox, so that eclipse generates automatically the web.xml file under WEB-INF.

Here we go, the structure of the generated project looks like the following:

 

 

 

 

 

 

2. Create Hello World servlet

Right click on the src folder, then select New -> Servlet

Servlet 2 Hello World

In the next screen, select the name and the package of the servlet then click “Next”.

Servlet 2 Hello World

In this screen, you can define the url mapping of the new servlet, by default the mapping is set to the servlet name. Just keep it as is, you can always change it later on inside web.xml.

Servlet 2 Hello World

Here you define the inherited methods to be generated inside the servlet, by default each servlet should implement doGet and doPost methods, so just keep it as is and click “Finish”.

After clicking finish, eclipse automatically creates a servlet class named HelloWorld.java under com.programmer.gate package as the following:

it also writes the definition of the servlet under web.xml as the following:

Our new servlet can be accessed by: <application-path>/HelloWorld. If you ever want to change its path, just modify the <url-pattern> attribute under <servlet-mapping>.

In order to return a hello world html message from our servlet we modify the doGet() method to be as the following:

3. Deploy the applicaton

Follow this guide in order to setup Tomcat 7 in eclipse.

Then deploy your application to Tomcat 7, just drag and drop the application to the tomcat instance under servers view.

 

Servlet 2 Hello World

Now that the application is deployed successfully under Tomcat 7, in order to start tomcat, right click on tomcat instance -> Start.

4. Access the application

After starting up tomcat, you can access the “Hello World” servlet using the following url:

http://localhost:8085/Servlet-2-Hello-World/HelloWorld

In order to find the port number of the application, double click on the tomcat instance in servers view:

Servlet 2 Hello World

These port attributes are editable and you can change them anytime.

/Servlet-2-Hello-World: denotes the deployed application name.

Servlet 2 Hello World

/HelloWorld: denotes the name of the requested servlet, it is the value of <url-pattern> attribute under <servlet-mapping> in web.xml.

Finally, the output of the url would look like:

Servlet 2 Hello World

 

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar