Try not to write java code in jsp. That was the way JSP was written a long time ago when it first came out. Later it was found to be very difficult to maintain and modify, so the current type of EL expression ${} appeared.

Writing method in ancient times (java code and html code are directly mixed and written):
<body>
<h1>购物车</h1>
<table border="1">
<tr>
<th>商品名称</th>
<th>商品数量</th>
</tr>
<%
Map<String,Integer> cart = (Map<String,Integer>)session.getAttribute("cart");
if(cart!=null && cart.size()>0){
for(Entry<String,Integer> en : cart.entrySet()){
%>
<tr>
<td><%=en.getKey() %></td>
<td><%=en.getValue() %></td>
</tr>
<% }
}
%>
</table>
</body>
Modern writing method (JSTL tag )
<table border="1">
<tr>
<th>用户名</th>
<th>当前遍历索引</th>
<th>当前遍历计数</th>
<th>是否是集合第一个元素</th>
<th>是否是集合最后一个元素</th>
</tr>
<c:forEach items="${list}" var="name" varStatus="st" >
<tr class="${st.index%2==0?"one":"two"}" >
<td>${name}</td>
<td>${st.index}</td>
<td>${st.count}</td>
<td>${st.first}</td>
<td>${st.last}</td>
</tr>
</c:forEach>
</table>
<hr>
<!-- 数数的功能-->
<c:forEach begin="1" end="10" step="1" var="num" >
${num}
</c:forEach>The above is the detailed content of How to write jsp. For more information, please follow other related articles on the PHP Chinese website!