Home > OS >  Problems with cli.get() and getting attributes in JSP and Servlet
Problems with cli.get() and getting attributes in JSP and Servlet

Time:11-27

Good morning, I have this problem. I have a java class "Cliente" with the corresponding data (I leave you the code)

package Clases;

public class Cliente {
    public Cliente(String dni, String nombre, String apellido, String telefono){
    }
}

Then, I have this Servlet with DoGet where I generate a list of Cliente and get my session, and a dopost that asks for the parameters:

package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.util.*;
import Clases.Cliente;


@WebServlet(name = "SvPrueba", urlPatterns = {"/SvPrueba"})
public class SvPrueba extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

    }
    

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        List<Cliente> listaClientes = new ArrayList<> ();
        listaClientes.add(new Cliente("12345678", "Luisina", "de Paula", "444222357"));        
        listaClientes.add(new Cliente("46325965", "Avril", "Lavigne", "774568931"));        
        listaClientes.add(new Cliente("69584123", "Gianluigi", "Guidicci", "4567531654"));
        HttpSession misession = request.getSession();
        misession.setAttribute("listaClientes", listaClientes);
        response.sendRedirect("MostrarJSP.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String dni = request.getParameter("dni");
        String nombre = request.getParameter("nombre");
        String apellido = request.getParameter("apellido");
        String telefono = request.getParameter("telefono");
        processRequest(request, response);
    }   

     @Override
    public String getServletInfo() {
        return "Short description";
    }
    

}

Finally, I have a JSP where it presents the error: In it I bring the session and ask for the attributes to write them. However, on every "cli.get" I get cannot find symbol error

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Clientes</title>
    </head>
    <body>
        <h1>Lista de Clientes</h1>
        <%@ page import="java.util.List" %>
        <%@ page import="Clases.Cliente" %>
        <%
            List<Cliente> listaClientes = (List) request.getSession().getAttribute("listaClientes");
            int cont=1;
            for (Cliente cli : listaClientes) { %>
                <p><b>Cliente Nº <%=cont%></b></p>
                <p>Dni: <%=cli.getDni()%></p>
                <p>Nombre: <%=cli.getNombre()%></p>
                <p>Apellido: <%=cli.getApellido()%></p>
                <p>Teléfono: <%=cli.getTelefono()%></p>
            <% cont= cont 1;%>
        <%}%>
    </body>
</html>

I'm starting in Java and I can't fix this :(

I hope that getDni, getNombre, etc. will bring me the data and write it in the HTML code, but I have some error and I don't know what it is. Thank you ^^

CodePudding user response:

The Cliente class is empty (apart from an empty constructor that does nothing by itself if you don't tell it what to do). You have to define both the data it contains and the methods to get the data. Something like this:

public class Cliente {
    // the data in the class
    String dni;
    String nombre;
    String apellido;
    String telefono;

    // manually set all members here
    public Cliente(String dni, String nombre, String apellido, String telefono) {
        this.dni = dni;
        this.nombre = nombre;
        this.apellido = apellido;
        this.telefono = telefono;
    }

    // getter methods are not created automatically, you have to define them
    public String getDni() { return dni; }
    public String getNombre() { return nombre; }
    public String getApellido() { return apellido; }
    public String getTelefono() { return telefono; }
}
  • Related