1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
   | package fonctions;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modeles.MdAbonnes;
import exception.Ca_chie;
/**
 * Servlet implementation class emprunts
 */
public class emprunts extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public static final String KEY_VALUE = "value";
    public static final String KEY_DELAY = "delay";
    public static final String VAR_LIST = "list";
    public static final String VIEW_RESOURCE =
            "biblio/list.jsp";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public emprunts() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(request, response);
		doPost( request, response );
		
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(request, response);
		try{
			
			
			//
	        // If a delay was requested, cause the delay to occur
	        //
	        if (request.getParameter( KEY_DELAY ) != null) {
	            delay(request.getParameter( KEY_DELAY ));
	        }
	        //
	        // Get the passed prefix and obtain the list of matches
	        //
	        String prefix = request.getParameter( KEY_VALUE );
	        List<String> matches = findMatches( prefix );
	        //
	        // Set the matches as a scoped variable on the request
	        // and shuffle off to the JSP
	        //
	        request.setAttribute( VAR_LIST, matches );
	        request.getRequestDispatcher( VIEW_RESOURCE)
	            .forward( request, response );
	}
		catch (Ca_chie e) {
			try {
				
				
				request.setAttribute("message", e.toString());
				
				getServletContext().getRequestDispatcher("/erreur.jsp").forward(request,response);
				
			} catch (ServletException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}
	
	private void delay( String delayValue ) {
        try {
            Thread.sleep( Integer.parseInt( delayValue ) );
        }
        catch (Exception e) {
            //On failure, perform no delay
        }
    }
	private List<String> abonne() throws Ca_chie{
    
		List<MdAbonnes> liste= new ArrayList<MdAbonnes>();
		List<String> lstr= new ArrayList<String>();
		for (MdAbonnes a : liste)
		   lstr.add(a.getNom());
    return lstr;
	}
    private List<String> findMatches( String prefix ) throws Ca_chie {
    	
    	List<String> matches = new ArrayList<String>();
    	for (String choice : abonne()) {
            if (choice.startsWith( prefix )) matches.add( choice );
        }
        return matches;
    }
} | 
Partager