Rabu, 04 Januari 2012

LANGKAH-LANGKAH MEMBUAT KALKULATOR DENGAN SERVLET

1.      Buat project baru pada netbeans anda, pilih Java Web -> Web Application, kemudian klik tombol Next >


2.      Beri nama project sesuai dengan nama yang anda inginkan, kemudian klik tombol Next >


3.      Klik tombol Finish


4.      Buat package baru dalam project yang anda telah buat sebelumnya, dengan cara klik kanan pada folder source packages kemudian pilih New -> Java Package   


5.      Tulis nama package seperti yang anda inginkan, kemudian klik tombol Finish


6.      Selanjutnya kita buat servlet baru dengan cara klik kanan pada packages yang baru anda buat kemudian pilih New -> Servlet


7.      Beri nama pada kolom Class Name, klik Next >



8.      Klik Finish

9.      Buka servlet yang anda buat, lihat pada method ProcessRequest. Pada bagian tersebut terdapat try{}, ganti kode tersebut dengan dengan kode dibawah ini :

try {
     out.println("<html>");
     out.println("<head>");
     out.println("<title>Kalkulator Servlet</title>"); 
     out.println("</head>");
     out.println("<body>");
     out.println("<h1>Kalkulator Servlet</h1>");
     out.println("<h3>Kalkulator Standard</h3>");
     out.println("<form method='post' action='proses'>");
     out.println("<table border='0' cellpadding='3' cellspacing='4'>"
     +"<tr><td colspan='2'>Angka 1 : </td><td colspan='2'><input type='text' name='angka1' size='5'></td></tr>"
     +"<tr><td colspan='2'>Angka 2 : </td><td colspan='2'><input type='text' name='angka2' size='5'></td></tr>"
     +"<tr><td><input type='submit' name='tambah' value=' + '></td>"
     +"<td><input type='submit' name='kurang' value=' - '></td>"
     +"<td><input type='submit' name='kali' value=' x '></td>"
     +"<td><input type='submit' name='bagi' value=' : '></td></tr></table>");
     out.println("</form>");
     out.println("<h3>Kalkulator Scientific</h3>");
     out.println("<form method='post' action='prosesdua'>");
     out.println("<table border='0' cellpadding='3' cellspacing='4'>"
     +"<tr><td colspan='2'>Angka : </td><td colspan='2'><input type='text' name='angka3' size='5'></td></tr>"
     +"<tr><td><input type='submit' name='sin' value='sin'></td>"
     +"<td><input type='submit' name='cos' value='cos'></td>"
     +"<td><input type='submit' name='tan' value='tan'></td>"
     +"<tr><td><input type='submit' name='faktorial' value=' n!'></td>"
     +"<td><input type='submit' name='akar' value=' √ '></td>"
     +"<td><input type='submit' name='pangkat' value=' ^ '></td></tr</table>");
     out.println("</form>");
     out.println("</body>");
     out.println("</html>");
}


10.  Ulangi langkah 6 – 8, kemudian ketik kode seperti berikut :

package kalkulator;

/*
 * 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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author admin
 */
@WebServlet(name = "proses", urlPatterns = {"/proses"})
public class proses 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)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        double a=Double.parseDouble(request.getParameter("angka1"));
        double b=Double.parseDouble (request.getParameter("angka2"));
        double c = 0;
        double i;
        String d = null;
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Hasil</title>"); 
            out.println("</head>");
            out.println("<body>");
           
        if(request.getParameter("tambah") != null){
            d="+";
            c = a+b; 
            out.println("<h1>Hasil Operasi</h1>");
        out.println("<h2>"+a+" "+d+" "+b+" = "+c+"</h2>");
        }
        else if (request.getParameter("kurang") != null)
        {
            d="-";
            c = a-b;
            out.println("<h1>Hasil Operasi</h1>");
        out.println("<h2>"+a+" "+d+" "+b+" = "+c+"</h2>");
        }
         else if (request.getParameter("kali") != null) {
             d="x";
             c = a*b;
             out.println("<h1>Hasil Operasi</h1>");
        out.println("<h2>"+a+" "+d+" "+b+" = "+c+"</h2>");
         }
         else if(request.getParameter("bagi") !=null){
             d="/";
             c = a/b;
             out.println("<h1>Hasil Operasi</h1>");
        out.println("<h2>"+a+" "+d+" "+b+" = "+c+"</h2>");
         }
        //out.println("<h1>Hasil Operasi</h1>");
        //out.println("<h2>"+a+" "+d+" "+b+" = "+c+"</h2>");
            out.println("</body>");
            out.println("</html>");
            out.println("<form method='post' action='kalkulator'>");
            out.println("<input type='submit' name='back' value='back'>");
        } 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 {
        processRequest(request, response);
    }

    /**
     * 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 {
        processRequest(request, response);
    }

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




11.  Ulangi langkah 6 – 8, dan ketik kode sebagai berikut :

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

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author admin
 */
@WebServlet(name = "prosesdua", urlPatterns = {"/prosesdua"})
public class prosesdua 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)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String angka3=request.getParameter("angka3");
        try {   

        double e=Double.parseDouble(request.getParameter("angka3"));
        double h=0;
        String d = null;

           
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Hasil</title>"); 
            out.println("</head>");
            out.println("<body>");
           
        if(request.getParameter("sin") != null){
            d="sin dari ";
            h = Math.sin(e);
        }
        else if (request.getParameter("cos") != null)
        {
            d="cos dari ";
            h = Math.cosh(e);
            }
         else if (request.getParameter("tan") != null) {
             d="tan dari ";
             h = Math.tan(e);
         }
         else if(request.getParameter("faktorial") !=null){
             d="faktorial dari ";
             h=1;
             for(int i=1;i<=e;i++){
               h=h*i;
             }
         }
       
         else if(request.getParameter("akar") !=null){
             d="akar dari ";
             h = (float) Math.sqrt(e);
         }
       
         else if(request.getParameter("pangkat") !=null){
             d="kuadrat dari ";
             h = (float) Math.pow(e, 2);
         }
        out.println("<h1>Hasil Operasi</h1>");
        out.println("<h2>"+d+" "+e+" = "+h+"</h2>");
            out.println("</body>");
            out.println("</html>");
            out.println("<form method='post' action='kalkulator'>");
            out.println("<input type='submit' name='back' value='back'>");
        } 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 {
        processRequest(request, response);
    }

    /**
     * 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 {
        processRequest(request, response);
    }

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


Tidak ada komentar:

Posting Komentar