jazmin
6 months ago
1 changed files with 155 additions and 0 deletions
@ -0,0 +1,155 @@ |
|||
/* |
|||
* 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.tsj.chapala.sistemas.jin.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.tsj.chapala.sistemas.jin.bl.MarcaBLLocal; |
|||
import mx.edu.tsj.chapala.sistemas.jin.modelo.Marca; |
|||
|
|||
/** |
|||
* |
|||
* @author jazmin |
|||
*/ |
|||
@WebServlet(name = "marcaServlet", urlPatterns = {"/marcaServlet"}) |
|||
public class marcaServlet extends HttpServlet { |
|||
|
|||
@EJB |
|||
private MarcaBLLocal marcaBL; |
|||
private static final String TOKEN = "4j*Lz8&mQ1^sT2n@Ew7#Vb9X%Rp6Aa"; |
|||
Marca 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("Opc"); |
|||
|
|||
|
|||
switch(opcion){ |
|||
case "1": |
|||
m = marcaBL.buscarIdInt(Integer.parseInt(request.getParameter("id"))); |
|||
if(m.getStatus() == 0){ |
|||
out.print("No existe esa marca"); |
|||
}else{ |
|||
out.print("<h1> Nombre: "+m.getNombre()+"</h1>"); |
|||
} |
|||
|
|||
break; |
|||
case "2": |
|||
m = marcaBL.buscarIdInt(Integer.parseInt(request.getParameter("id"))); |
|||
if(m.getStatus() == 0){ |
|||
out.print("No existe ese elemento"); |
|||
}else{ |
|||
marcaBL.Eliminar(m); |
|||
out.print("Eliminado con éxito"); |
|||
} |
|||
break; |
|||
case "3": |
|||
Marca nuevo = new Marca(); |
|||
nuevo.setStatus((short) 1); |
|||
nuevo.setNombre(request.getParameter("nombre")); |
|||
|
|||
marcaBL.Agregar(nuevo); |
|||
out.print("Agregado con éxito"); |
|||
break; |
|||
case "4": |
|||
|
|||
try { |
|||
int id = Integer.parseInt(request.getParameter("id")); |
|||
Marca marcaExistente = marcaBL.buscarIdInt(id); |
|||
|
|||
if (marcaExistente == null || marcaExistente.getStatus() == 0) { |
|||
out.print("No existe esa marca"); |
|||
} else { |
|||
String nuevoNombre = request.getParameter("nombre"); |
|||
|
|||
// Actualizar los campos necesarios
|
|||
if (nuevoNombre != null && !nuevoNombre.isEmpty()) { |
|||
marcaExistente.setNombre(nuevoNombre); |
|||
} |
|||
|
|||
// Aquí puedes agregar más campos si es necesario
|
|||
// marcaExistente.setOtroCampo(request.getParameter("otroCampo"));
|
|||
|
|||
marcaBL.Editar(marcaExistente); |
|||
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