Tuesday, 6 January 2026

Accessing Map attached to Servlet using JSP Expression Language and JSTL Tag Library

 

 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>&lt;c:forEach&gt; 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>&lt;c:choose&gt; 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/>

Monday, 30 March 2015

Accessing ArrayList , HashMap attached to Servlet using JSP Expression Language and JSTL Tag Library

/ **
Servlet class in which ArrayList and HashMap is created and attached to ServletContext Object

*/

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class ServletExpression extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

  {
    response.setContentType("text/html");

PrintWriter out = response.getWriter();
out.println("<html> <head> <title> First Servlet ... </title></head>");
out.println("<body> <h1> Welcome to JAVA  </h1>");

ArrayList<String> alist = new ArrayList<String>();

alist.add("ISTAR");  alist.add("ADIT");
alist.add("VPSCI");  alist.add("NVPAS");

HttpSession session = request.getSession();
session.setAttribute("instname" ,alist);


//Setting hashmap in Application

HashMap acnt = new HashMap();

acnt.put("abc",200000);
acnt.put("xyz",100000);
acnt.put("pqr",300000);
acnt.put("lmn",400000);

ServletContext application = request.getServletContext();

application.setAttribute("acctname",acnt);

out.println("</body></html>" );
 
  }  //end of method
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

  {
doPost(request, response);
  }  //end of doGet method


}




2) Accessing ArrayList and HashMap elements in JSP Page (displayApplication.jsp):


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:out value="JSTL Tag Example" />

<hr/>
1:
${instname[0]} <br/>
2:
${instname[1]} <br/>
3:
${instname[2]} <br/>
4: <c:out value="${instname[3]}" default="ISTAR" /> <br/>
5: <c:out value="${instname[4]}" default="ISTAR" /> <br/>
6: <c:out value="${instname[5]}" default="ISTAR" /> <br/>


<hr/>
<b>Using JSTL , accessing ArrayList values::: </b><br/><br/>

<c:forEach var="i" items="${instname}" >

${i} <br/>
</c:forEach>

 <hr/>
 <b>
 <c:out value="<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/> <br/>

 <b>
 <c:out value="<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="i" items="${acctname}" >

${i.key} has account balance US \$ ${i.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/>

Wednesday, 11 March 2015

Creating Attribute at Request ,Session and Application in Servlet class

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import education.Student;

public class SessionApplicationDemo extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

  {
    response.setContentType("text/html");

PrintWriter out = response.getWriter();
out.println("<html> <head> <title> First Servlet ... </title></head>");
out.println("<body> <h1> Welcome to JAVA  </h1>");
out.println("Hello World <br>" );

Student student1 = new Student();
student1.setSid(2020);
student1.setSname("Hitesh");

Student student2 = new Student();
student2.setSid(3020);
student2.setSname("Nayan");
//access ServletContext

ServletContext application = request.getServletContext();

application.setAttribute("s2",student1);
//accessing application attribute
Student st1 = (Student) application.getAttribute("s2");

out.println(st1.getSid() +"<br/>");
out.println(st1.getSname());



//creating session

HttpSession session = request.getSession();

session.setAttribute("s3",student2);

/*request.setAttribute("s2",student1);
//accessing request attribute
Student st1 = (Student) request.getAttribute("s2");

out.println(st1.getSid() +"<br/>");
out.println(st1.getSname());
*/
//access session attribute
Student st2 = (Student) session.getAttribute("s3");

out.println(st2.getSid() +"<br/>");
out.println(st2.getSname());

out.println("</body></html>" );
 
  }  //end of method
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

  {
doPost(request, response);
  }


}