Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Create Addition Program using JSP and Servlet

Step1st:-   Create a JSP web page and design form using HTML Form Content.
                <form action="ServletName" method="post">

Step2nd:-  Create Servlet and Write Code under doPost() in eclipse otherwise in Netbeans processRequest() which is used define method which will be called under doGet() and doPost()

                int a = Integer.parseInt(request.getParameter("textfiledname"));

     request is the Object of HttpRequest class and getParameter() is used to get the data of HTML component(textfield,radio,checkbox,etc)

 Complete Code explanation:
Code of JSP:-

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <form method="post" action="AdditionSer">
            <input type="text" name="txtnum1" placeholder="Enter First Number" />
            <input type="text" name="txtnum2" placeholder="Enter Second Number" />
            <input type="submit" name="btnsubmit"  value="Submit"  />

Code of Servlet:-

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 public class AdditionSer extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
          int a = Integer.parseInt(request.getParameter("txtnum1"));
          int b = Integer.parseInt(request.getParameter("txtnum2"));
          int c = a+b;
          out.print("result is "+c);

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post