Servlet Code:
java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ServletExpression extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html;charset=UTF-8");
// Prepare data
ArrayList<String> instituteList = new ArrayList<>();
instituteList.add("Institute-1");
instituteList.add("Institute-2");
instituteList.add("Institute-3");
instituteList.add("Institute-4");
Map<String, Integer> accountMap = new HashMap<>();
accountMap.put("abc", 200000);
accountMap.put("xyz", 100000);
accountMap.put("pqr", 300000);
accountMap.put("lmn", 400000);
// Store in session
HttpSession session = request.getSession();
session.setAttribute("instname", instituteList);
// Store in application scope
ServletContext application = request.getServletContext();
application.setAttribute("acctname", accountMap);
// Output HTML
try (PrintWriter out = response.getWriter()) {
out.println("""
<html>
<head>
<title>First Servlet</title>
</head>
<body>
<h1>Welcome to JAVA</h1>
</body>
</html>
""");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}
}
JSP (displayApplication.jsp)jsp<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <h2>JSTL Tag Example</h2> <hr/> <!-- Direct index access --> 1: ${instname[0]} <br/> 2: ${instname[1]} <br/> 3: ${instname[2]} <br/> 4: <c:out value="${instname[3]}" default="Institute-1"/> <br/>
5: <c:out value="${instname[4]}" default="Institute-2"/> <br/>
6: <c:out value="${instname[5]}" default="Institute-3"/> <br/>
<hr/> <b>Using JSTL to iterate ArrayList:</b><br/><br/> <c:forEach var="item" items="${instname}"> ${item} <br/> </c:forEach> <hr/> <b><c:forEach> examples:</b><br/> <c:forEach var="i" begin="1" end="10"> ${i} <c:if test="${i < 3}"> less than 3 </c:if> <c:if test="${i < 6}"> less than 6 </c:if> <c:if test="${i < 8}"> less than 8 </c:if> <br/> </c:forEach> <hr/> <b><c:choose> example:</b><br/> <c:forEach var="i" begin="1" end="10"> ${i} <c:choose> <c:when test="${i < 3}"> less than 3 </c:when> <c:when test="${i < 6}"> less than 6 </c:when> <c:when test="${i < 8}"> less than 8 </c:when> <c:when test="${i == 8}"> equal to 8 </c:when> <c:otherwise> greater than 8 </c:otherwise> </c:choose> <br/> </c:forEach> <hr/> <b>HashMap example:</b><br/> <c:forEach var="entry" items="${acctname}"> ${entry.key} has account balance US $ ${entry.value} <br/> </c:forEach> <hr/> <b>Reverse Order Print:</b><br/> <c:forEach var="i" begin="1" end="10" step="2"> <c:set var="revorder" value="${11 - i}" /> ${revorder} <br/> </c:forEach> <hr/>