Selasa, 05 Mei 2015

Aplikasi Servlet Untuk Menambahkan Dan Mengalikan Dua Bilangan Yang Diinputkan




Untuk membuat Aplikasi Servlet Untuk Menambahkan Dan Mengalikan Dua Bilangan Yang Diinputkan , ada 2 tahap :

1 ) Langkah pertama ,,,,
 Membuat satu file html sebagai form inputan angka yang akan dimasukkan, kemudian beri nama  newhtml.html  
lalu ketikkan kode dibawah ini!

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1>Kalkulator Servlet Untuk Menjumlahkan Atau Mengalikan Dua buah bilangan</h1>
        <form method="GET" action="NewServlet1">
            <input type="text" name="valOne" value="0">
            <input type="text" name="valTwo" value="0">
            <input type="submit" value="ADD">
             </br> </br>
        </form>
        <form method="POST" action="NewServlet1">
            <input type="text" name="valOne" value="0">
           <input type="text" name="valTwo" value="0">
         
            <input type="submit" value="MULTIPLY">
        </form>
     
     
     
    </body>
</html>


2 ) Langkah Kedua ,,,,
     Membuat file servlet , kemudian beri nama NewServlet1.java 

lalu ketikkan kode dibawah ini!

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


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;

/**
 *
 * @author Goldfriet
 */
public class NewServlet1 extends HttpServlet  {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response, String str)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String txt = "";
        try {
            if (str != null) {
                txt = str;
            }
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FirstServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet FirstServlet at " + request.getContextPath() + "</h1>");
            out.println("<p>");
            out.println("<h2>");
            out.println("Hello :).... ");
            out.println("<p>");
            out.println("Hasilnya Adalah " + txt);
            out.println("</h2>");
            out.println("<form method=\"GET\" action=\"FirstServlet\">");
         
         
        } finally {
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int rslt;
        String result = null;
        String paramOne = null;
        String paramTwo = null;

        paramOne = request.getParameter("valOne");
        paramTwo = request.getParameter("valTwo");
        rslt = Integer.parseInt(paramOne) + Integer.parseInt(paramTwo);
        result = String.valueOf(rslt);

        processRequest(request, response, result);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int rslt;
        String result = null;
        String paramOne = null;
        String paramTwo = null;

        paramOne = request.getParameter("valOne");
        paramTwo = request.getParameter("valTwo");

        rslt = Integer.parseInt(paramOne) * Integer.parseInt(paramTwo);
        result = String.valueOf(rslt);
        processRequest(request, response, result);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

Setelah itu simpan, kemudian silahkan dijalankan newhtml.html    , jika berhasih hasilnya akan seperti dibawah ini...





Tidak ada komentar:

Posting Komentar