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

package servlet;


import java.util.List;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

import entityJB.*;
import java.io.IOException;
import java.io.PrintWriter;


/**
 *
 * @author Administrator
 */
public class Controllerserv extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
     @EJB()
     private BaleBeanRemote2 baleBeanRemote;
    
                     
     
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
           
            List<Baleent2> Baleent2;
		
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Controllerserv</title>");  
            out.println("</head>");
            out.println("<body>");
            
             out.println("<form method='get' action='Controllerserv'>");
                        out.println("<select name='viewitems'>");
                        out.println("<option>View All Discount bales</option>");
                        out.println("<option>View All Bales</option>");
                       
                        			
                        out.println("<option>Find item by description or Code</option>");
                        
                        out.println("</select>");
                        out.println("<input type='Submit' value='Get Bale details' name='search'/>");
            out.println("</form>");
        
            String searchitem=request.getParameter("viewitems");
            
             if(searchitem!= null && searchitem.equals("View All Bales")){
        
             try{
                 //call the allitems method
                 Baleent2=baleBeanRemote.findAllBales();
                  HttpSession session = request.getSession();
                  session.setAttribute("Baleent2",Baleent2);
                  RequestDispatcher dispatcher = getServletContext()
                                               .getRequestDispatcher("/Viewdisplay.jsp");
                                               dispatcher.forward(request, response);
                                //forward data to the Viewdisplay.jsp to present
                                //it to user
                                  dispatcher.forward(request, response);
             }
            catch(Exception ex){
			System.err.println("Can not show the all bale page");
			}
            }
     
            
             else {
             try{
                 //call the allbales method
                 Baleent2=baleBeanRemote.findAllBales();
                  HttpSession session = request.getSession();
                  session.setAttribute("Baleent2",Baleent2);
                  RequestDispatcher dispatcher = getServletContext()
                                               .getRequestDispatcher("/Viewdisplay.jsp");
                                               dispatcher.forward(request, response);
                                 //forward data to the DiscountItemsj.jsp to present
                                //it to user
                                dispatcher.forward(request, response);
             }
            catch(Exception ex){
			System.err.println("Can not show the all BALES page");
			}
            }
            
            //out.println("<h1>Servlet Controllerserv at " + request.getContextPath () + "</h1>");
            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
    */
    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
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

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