Prev Next

Web / JSP interview questions

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. How do I test if a string contains the given value in EL?

Use the fn:contains() or fn:containsIgnoreCase() function.

<c:forEach items="${cartItems}" var="eachItem">
    <c:if test="${not fn:containsIgnoreCase(eachItem, 'egg')}">
        <p> ${eachItem} doesn't contain 'Egg'</p>
    </c:if>
</c:forEach>

2. if-else option in JSTL.

<c:if> tag does not have else tag. So we can use <c:choose> as illustrated below for if-else if- else scenario.

<c:choose>
  <c:when test="${condition1}">
    ...
  </c:when>
  <c:when test="${condition2}">
    ...
  </c:when>
  <c:otherwise>
    ...
  </c:otherwise>
</c:choose>

3. How do I find or compare collection size using JSTL?

JSTL functions library (fn) could be used to find the size or compare size of the collection. for e.g.

<c:if test="${fn:length(listItems) gt 0}">
   <p>Items found.</p>
</c:if>

4. How do I access session scoped attributes in JSP?

Using the below scriptlet in JSP, the session attributes can be accessed.

<%=request.getSession().getAttribute("sessionVariable")%>

Also the session attributes could be accessed using JSTL and Expression Language tag as shown below.

<c:out value="${sessionScope.sessionVarible}"/>

or using,

<c:out value="${sessionScope['sessionVariable']}"/>
5. What is Java Server Page (JSP)?

A Java Server Page (JSP) is a web page that contains two types of text: static data and JSP elements. Static data can be expressed in any text-based format, such as HTML or XML. JSP is a technology that mixes static content with dynamically-generated content.

6. Are JSP expressions evaluated inside a HTML comment on JSP page?

Yes, the JSP expressions will be resolved.

«
»
DataStructures

Comments & Discussions