Passing attributes from a Servlet to a JSP

Asked

Viewed 577 times

-1

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

In my Servlet, I am 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 it’s not showing the ID on JSP. Could someone help me?

  • Haha sorry, the truth is that this up there is not working, IE, the id does not appear in JSP

3 answers

1

Instead of putting:

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

Try:

value="${id_moto}"
  • Hello this would only work if I was using taglib...

  • then try to store in a Avascript variable and then return this variable. usually I use Taglib makes things much easier.

  • http://www.guj.com.br/t/chamando-um-getattribute-em-uma-pagina-jsp/46266/4 See if this helps you.

  • value="<%=request.getattribute("id_moto"); %>" Try now. I think I missed the =

0

Here’s what you used:

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

Note that you used double quotes both inside and outside, so these quotes do not open and close where you think they would. Also, missed the = after the first %.

If you used this:

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

That is, with simple quotes on the outside and with the =, then it would work.

0

I managed to solve the problem, in Servlet stayed 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 100% functional!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.