Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding EL Expressions, Filters, and Listeners in Java Web Development

Tech May 17 4

EL Expresions in JSP

Basic Syntax and Usage

EL (Expression Language) provides a simplified way to access data in JSP pages with out scriptlets. The syntax is ${expression}.

<% request.setAttribute("greeting","Hello World"); %>
Traditional JSP: <%= request.getAttribute("greeting") %>
EL Expression: ${greeting}

Accessing Data Structures

EL can navigate through objects, lists, and maps:

public class User {
    private String username = "testUser";
    private Address address = new Address();
    // getters/setters
}
${user.username}
${user.address.city}
${userList[0].username}
${userMap["key"].username}

Servlet Filters

Filter Implementation

Filters intercept requests/responses for preprocessing and postprocessing:

public class EncodingFilter implements Filter {
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
        throws IOException, ServletException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        chain.doFilter(req, resp);
    }
}

Filter Configuration

Filters are configured in web.xml:

<filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.example.EncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Servlet Listeners

Context Listener Example

Listeners respond to application events:

public class AppListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        // Application startup logic
    }
    
    public void contextDestroyed(ServletContextEvent sce) {
        // Application shutdown logic
    }
}

Listener Configuration

<listener>
    <listener-class>com.example.AppListener</listener-class>
</listener>

JSTL Tags

Conditional Logic with JSTL

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

<c:if test="${user.role == 'admin'}">
    Admin controls
</c:if>

<c:choose>
    <c:when test="${score >= 90}">A</c:when>
    <c:when test="${score >= 80}">B</c:when>
    <c:otherwise>C</c:otherwise>
</c:choose>

Looping with JSTL

<c:forEach items="${userList}" var="user" varStatus="status">
    ${status.index}. ${user.username}
</c:forEach>
Tags: JavaJSP

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.