Created
June 1, 2010 14:55
-
-
Save dbonillaf/421028 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.six.test; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.ejb.EJB; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* Servlet de todo a 100 que sirve como ruinosa fachada para nuestros EJBs | |
* @author McGyver | |
*/ | |
@WebServlet(name = "RenombraServlet", urlPatterns = {"/RenombraServlet"}) | |
public class RenombraServlet extends HttpServlet { | |
@EJB | |
private Renombrador bean; | |
@EJB | |
private ActualizaCliente session; | |
/** | |
* Procesa peticiones HTTP de tipo <code>GET</code> y <code>POST</code>. | |
* @param request Servlet request | |
* @param response Servlet response | |
* @throws ServletException si existe algún error en el funcionamiento del Servlet | |
* @throws IOException si existe algún error de escritura/lectura | |
*/ | |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
response.setContentType("text/html;charset=UTF-8"); | |
PrintWriter out = response.getWriter(); | |
try { | |
String nuevo_nombre = bean.renombra(); | |
// Este operador ternario esta dedicado, con amor, a Xavi Gost (@XaV1uzz) | |
int tipoBloqueo = | |
esUnNumero(request.getParameter("lock")) ? | |
Integer.parseInt(request.getParameter("lock")) : 0; | |
out.println("<html>"); | |
out.println("<head>"); | |
out.println("<title>Renombrando a un Cliente</title>"); | |
out.println("</head>"); | |
out.println("<body>"); | |
out.println("<h1>Servlet " + request.getContextPath() + "/RenombraServlet - " + nuevo_nombre + "</h1>"); | |
out.println("<br/>"); | |
try { | |
session.renombra(nuevo_nombre, tipoBloqueo); | |
out.println("<p>Renombrado correcto</p>"); | |
} catch (Throwable e) { | |
out.println("<p>Upps !!!: " + e + "</p>"); | |
out.println("<p>"); | |
e.printStackTrace(out); | |
out.println("</p>"); | |
} | |
out.println("</body>"); | |
out.println("</html>"); | |
} finally { | |
out.close(); | |
} | |
} | |
/** | |
* Metodo estupido que he creado para no tener que importar ni una | |
* libreria de Commons de Apache. Reinventing the rueda !!! | |
* @param texto la cadena de texto de la que queremos averiguar si es un número | |
* @return true si el texto expresa un número | |
*/ | |
private boolean esUnNumero(String texto) { | |
boolean respuesta = false; | |
try { | |
if (texto != null) { | |
Integer.parseInt(texto); | |
respuesta = true; | |
} | |
} catch (NumberFormatException ex) { | |
// Este catch vacio se vende como espacio patrocinado | |
// Anunciante, si quiere exponer sus productos aquí | |
// contacte con [email protected] | |
} | |
return respuesta; | |
} | |
// <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 | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
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 | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
/** | |
* Returns a short description of the servlet. | |
* @return a String containing servlet description | |
*/ | |
@Override | |
public String getServletInfo() { | |
return "Short description"; | |
}// </editor-fold> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment