jsp文件中的Dispatly servlet arraylist不起作用

问题描述 投票:0回答:1

我不知道为什么我的jsp页面不会显示我在servlet中创建并添加到会话的列表。任何帮助将不胜感激。我看了很多答案,但找不到解决方法。

我得到的输出是;

艺术家信息摘要

姓氏姓氏出生

因此将显示表标题,而不是实际值。

美术课

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>

}

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>
java jsp servlets
1个回答
0
投票

您正在如下所示在会话变量中设置名称为info的手动创建的艺术家列表。

session.setAttribute("info", list);

如果要在jsp上显示此数据,则需要在info循环中使用名称为c:forEach的相同变量,如下所示。

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

还要密切注意上面为每个JSTL使用的属性名,名字等。

© www.soinside.com 2019 - 2024. All rights reserved.