Está en: | Inicio >> ISValidator >> Ejemplos >> Servlet | |
![]() | ![]() |
Ejemplos de ISValidatorEsta página recoge unos ejemplos de uso de las rutinas ISValidator. Puede así mismo ver los ejemplos que se incluyen en el paquete para descargar. SituaciónSupongamos que queremos realizar un programa que funcione bajo la línea de comandos del Sistema Operativo para mandar emails y registrarlos en una base de datos. A este programa se le deben pasar cuatro parámetros:
Si alguna de estas restricciones no se cumpliese se deberá mostrar un mensaje por pantalla indicando todas las condiciones incumplidas. EjemploEste ejemplo está realizado sobre la versión de las rutinas 0.0.9 La adaptación a la versión 0.0.10 es muy fácil ya que solo implica cambiar los contenedores. Se ha dejado este ejemplo para poder comprobar las diferencias entre las dos versiones y la mejora que supone esta última. public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); res.setContentType("text/plain"); try { //For the email ServletParameterConstraintContainer theEmail = new ServletParameterConstraintContainer("email", req); theEmail.addConstraint(new EmailConstraint()); theEmail.addConstraint(new RegularExpresionConstraint(".{1,100}")); //For the subject ServletParameterConstraintContainer theSubject = new ServletParameterConstraintContainer("subject", req); theSubject.addConstraint(new RegularExpresionConstraint(".{1,255}")); //For the Date ServletParameterConstraintContainer theDate = new ServletParameterConstraintContainer("date", req); theDate.addConstraint(new DateConstraint()); //For the Body ServletParameterConstraintContainer theBody = new ServletParameterConstraintContainer("body", req); theBody.addConstraint(new RegularExpresionConstraint(".{1,1000}")); //The meta container SimpleMetaContainer inputParameters = new SimpleMetaContainer(); inputParameters.addContainer(theEmail); inputParameters.addContainer(theSubject); inputParameters.addContainer(theDate); inputParameters.addContainer(theBody); if (inputParameters.checkAll()) { out.println("All Ok"); out.println("The Email is: " + theEmail.getValueToCheck()); out.println("The Subject is: " + theSubject.getValueToCheck()); out.println("The Date is: " + theDate.getValueToCheck()); out.println("The Body is: " + theBody.getValueToCheck()); } else { out.println("Not Ok"); Hashtable errorMessages = new Hashtable(); errorMessages.put("EmailConstraint", "The argument ($valueToCheck;) is not a valid email"); errorMessages.put("RegularExpresionConstraint", "The argument ($valueToCheck;) has an invalid size"); errorMessages.put("DateConstraint", "The argument ($valueToCheck;) is not a valid date"); errorMessages.put("NotNullConstraint", "You haven´t put all the parameters"); errorMessages.put("NotBlankConstraint", "You have put a blank parameter"); inputParameters.setInvalidConstraintProcesor(new SimpleInvalidConstraintProcesor(errorMessages)); InvalidConstraintProcesorContainer errorContainer = inputParameters.getInvalidConstraintProcesorContainer(); Enumeration iterator = errorContainer.elements(); while (iterator.hasMoreElements()) { out.println(((InvalidConstraintProcesor) iterator.nextElement()).getMessage()); } } } catch (Exception e) { e.printStackTrace(out); } } |
|