Skip to content

Instantly share code, notes, and snippets.

@Abhijeetd01
Forked from PrabhjotSingh/MyServlet.java
Last active June 28, 2018 03:13
Show Gist options
  • Save Abhijeetd01/8cb4dd6739284f34c9613f526a9cc2e4 to your computer and use it in GitHub Desktop.
Save Abhijeetd01/8cb4dd6739284f34c9613f526a9cc2e4 to your computer and use it in GitHub Desktop.

Revisions

  1. Abhijeetd01 revised this gist Jun 28, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <body bgcolor="Yellow">

    <form action="MyServlet" method="get">
    Enter 1st Number <input type="text" name="n1"><br>
  2. @PrabhjotSingh PrabhjotSingh revised this gist Dec 2, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions MyServlet.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    /**
    * View full program at http://java.directref.com/?p=331
    */
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
  3. @PrabhjotSingh PrabhjotSingh created this gist Dec 2, 2012.
    37 changes: 37 additions & 0 deletions MyServlet.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class MyServlet extends HttpServlet
    {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    response.setContentType("text/html"); // informing the client that which format of data/response will be send
    PrintWriter out= response.getWriter();

    //All data entered into a "TEXT" field of form, is stored as "STRING"
    //Hence if we enter "1" in a "TEXT" field of form, it will be stored as "ONE"
    //To convert it to "INT", we use a predefined function called= "parseInt()"
    int a1= Integer.parseInt(request.getParameter("n1"));
    // using "getParameter()" to retrieve data entered by user in "n1" field
    int a2= Integer.parseInt(request.getParameter("n2"));

    if (request.getParameter("r1")!=null) // checking if 1st radio button checked or not?
    {
    out.println("<h3> Addition= </h3>"+(a1+a2));
    }
    else if(request.getParameter("r2")!=null) // checking if 2nd radio button checked or not?
    {
    out.println("<h3> Subtraction= </h3>"+(a1-a2));
    }
    else if(request.getParameter("r3")!=null) //checking if 3rd radio button checked or not?
    {
    out.println("<h3> Multiply= </h3>"+(a1*a2));
    }
    else
    {
    out.println("<h3> Divide= </h3>"+(a1/a2));
    }
    }

    }
    21 changes: 21 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>

    <form action="MyServlet" method="get">
    Enter 1st Number <input type="text" name="n1"><br>
    Enter 2nd Number <input type="text" name="n2"><br>

    <input type="radio" name="r1" value="Add">Add<br>
    <input type="radio" name="r2" value="Subtract">Subtract<br>
    <input type="radio" name="r3" value="Multiply">Multiply<br>
    <input type="radio" name="r4" value="Divide">Divide<br>
    <input type="Submit" value="Submit">

    </form>
    </body>
    </html>