1

I have the following code and i want that the Asistir button disapear if the user is suscribed in one event, what should i write in servlet and jsp to make it work? thanks.

The jsp button

<a href="formularioAsiste.jsp?id=${ev.idEvento}" class="btn btn-default"> 
                        <span class="glyphicon glyphicon-check">Asistir</span>                      
                        </a>

The Servlet that show the table

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

            GestionAsistentes gestionAsistentes = new GestionAsistentes();

            GestionEventos gestionEventos = new GestionEventos();

            Collection<Evento> listaEventos = gestionEventos.list();

            Collection<Asiste> listaAsistentes = gestionAsistentes.list();

            request.setAttribute("asistentes", listaAsistentes);


            request.setAttribute("eventos", listaEventos);

            request.getRequestDispatcher("tabla.jsp").forward(request, response);
            // request.getRequestDispatcher("TablaEventosServlet").forward(request,
            // response);
        }

ListaAsistentes have the user that asist and the event related to that user, the code:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        request.setCharacterEncoding("UTF-8");

        String strIdEvento = request.getParameter("evento");
        Usuario asis = (Usuario) request.getSession().getAttribute("usuario");

        GestionEventos gesEven = new GestionEventos();

        Evento ev = gesEven.getEventoPorId(Integer.parseInt(strIdEvento));

        String nombreEntidad = request.getParameter("nombreEntidad");
        String nombreCuenta = request.getParameter("nombreCuenta");
        String iban = request.getParameter("iban");
        String numeroCuenta = request.getParameter("numeroCuenta");
        Date fechaPago = new Date();

        GestionAsistentes gestionAsistentes = new GestionAsistentes();



        Asiste asistente = new Asiste(nombreEntidad, nombreCuenta, iban, numeroCuenta, fechaPago);

        asistente.setPrimaryKey(new UsuarioEventoId(asis,ev));

        gestionAsistentes.addAsistente(asistente);

        request.setAttribute("asistentes", gestionAsistentes.list());       




        response.sendRedirect("TablaEventosServlet");
    }
Luke Garrigan
  • 4,571
  • 1
  • 21
  • 29
Jose
  • 310
  • 1
  • 12
  • A bit difficult for non-Spanish to read. Maybe add a more concrete explanation. A `request.setAttribute` should do, and in the JSP either a primitive `<% if (...) { %>...` or a nicer `...` for instance. – Joop Eggen Jun 15 '16 at 10:06
  • Thanks for your patience with language, i am trying to extract the users that attends to the event from a list (listaAsistentes) and if the logged user come to the event, don't show the button or something similar. – Jose Jun 15 '16 at 13:34

1 Answers1

0

There is 2 solutions :

1/ add a boolean parameter in the bean of user when user subscribe you set it (parameter)
2/ In the servlet when the user click subscibe you pass the control to a specific servlet it will set in his session a parameter you can call it whenever you want

this an example of solution N°2:

public class ExampleServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

//your code 



        request.getSession().setAttribute("subscribed", "isSubscribed");

        RequestDispatcher view = request.getRequestDispatcher("My.jsp");
        view.forward(request, response); 


    }}

then in your JSP

  <% String msg2=(String)request.getAttribute("subscribed");%>

        <% if ( (msg2==null || msg2.isEmpty()) ) { %> 
         <a href="ExampleServlet?id=${ev.idEvento}" class="btn btn-default"> 
                        <span class="glyphicon glyphicon-check">Asistir</span>                      
                        </a>           
        <% } else if(msg2!=null) { %>

         <% }  %>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Taha
  • 1,072
  • 2
  • 16
  • 29
  • FYI: using scriptlets is discouraged [since 2003](http://www.oracle.com/technetwork/articles/javase/code-convention-138726.html). Try catching up [current state of technology](http://stackoverflow.com/tags/el/info) and stop teaching starters bad practices. See also http://stackoverflow.com/q/3177733 – BalusC Jun 15 '16 at 11:06