Passing Attributes from a Servlet to a JSP [Closed]

-1

Hello. I'm developing a system where the user clicks an edit button and then opens the edit page with the attributes of the object already filled in the text boxes.

In my servlet, I'm using the following logic:

Moto escolhida = new Moto();

try {
    MotoDao dao = new MotoDao();
    List<Moto> motos = dao.consulta();
    //monta o objeto moto a partir do id_moto
    for (Moto m : motos) {
        if (id_moto == m.getId()) {
            escolhida = m;
        }
    }

System.out.println("dados da moto: "+ escolhida.getId()+" / "+escolhida.getModelo() );
//passa os atributos da moto escolhida para a jsp

request.setAttribute("id_moto", escolhida.getId());
request.setAttribute("marca", escolhida.getMarca());
request.setAttribute("modelo", escolhida.getModelo());
request.setAttribute("potencia", escolhida.getPotencia());
request.setAttribute("ano", escolhida.getAno());
request.setAttribute("valor", escolhida.getValor());

request.getRequestDispatcher("editar.jsp").forward(request, response);

In my JSP:

<td> ID:</td> <td>  <input type="text" name="id_moto"  value="<%request.getAttribute("id_moto"); %>" required  /></td>

In theory, this would work, but the ID in the JSP is not appearing. Could someone help me?

    
asked by anonymous 03.10.2017 / 13:50

3 answers

1

Instead of putting:

value="<%request.getAttribute("id_moto"); %>"

Try:

value="${id_moto}"
    
03.10.2017 / 19:45
0

I was able to solve the problem, in the servlet it was this way:

int id_moto = Integer.valueOf(request.getParameter("id_moto"));
        System.out.println("Você clicou no botão editar, id da moto: " + id_moto);
        Moto escolhida = new Moto();

        try {
            MotoDao dao = new MotoDao();
            List<Moto> motos = dao.consulta();
            //monta o objeto moto a partir do id_moto
            for (Moto m : motos) {
                if (id_moto == m.getId()) {
                    escolhida = m;
                }
            }

            System.out.println("dados da moto: " + escolhida.getId() + " / " + escolhida.getModelo());

            //passa o objeto moto escolhida para a jsp
            request.setAttribute("moto", escolhida);

            RequestDispatcher rd = request.getRequestDispatcher("editar.jsp");

            rd.forward(request, response);

and in Jsp I did the following treatment:

<%@page import="Model.Moto"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.http.*"%>

<% //estou criando uma variável do mesmo do tipo do atributo 
    Moto moto = new Moto();
    moto = (Moto) request.getAttribute("moto");
%>



<tr> <td> ID:</td> <td>  <input type="text" name="id_moto"  value="<%= moto.getId() %>"  disabled="true"   required  /></td> </tr>

This is 100% functional!

    
03.10.2017 / 20:01
0

Here's what you used:

value="<%request.getAttribute("id_moto"); %>"

Note that you used double quotation marks both inside and out, so those quotes do not open and close where you think they would. Additionally, = was missing after the first % .

If you used this:

value='<%= request.getAttribute("id_moto"); %>'

That is, with single quotation marks outside and with = , that would work.

    
10.10.2017 / 22:19