Servlet Dengan Jumlah yang Bisa Ditampung (Client) 5 Secara Bersamaan

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

import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import siregar.romauli.siregar.thread.*;

/**
 *
 * @author toshiba
 */
public class FirstServlet extends HttpServlet {
    public int numberOfClients = 5;
    public int numberMax = 0;
    ProcessThread aThread = null;

    /**
     * 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>Sorry busy bro </h1>");
            out.println("<p>");
            out.println("<h2>");
            out.println("Hello :).... ");
            out.println("<p>");
            out.println("Hasilnya Adalah " + txt);
            out.println("</h2>");
            out.println("<img src=\"image/1.jpg\" height=\"200\" width=\"100\"/>");
            out.println("</body>");
            out.println("</html>");
        } 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 synchronized void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       while (numberOfClients > numberMax)
       {
        try
        {
           
                wait();
        }
        catch (Exception e)
        {

        }
       
       }
       numberOfClients = numberMax +1;
       int rslt;
        String result = "foobar";
       aThread = new ProcessThread(this, request, response, result);
       aThread.start();
       
        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, rslt);
    }

    /**
     * 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;
        Enumeration<String> attrs = null;
        String operation = null;

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

        rslt = Integer.parseInt(paramOne) * Integer.parseInt(paramTwo);
        //result = String.valueOf(rslt);
        result = "";
        while (attrs.hasMoreElements()) {
            result = result + ", " + attrs.nextElement();
     
        }
    }
    processRequest(request, response, rslt);
}

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

Komentar

Postingan populer dari blog ini

Rational Unified Process

Iterative Development

Timeboxing Model