Servlet
ArrayList<String[]> itemsInCart = new ArrayList<String[]>();
String[] test = {"bah","3.50","false"};
itemsInCart.add(test);
ArrayList<Integer> testALEmpty = new ArrayList<>();
ArrayList<Integer> testALItems = new ArrayList<>();
testALItems.add(1);
testALItems.add(2);
testALItems.add(3);
String testStr = "This is a test string";
request.setAttribute("testALEmpty", testALEmpty);
request.setAttribute("testALItems", testALItems);
request.setAttribute("testStr", testStr);
request.setAttribute("cartAttribute", itemsInCart);
try {
getServletContext().getRequestDispatcher("/Cart.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
JSP
if (request.getAttribute("cartAttribute") == null) {
%>
<b>No Cart</b>
<%
}
When the servlet forwards to the JSP, I have No Cart because for some reason the servlet is not passing the attributes to the JSP.
set request attribute to session attribute:
request.getSession().setAttribute("parameter", "test");
or
There are two ways you can accomplish this.
Using a JSP expression you would use <%= %> as (notice no ; at the end)
<%= parameter %>
The second and preferred way is to use JSP EL syntax and reference the request attribute directly using ${ } as
${parameter}
The first option requires you to pull the attribute out of its scope first. The second doesn't.
String parameter = (String) request.getAttribute("parameter");