Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, 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

This is the standard approach of Java web application that we should always create a design layer under JSP and code layer under servlet.

It is also called the 2-tier architecture of java means we work on two different layers then it will be 2-tier.




Step by Step Implementation of Java Web Development:-



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



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>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="post" action="AdditionSer">
            <input type="text" name="txtnum1" placeholder="Enter First Number" />
            <br>
            <br>
            <input type="text" name="txtnum2" placeholder="Enter Second Number" />
            <br>
            <br>
            <input type="submit" name="btnsubmit"  value="Submit"  />
           
        </form>
    </body>
</html>



Code of Servlet for Netbeans:-


import java.io.IOException;
import java.io.PrintWriter;
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 {
        response.setContentType("text/html;charset=UTF-8");
        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);
        }
    }

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

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

    

Code for Servlet in eclipse

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
int a = Integer.parseInt(request.getParameter("txtnum1"));
int b = Integer.parseInt(request.getParameter("txtnum2"));
int c = a+b;
out.print(c);
}

Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post