martes agosto 12, 2003
public class MiServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String action = request.getParameter("action");
if(action.equals("alta")){
// codigo del alta
}else if(action.equals("baja")){
// codigo para dar de baja
else if (....){
}....
}
}
public interface Accion {
public String accion();
}
public class AccionFactory {
public static Accion getAccion(String accion){
Accion action = null;
try {
action = (Accion)Class.forName(accion).newInstance();
} catch (InstantiationException e) {
System.out.println("Error al instanciar la clase");
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println("Acceso Ilegal a no se que ¿?");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("Tas tonto esa clase no existe");
e.printStackTrace();
}
return action;
}
}
public class MapeoAccionesDAO {
private Properties mapa;
public MapeoAccionesDAO(String configuracion){
mapa = new Properties();
try {
InputStream iStream = new FileInputStream(configuracion);
mapa.load(iStream);
} catch (FileNotFoundException e) {
System.out.println("Error fichero no encontrado");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Error IOException no se porque da este error");
e.printStackTrace();
}
}
public String getNombreAccion(String requesURI){
return mapa.getProperty(requesURI);
}
}
public class Controlador extends HttpServlet {
/*
* Usaremos un objeto que nos mapee las URI con las
* clases accion asociadas a esa URI
*/
private MapeoAccionesDAO acciones;
public void init(ServletConfig config) throws ServletException {
super.init(config);
/*
* Inicializamos el mapeo usando un patron DAO
*/
ServletContext sc = config.getServletContext();
String configuracion = sc.getRealPath("configuracion.properties");
acciones = new MapeoAccionesDAO(configuracion);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
this.procesar(request,response);
}
protected void doPost( HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
this.procesar(request,response);
}
private void procesar( HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
Accion action = null;
/*
* Extraemos la URI de la solicitud
* y se la pasamos al properties como clave
*/
String accion = acciones.getNombreAccion(request.getRequestURI());
System.out.println("Voy a realizar la accion : " + accion);
/*
* Instanciamos la clase que nos devuelve el properties
* y usamos este como zona de mapeo
* y todo esto usando un patron Factory
*/
action = AccionFactory.getAccion(accion);
String nuevaURL = action.accion();
response.sendRedirect(nuevaURL);
}
}
comprar.do=comprarAccion
/mittienda/elegir.do=org.mitienda.elegirAccion
/mittienda/devolver.do=org.mitienda.devolveAccion
/mittienda/buscar.do=org.mitienda.buscarAccion
/mittienda/inicio.do=inicioAccion