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

package paqueteCtrl;



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


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import paqueteBL.FvcBL;
import paqueteBL.Host;
import paqueteBL.VM;
import paqueteBL.VMext;
import paqueteCMD.*;
import paqueteCMD.interfaceCMD;
import paqueteVIX.VIX;
//import paqueteCMD.interfaceCMD;


/**
 *
 * @author sysmq
 */
public class dispacher extends HttpServlet {

    //@Override
    //public void init()
    //{
    //    System.out.println("hola");
    //    VIX a=new VIX();
    //    a.start();
    //}
    
    /**
     * 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");
        response.setContentType("text/html;charset=ISO-8859-1");

        //HttpSession session = request.getSession(true);
        PrintWriter out = response.getWriter();
        String accion="";
        try {
            /* TODO output your page here*/

            String formulario=(String)request.getParameter("form");
            accion=(String)request.getParameter("accion");
            Class clase;
            String url="";

           
            if (formulario==null)
            {
                formulario="General";
            }

            //if (formulario!=null)
            {
                System.out.println("FORM="+formulario+" accion:"+accion );
                clase=Class.forName("paqueteCMD.cmd"+formulario);
                interfaceCMD o=(interfaceCMD)clase.newInstance();
                url=o.procesa(accion, request,response);
            }
            //else
            //if (accion!=null && (accion.equals("login") || Sesion.getUsuario(request)!=null))
            //{
                //clase=Class.forName("paqueteCMD.Cmd"+formulario);
                //interfaceCMD o=(interfaceCMD)clase.newInstance();
                //url=o.procesa(accion, request,response);
            //    url="/viewPrincipal.jsp";
            //}

            System.out.println("PAGINA:"+url);
            //else
            //{
            //    String ip = request.getRemoteAddr();
            //    url = "/intranet/viewIntranetLogin.jsp";
            //}
            //if (accion.equals("login") && url.startsWith("/dispacher"))
            //    response.sendRedirect("/oficinaDigital"+url);
            //else
            getServletConfig().getServletContext().getRequestDispatcher(url).forward(request, response);
        }

        catch(Exception e)
        {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>ERROR</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Se ha producido un error no especifico!</h1>");
            out.println("Reintente la operación. Si se mantiene el error, póngase en contacto con el desarrollador.");
            out.println("<br/>Accion:"+accion);
            out.println("Error: "+e.toString()+" Accion:"+accion );
            out.println("</body>");
            out.println("</html>");

//            Sesion.setUsuario(request, null);
        }
        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>

}