Question

I am unable to call a custom tag I have created in JSP.

Relevant portion of tag descriptor library:

<!-- username tag -->
<tag>
    <name>username</name>
    <tag-class>abc.xyz.UserNameTagHandler</tag-class>
    <body-content>empty</body-content>
</tag> 

and Java class is :

public class UserNameTagHandler extends TagSupport {            
    public int doTag() throws JspException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)pageContext.getRequest();
        String username = httpServletRequest.getRemoteUser();
        if (username==null) {
            username = "Guest";
        } else {
            username = username.replaceFirst("@.*$", "");
        }
        JspWriter jspWriter = pageContext.getOut();
        try {
            jspWriter.print(username);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }
}

and index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/miscHelperToolCusLib.tld" 
prefix="misc"%>
<html>
  <body>
    <div>
        <h1 class="header">Welcome <misc:username />.</h1>
    </div>
  </body>
<html>

I see on the page:Welcome .

Any idea what has gone wrong. Any help appreciated.

Was it helpful?

Solution

Finally I solved it out myself. If you extend from TagSupport then you have to implement doStartTag() function instead of doTag().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top