Followers

Servlet: Multiple Buttons in Servlet

index.html
==========

<html>
    <head>
        <title>Multiple Buttons Demo </title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="DemoServlet" method="get">
            <p>Please enter the first number : <input type="text" name="first" ></p>
            <p>Please enter the second number : <input type="text" name="second" ></p>
           
            <p><input type="submit" name ="add" value="Addition" ></p>
            <p><input type="submit" name="sub" value="Subtraction" ></p>
            <p><input type="submit" name="end" value="END" ></p>
           
        </form>
    </body>
</html>

web.xml
========

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>DemoServlet</servlet-name>
        <servlet-class>MultipleButtonsEx</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DemoServlet</servlet-name>
        <url-pattern>/DemoServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

MultipleButtonsEx.java
======================

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 MultipleButtonsEx extends HttpServlet {

    protected void processRequest(HttpServletRequest p, HttpServletResponse q)
            throws ServletException, IOException {
        q.setContentType("text/html;charset=UTF-8");
        PrintWriter out = q.getWriter();
        String first = p.getParameter("first");
        String second = p.getParameter("second");
        if(first.equals("") || second.equals("")){
        out.println("Invalid data try again");
        return;
        }
        int fno = Integer.parseInt(first);
        int sno = Integer.parseInt(second);
        int result = 0;
        if(p.getParameter("add") != null){
            result = fno+sno;
        out.println("<b> The result of the addition is </b> "+result);      
        }
        if(p.getParameter("sub") != null){
            result = fno - sno;
        out.println("<b> The result of the subtraction is </b> "+result);
        }
        if(p.getParameter("end") != null){
         
        out.println("<b> Thank You ");
        out.println("Hace a nice day");
        }
        out.println("<br /><br />");
        out.println("BYE, BYE");
       
    }

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


}

No comments:

Post a Comment