hugoa
6 months ago
2 changed files with 159 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 xforce.servicios; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.PrintWriter; |
|||
import java.util.List; |
|||
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 xforce.bl.MarcaBLLocal; |
|||
import xforce.dao.LoginDAO; |
|||
import xforce.modelo.Marca; |
|||
import xforce.modelo.Usuarios; |
|||
import xforce.msg.Mensaje; |
|||
|
|||
/** |
|||
* |
|||
* @author hugoa |
|||
*/ |
|||
@WebServlet(name = "MarcaEndPoint", urlPatterns = {"/MarcaEndPoint"}) |
|||
public class MarcaEndPoint extends HttpServlet { |
|||
|
|||
@EJB |
|||
private MarcaBLLocal marcaBL; |
|||
|
|||
/** |
|||
* 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. */ |
|||
|
|||
// Obtener el token de la solicitud
|
|||
String token = request.getParameter("token"); |
|||
|
|||
// Verificar si el token es válido
|
|||
LoginDAO loginDAO = new LoginDAO(); |
|||
Usuarios usuario = new Usuarios(); |
|||
usuario.setToken(token); |
|||
String s = loginDAO.tokenEnd(token); |
|||
System.out.println(s); |
|||
|
|||
if (s == null) { |
|||
// Token no válido
|
|||
out.println("<h1>Token no valido</h1>"); |
|||
//response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token no válido");
|
|||
List<Marca> listaMarcas = marcaBL.buscarValidos(true); |
|||
if (listaMarcas != null && !listaMarcas.isEmpty()) { |
|||
for (Marca marca : listaMarcas) { |
|||
out.println("<p>" + marca.getId() + " || " + marca.getNombre() + "</p>"); |
|||
} |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
Marca marcas = new Marca(); |
|||
|
|||
String opc = request.getParameter("opc"); |
|||
String id = request.getParameter("id"); |
|||
String nombre = request.getParameter("nombre"); |
|||
Mensaje m = null; |
|||
|
|||
if (opc != null) { |
|||
|
|||
switch (Integer.parseInt(opc)) { |
|||
case 1: |
|||
marcas.setNombre(nombre); |
|||
m = marcaBL.agregar(marcas); |
|||
break; |
|||
case 2: |
|||
|
|||
marcas.setId(Integer.valueOf(id)); |
|||
|
|||
marcas.setNombre(nombre); |
|||
|
|||
m = marcaBL.editar(marcas); |
|||
break; |
|||
|
|||
case 3: |
|||
|
|||
marcas.setId(Integer.valueOf(id)); |
|||
|
|||
m = marcaBL.eliminar(marcas); |
|||
|
|||
break; |
|||
default: |
|||
throw new AssertionError(); |
|||
} |
|||
} |
|||
|
|||
List<Marca> listaMarcas = marcaBL.buscarValidos(true); |
|||
out.print("<h1>" + m + "</h1>"); |
|||
if (listaMarcas != null && !listaMarcas.isEmpty()) { |
|||
for (Marca marca : listaMarcas) { |
|||
out.println("<p>" + marca.getId() + " || " + marca.getNombre() + "</p>"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
// <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