I can't figure out why my jsp page won't display a list I created in a servlet and added to the session. Any help would be greatly appreciated. I've looked at a lot of answers but I can't find one to fix this.
The output I'm getting is;
Summary of artist information
Surname Fname nationality birth
So the table headings are being displayed but not the actual values.
Artist Class
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 file
<%@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>