Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at

Tuesday, October 7, 2014

How to start with servlets, a guide for beginners.

How to start with Servlets, Write a servlet program to print the name coming from a HTML form.

Before we start with servlets , make sure you have following software installed.
  1. Apache Tomcat 7.0
  2. Java 1.7 or higher

Create a folder structure as shown below under C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps directory.

  1. Create a file say and placed it in the classes folder.
  2. import javax.servlet.http.*;
    import javax.servlet.*;
    public class HelloWorld extends HttpServlet 
     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
      String name=request.getParameter("t1");
      PrintWriter ps=response.getWriter();
      ps.print("Hello "+name);
  3. compile the file.
  4. edit the web.xml file as given below.

  5. <?xml version="1.0" encoding="ISO-8859-1"?>
     Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    <web-app xmlns="" xmlns:xsi=""
      version="3.0"  metadata-complete="true">
      <display-name>Welcome to Tomcat</display-name>
      <description>Welcome to Tomcat</description>
  6. Create a Hello.html file and place it in HelloWorld i.e. root folder.

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
      <title> First App</title>
     <form action="">
     <h1>Please enter your name </h1>
      <input type="text" name="t1"><input type="submit" value="Submit">
  8. Run the Tomcat Server.
  9. Navigate to the following URL.

    You should see the Hello.html web page on your browser.

  10. Enter your Name in the text field and click submit.
  11. You should see the result something like below.

If you run into any error, please feel free to reach us. 

No comments: