ArrayList servlet de la exhibición en el archivo JSP no funciona

user12114019:

No puedo entender por qué mi página JSP no mostrará una lista que he creado en un servlet y se añade a la sesión. Cualquier ayuda sería muy apreciada. He mirado en muchas respuestas, pero no puedo encontrar uno para solucionar este problema.

La salida que estoy recibiendo es;

Resumen de la información del artista

Apellido NombreA nacimiento la nacionalidad

Así se visualizan los encabezados de la tabla, pero no los valores reales.

artista Clase

public class Artist {
     private String firstname;
    private String surname;
    private String nationality;
    private int birthyear;



    public Artist(String F, String S, String N, int I){
        this.firstname=F;
        this.surname=S;
        this.nationality=N;
        this.birthyear=I;

    }

    public String getFirstName(){
        return firstname;
    }



        public String getSurname(){
        return surname;
    }
          public String getNationality(){
        return nationality;
    }

        public int getBirth(){
        return birthyear;
        }  

}


servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;

/**
 *
 * @author User
 */
@WebServlet(urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {

    /**
     * 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()) {


            Artist A1 = new Artist("smith", "john", "american", 1999);
            Artist A2 = new Artist("pan", "peter", "canadian", 1956);

            List<Artist> list = new ArrayList<Artist>();
            list.add(A1);
            list.add(A2);

              HttpSession session = request.getSession(); 
        session.setAttribute("info", list);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/newjsp.jsp");
        dispatcher.forward(request,response);

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <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>

}

archivo JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head></head>
      <body>
        <h1>Summary of artist information</h1>
        <table>
            <thead>
            <tr>
                <th>Surname</th>
                <th>Fname</th>
                <th>nationality</th>
                <th>birth</th>


            </thead>
           <c:forEach items="${art}" var="artist">
                <tr>
                    <td>${artist.Surname}</td>
                    <td>${artist.Firstname}</td>
                    <td>${artist.Nationality}</td>
                    <td>${artist.BirthYear}</td>
                </tr>
                </c:forEach>



        </table>
    </body>
</html>
Shaikh amarilla:

Está tratando de ajustar su lista de artista creó manualmente en la variable de sesión de la siguiente manera con el nombre info.

session.setAttribute("info", list);

Si desea mostrar estos datos en JSP que necesita usar la misma variable con el nombre infoen su c:forEachbucle, como se muestra a continuación.

<c:forEach items="${info}" var="artist">
                <tr>
                    <td>${artist.surname}</td>
                    <td>${artist.firstname}</td>
                    <td>${artist.nationality}</td>
                    <td>${artist.birthYear}</td>
                </tr>
  </c:forEach>

También debe prestar mucha atención a los nombres de propiedades apellido, apellido, etc. utilizado en anterior para cada JSTL.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=345429&siteId=1
Recomendado
Clasificación