We study the Josephson effect in short ballistic SINIS and SIFIS double-barrier junctions, consisting of clean superconductors (S), a normal metal (N) or ferromagnet (F), and insulating interfaces (I). For SINIS double-tunnel junctions, the critical Josephson current as a function of the junction width shows sharp peaks because of resonant amplification of the Andreev process when the quasi-bound states of the normal interlayer enter the superconducting gap and morph into phase-sensitive bound states. For SIFIS double-tunnel junctions the corresponding quasi-bound states are spin-split, they amplify the supercurrent less efficiently, and trigger transitions between 0 and π states of the junction. In contrast to SINIS junctions where the critical current reaches a peak value when the Andreev bound states cross the Fermi surface, here a narrow dip opens up exactly at the peak due to compensation of partial currents flowing in opposite directions. With increased barrier transparency, the described mechanism is modified by the broadening and overlap of quasi-bound states. Temperature-induced transitions both from 0 to π and from π to 0 states are studied by computing the phase diagram (with temperature and junction width as the variables) for different interfacial transparencies varying from transparent metallic to the tunnel limit.