Is it possible to get the name of the page being called inside a jsp 2.0 custom tag?

I am writing a custom JSP tag using JSP 2 tags. Inside my tag I would like to know which page the tag is called in order to generate urls. Is this possible without passing it through an attribute?

0


source to share


4 answers


It turns out that the request object is actually available, but only in the EL part of the tag. So this will work:

<form action="${pageContext.request.requestURI}">

      

But not this:



<form action="<%=request.requestURI%>">

      

Or that:

<form action="<%=pageContext.request.requestURI%>">

      

+2


source


I think in your tag code you can inspect the request object and its url and determine the page from that.



+1


source


It is possible to access the request from a tag file through a member variable pageContext

.

public class YourTag extends TagSupport {
    public int doStartTag() throws JspException {
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        String pathInfo = req.getPathInfo();

      

+1


source


The request object is available in the tag. It doesn't matter if you are using a class or a tag file. In tag files, it is available in Java scripts as well as EL. However, it is available as a ServletRequest object, not an HttpServletRequest object (in EL, the class of the object does not matter, but it is executed in scripts).

Also, in your scripts, you need to access the full method, not just the property name. Thus, your code should be:

<form action="<%= pageContext.getRequest().getRequestURI() %>">

      

but even that won't work because getRequestURI () is the HttpServletRequest [1] method, not the ServletRequest. So either use EL or use longer scriptlets in your tag file and cast the request object.

[1] http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html#getRequestURI ()

0


source