Classe Banco, ja adiciona duas empresas no meu list
package br.com.alura.servelet;
import java.util.ArrayList;
import java.util.List;
public class BancoDados {
private static List<Empresa> lista = new ArrayList<>();
static {
Empresa obj = new Empresa();
obj.setNome("Alura");
Empresa obj2 = new Empresa();
obj2.setNome("Caelum");
BancoDados.lista.add(obj);
BancoDados.lista.add(obj2);
}
public void adiciona(Empresa empresa) {
BancoDados.lista.add(empresa);
}
public List<Empresa> getEmpresa(){
return BancoDados.lista;
}
}
Classe recupera o list e coloca dentro de uma Servlet
package br.com.alura.servelet;
import java.io.IOException;
import java.io.PrintWriter;
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 com.sun.media.sound.RealTimeSequencerProvider;
/**
* Servlet implementation class ListaEmpresa
*/
@WebServlet("/listaEmpresa")
public class ListaEmpresa extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
BancoDados banco = new BancoDados();
List<Empresa> lista = banco.getEmpresa();
PrintWriter out = response.getWriter();
request.setAttribute("empresas", lista);
RequestDispatcher rd = request.getRequestDispatcher("/list.jsp");
rd.forward(request, response);
}
}
Pega os resultados da Servelet e imprime em HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.List, br.com.alura.servelet.Empresa"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<ul>
<%
List<Empresa> lista =(List<Empresa>)request.getAttribute("empresas");
for(Empresa empresa : lista) {
%>
<li> <%= empresa.getNome()%></li>
<%
}
%>
</ul>
</body>
</html>
E apresenta o seguinte erro.
HTTP Status 500 – Internal Server Error
Type Exception Report
Message An exception occurred processing [list.jsp] at line [16]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing [list.jsp] at line [16]
13: <%
14:
15: List<Empresa> lista =(List<Empresa>)request.getAttribute("empresas");
16: for(Empresa empresa : lista) {
17:
18: %>
19: <li> <%= empresa.getNome()%></li>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:625)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:514)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NullPointerException
org.apache.jsp.list_jsp._jspService(list_jsp.java:135)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)