nickdalyrendon
6 months ago
6 changed files with 289 additions and 0 deletions
@ -0,0 +1,88 @@ |
|||||
|
/* |
||||
|
* 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.chapala.sistemas.servicios; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.io.PrintWriter; |
||||
|
import javax.servlet.ServletException; |
||||
|
import javax.servlet.annotation.WebServlet; |
||||
|
import javax.servlet.http.HttpServlet; |
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* @author nickdalyrendon |
||||
|
*/ |
||||
|
@WebServlet(name = "ProveedorEndpoint", urlPatterns = {"/ProveedorEndpoint"}) |
||||
|
public class ProveedorEndpoint extends HttpServlet { |
||||
|
|
||||
|
/** |
||||
|
* 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()) { |
||||
|
/* TODO output your page here. You may use following sample code. */ |
||||
|
out.println("<!DOCTYPE html>"); |
||||
|
out.println("<html>"); |
||||
|
out.println("<head>"); |
||||
|
out.println("<title>Servlet ProveedorEndpoint</title>"); |
||||
|
out.println("</head>"); |
||||
|
out.println("<body>"); |
||||
|
out.println("Parametro"+request.getParameter("test")); |
||||
|
out.println("<h1>Servlet ProveedorEndpoint at " + request.getContextPath() + "</h1>"); |
||||
|
out.println("</body>"); |
||||
|
out.println("</html>"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// <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>
|
||||
|
|
||||
|
} |
@ -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.ProveedorBLLocal; |
||||
|
import mx.edu.tjs.chapala.sistemas.modelo.Proveedor; |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* @author nickdalyrendon |
||||
|
*/ |
||||
|
@WebServlet(name = "ProveedorEndpointt", urlPatterns = {"/ProveedorEndpointt"}) |
||||
|
public class ProveedorEndpointt extends HttpServlet { |
||||
|
|
||||
|
@EJB |
||||
|
private ProveedorBLLocal proveedorBL; |
||||
|
Proveedor 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 (!"tokenNickdaly160601".equals(token)) { |
||||
|
out.print("Acceso Denegado"); |
||||
|
|
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
switch (opcion) { |
||||
|
case "1": |
||||
|
m = proveedorBL.buscarIdInt(Integer.parseInt(request.getParameter("id"))); |
||||
|
if (m.getStatus() == 0) { |
||||
|
out.print("No existe el proveedor"); |
||||
|
} else { |
||||
|
out.print("<div style='font-family: Arial, sans-serif; padding: 20px;'>"); |
||||
|
out.print("<h1 style='color: blue;'>Proveedor encontrado</h1>"); |
||||
|
out.print("<h2 style='color: green;'>Nombre: " + m.getNombre() + "</h2>"); |
||||
|
out.print("</div>"); |
||||
|
} |
||||
|
|
||||
|
break; |
||||
|
|
||||
|
case "2": |
||||
|
m = proveedorBL.buscarIdInt(Integer.parseInt(request.getParameter("id"))); |
||||
|
if (m.getStatus() == 0) { |
||||
|
out.print("No existe ese elemento"); |
||||
|
} else { |
||||
|
proveedorBL.eliminarP(m); |
||||
|
out.print("Se elimino el proveedor"); |
||||
|
} |
||||
|
break; |
||||
|
|
||||
|
case "3": |
||||
|
Proveedor nuevo = new Proveedor(); |
||||
|
nuevo.setStatus((short) 1); |
||||
|
nuevo.setNombre(request.getParameter("nombre")); |
||||
|
proveedorBL.agregarP(nuevo); |
||||
|
out.print("Se agrego el proveedor"); |
||||
|
break; |
||||
|
|
||||
|
|
||||
|
case "4": |
||||
|
|
||||
|
try { |
||||
|
int id = Integer.parseInt(request.getParameter("id")); |
||||
|
Proveedor proveedorExistente = proveedorBL.buscarIdInt(id); |
||||
|
|
||||
|
if (proveedorExistente == null || proveedorExistente.getStatus() == 0) { |
||||
|
out.print("No existe el proveedor"); |
||||
|
} else { |
||||
|
String nuevoNombre = request.getParameter("nombre"); |
||||
|
|
||||
|
// Actualizar los campos necesarios
|
||||
|
if (nuevoNombre != null && !nuevoNombre.isEmpty()) { |
||||
|
proveedorExistente.setNombre(nuevoNombre); |
||||
|
} |
||||
|
|
||||
|
proveedorBL.editar(proveedorExistente); |
||||
|
out.print("Editado con éxito"); |
||||
|
} |
||||
|
} catch (NumberFormatException e) { |
||||
|
out.print("ID de proveedor no válido"); |
||||
|
} catch (Exception e) { |
||||
|
out.print("Ocurrió un error al editar el proveedor"); |
||||
|
} |
||||
|
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