Creating a Simple Java Servlet to Add Two Numbers in Eclipse

3 min read

About This Summary

This summary was generated using YouTubeToSummary - a free web tool for converting YouTube videos into text summaries. Summaries are tool outputs, not original content. You can use the tool for free to create your own summaries from any YouTube video.

Channel: Telusko

Creating a Simple Java Servlet to Add Two Numbers in Eclipse

Introduction

In this tutorial we walk through building a basic Java servlet that receives two numbers from an HTML form, adds them on the server side, and displays the result on the web page. The example uses Eclipse, Tomcat, and standard servlet APIs.

1. Project Setup

  • Create a Dynamic Web Project in Eclipse (the previous video covered this step).
  • Add an index.html page containing a form with two input fields (num1 and num2) and a Submit button that sends a request to the servlet.

2. Creating the Servlet Class

  1. Right‑click the project → New → Class.
  2. Name the class (e.g., AddServlet).
  3. Choose a package (e.g., com.dr.disco). Using the reverse‑domain naming convention ensures uniqueness.
  4. Click Finish – a plain Java class is generated.

3. Turning the Class into a Servlet

  • Extend HttpServlet:
public class AddServlet extends HttpServlet {
    // servlet code goes here
}
  • By inheriting from HttpServlet, the class automatically becomes a servlet.

4. Implementing the service Method

The servlet lifecycle requires a service(HttpServletRequest req, HttpServletResponse resp) method.

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 1. Retrieve parameters
    int i = Integer.parseInt(req.getParameter("num1"));
    int j = Integer.parseInt(req.getParameter("num2"));

    // 2. Perform addition
    int k = i + j;

    // 3. Send result back to client
    PrintWriter out = resp.getWriter();
    out.println("Result: " + k);
}

Key points - req.getParameter() returns a String; convert it to int with Integer.parseInt. - Use the HttpServletResponse object’s getWriter() to write output to the browser. System.out.println only prints to the server console.

5. Configuring the Servlet in web.xml

Tomcat needs a mapping that tells it which URL pattern invokes the servlet.

<servlet>
    <servlet-name>addServlet</servlet-name>
    <servlet-class>com.dr.disco.AddServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>addServlet</servlet-name>
    <url-pattern>/add</url-pattern>
</servlet-mapping>
  • <servlet-name> links the <servlet> and <servlet-mapping> entries.
  • <servlet-class> must be the fully‑qualified class name (including the package).
  • <url-pattern> matches the request URL (/add) used in the form’s action attribute.

6. Testing the Application

  1. Start the Tomcat server from Eclipse.
  2. Open index.html in a browser, enter two numbers, and click Submit.
  3. The browser displays the computed sum (e.g., Result: 46).
  4. If the result appears only in the console, verify that you used resp.getWriter().println(...) instead of System.out.println.

7. Common Pitfalls

  • Missing web.xml mapping – Tomcat cannot locate the servlet and returns a 404 error.
  • Using the wrong method name – only service, doGet, or doPost (depending on the request type) are recognized; arbitrary method names will not be invoked.
  • Forgetting to convert parameters – request parameters are strings; failing to parse them leads to NumberFormatException.
  • Not handling IOExceptiongetWriter() may throw an exception; declare throws IOException or handle it with a try‑catch block.

8. What Comes Next?

The video hints at extending the project with multiple servlets, exploring doGet/doPost methods, and adding more complex business logic.


By extending HttpServlet, defining a service method, retrieving request parameters, performing server‑side calculations, and correctly mapping the servlet in web.xml, you can create functional Java servlets that process user input and return results directly to the web page.

We use AI to generate summaries. Always double-check important information in the original video.

Key Takeaways

  • Create a Dynamic Web Project in Eclipse (the previous video covered this step).
  • Add an `index.html` page containing a form with two input fields (`num1` and `num2`) and a Submit button that sends a request to the servlet.

Educational Value

This summary can be used as an effective educational tool. Students can use it to create study notes, researchers can use it to extract information quickly, and professionals can use it for meeting preparation or continuous learning.

For Students:

Use this summary as a foundation for your study notes

For Researchers:

Extract key information quickly

For Professionals:

Prepare for meetings or continuous learning

Need Help?

Have questions about using the tool? Check our FAQ page or contact us.