nayelivilchismariscal
6 months ago
10 changed files with 1588 additions and 1392 deletions
@ -0,0 +1,158 @@ |
|||
/* |
|||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|||
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
|
|||
*/ |
|||
package mx.edu.tjs.servicios; |
|||
|
|||
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; |
|||
import mx.edu.tjs.chapala.sistemas.bl.CategoriaBLLocal; |
|||
import mx.edu.tjs.chapala.sistemas.modelo.Categoria; |
|||
|
|||
/** |
|||
* |
|||
* @author nayelivilchismariscal |
|||
*/ |
|||
@WebServlet(name = "CategoriaEndpoint", urlPatterns = {"/CategoriaEndpoint"}) |
|||
public class CategoriaEndpoint extends HttpServlet { |
|||
@EJB |
|||
private CategoriaBLLocal categoriaBL; |
|||
Categoria m ; |
|||
/** |
|||
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
|||
* methods. |
|||
* |
|||
* @param request servlet request |
|||
* @param response servlet response |
|||
* @throws ServletException if a servlet-specific error occurs |
|||
* @throws IOException if an I/O error occurs |
|||
*/ |
|||
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
|||
throws ServletException, IOException { |
|||
response.setContentType("text/html;charset=UTF-8"); |
|||
try (PrintWriter out = response.getWriter()) { |
|||
|
|||
String opcion = request.getParameter("opcion"); |
|||
// Verificar el token
|
|||
String token = request.getParameter("t"); |
|||
if (!"h978HIJG87FD5757huuyujJJKAYtyywedh".equals(token)) { |
|||
out.print("Acceso Denegado"); |
|||
|
|||
return; |
|||
} |
|||
|
|||
switch(opcion){ |
|||
case "1": |
|||
m = categoriaBL.buscarIdInt(Integer.parseInt(request.getParameter("id"))); |
|||
if(m.getStatus() == 0){ |
|||
out.print("No existe la categoria"); |
|||
}else{ |
|||
out.print("<div style='font-family: Arial, sans-serif; padding: 20px;'>"); |
|||
out.print("<h1 style='color: purple;'>Categoria </h1>"); |
|||
out.print("<h2 style='color: blue;'>Nombre: " + m.getNombre() + "</h2>"); |
|||
out.print("</div>"); |
|||
} |
|||
|
|||
break; |
|||
case "2": |
|||
m = categoriaBL.buscarIdInt(Integer.parseInt(request.getParameter("id"))); |
|||
if(m.getStatus() == 0){ |
|||
out.print("No existe ese elemento"); |
|||
}else{ |
|||
categoriaBL.eliminarC(m); |
|||
out.print("Se elimino la categoria"); |
|||
} |
|||
break; |
|||
case "3": |
|||
Categoria nuevo = new Categoria(); |
|||
nuevo.setStatus((short) 1); |
|||
nuevo.setNombre(request.getParameter("nombre")); |
|||
categoriaBL.agregarC(nuevo); |
|||
out.print("Se agrego la categoria"); |
|||
break; |
|||
case "4": |
|||
|
|||
try { |
|||
int id = Integer.parseInt(request.getParameter("id")); |
|||
Categoria categoriaExistente = categoriaBL.buscarIdInt(id); |
|||
|
|||
if (categoriaExistente == null || categoriaExistente.getStatus() == 0) { |
|||
out.print("No existe la categoria"); |
|||
} else { |
|||
String nuevoNombre = request.getParameter("nombre"); |
|||
|
|||
// Actualizar los campos necesarios
|
|||
if (nuevoNombre != null && !nuevoNombre.isEmpty()) { |
|||
categoriaExistente.setNombre(nuevoNombre); |
|||
} |
|||
|
|||
// Aquí puedes agregar más campos si es necesario
|
|||
// marcaExistente.setOtroCampo(request.getParameter("otroCampo"));
|
|||
|
|||
categoriaBL.editar(categoriaExistente); |
|||
out.print("Editado con éxito"); |
|||
} |
|||
} catch (NumberFormatException e) { |
|||
out.print("ID de marca no válido"); |
|||
} catch (Exception e) { |
|||
out.print("Ocurrió un error al editar la marca"); |
|||
} |
|||
break; |
|||
|
|||
|
|||
default: |
|||
out.print("<h1> No existe esa opcion </h1>"); |
|||
break; |
|||
|
|||
} |
|||
|
|||
|
|||
} |
|||
} |
|||
// <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>
|
|||
} |
|||
|
|||
|
Loading…
Reference in new issue