JSP – How To Get HttpServletRequest in JSP Custom Tag

The below shows you how to get a HttpServletRequest object in a JSP custom tag via a PageContext object.

Here is a trick how to do this.

package com.bytenota.web.jsp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class MyTag extends TagSupport {
    private static final long serialVersionUID = 1L;
    public int doEndTag() throws JspException {
        // get HttpServletRequest from pageContect object
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        return 0;