gudii9
asked on
JSP custom tag issues
Hi,
I was trying JSP Custom Tag example. In my JSP CustomTag is not recognized some reason. Do I need to configure something in web.xml as well. Please check the code, screenshots of the project structure, error.
CustomTag.jsp. tagclass in mytag.tld I removed customtags(since CalculatorTag is directly under default package for me) still did not work. Please advise on how can I make it work.
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
CustomTag2.JPG
I was trying JSP Custom Tag example. In my JSP CustomTag is not recognized some reason. Do I need to configure something in web.xml as well. Please check the code, screenshots of the project structure, error.
CustomTag.jsp. tagclass in mytag.tld I removed customtags(since CalculatorTag is directly under default package for me) still did not work. Please advise on how can I make it work.
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
<%@ taglib uri="/WEB-INF/mytags.tld" prefix="calc"%>CustomTag.JPG
<html>
<head>
<title> Scripting Variable in JSP</title>
<style>
h1 { font-family:Comic Sans MS; font-size:15pt; }
</style>
</head>
<body>
<calc:sum count="100"/>
</body>
</html>
mytas.tld
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library
1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>examples</shortname>
<info>Simple Library</info>
<tag>
<name>sum</name>
<tagclass>customtags.CalculatorTag</ tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
Calculator Bean class
package beans;
public class Calculator {
String count;
String sum;
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getSum() {
int s = 0;
int maxCount = Integer.parseInt(count);
for (int i = 1; i <= maxCount; i++) {
s += i;
}
sum = s + "";
return sum;
}
public void setSum(String sum) {
this.sum = sum;
}
}
WEB.xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-c lass>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-patt ern>
</servlet-mapping>
<servlet>
<servlet-name>Hello1</servlet-name>
<servlet-class>LoginServlet</servlet -class>
</servlet>
<servlet-mapping>
<servlet-name>Hello1</servlet-name>
<url-pattern>/LoginServlet</url-patt ern>
</servlet-mapping>
<servlet>
<servlet-name>Order</servlet-name>
<servlet-class>OrderProcessingServle t</servlet -class>
</servlet>
<servlet-mapping>
<servlet-name>Order</servlet-name>
<url-pattern>/OrderProcessingServlet </url-patt ern>
</servlet-mapping>
<servlet>
<servlet-name>billing</servlet-name>
<servlet-class>BillingServlet</servl et-class>
</servlet>
<servlet-mapping>
<servlet-name>billing</servlet-name>
<url-pattern>/BillingServlet</url-pa ttern>
</servlet-mapping>
<servlet>
<servlet-name>Confirmation</servlet- name>
<servlet-class>ConfirmationServlet</ servlet-cl ass>
</servlet>
<servlet-mapping>
<servlet-name>Confirmation</servlet- name>
<url-pattern>/ConfirmationServlet</u rl-pattern >
</servlet-mapping>
<servlet>
<servlet-name>Banner</servlet-name>
<servlet-class>BannerServlet</servle t-class>
</servlet>
<servlet-mapping>
<servlet-name>Banner</servlet-name>
<url-pattern>/BannerServlet</url-pat tern>
</servlet-mapping>
</web-app>
CustomTag2.JPG
ASKER
I tried one other similar example
http://www.roseindia.net/jsp/custom-tag.shtml
This works fine not sure what is the difference
http://www.roseindia.net/jsp/custom-tag.shtml
This works fine not sure what is the difference
EmptyCustomTag.JPG
JSP File
<%@ taglib uri="/WEB-INF/taglib.tld" prefix="custom"%>
<html>
<head>
<title>Custom Tag with no attribute</title>
</head>
<body bgcolor="ffffcc">
<H1>Welcome !</H1>
Custom tag work starts...
<custom:empty />
Custom tag work ends... Body Content of JSP page
</body>
</html>
taglib.tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>custom</shortname>
<tag>
<name>empty</name>
<tagclass>mytag.emptyTag</tagclass>
<bodycontent>empty</bodycontent>
<info>Tag having no attribute and no body</info>
</tag>
</taglib>
emptyTag.java
package mytag;
import java.io.*;
import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class emptyTag implements Tag {
private PageContext pageContext;
private Tag parent;
public emptyTag() {
super();
}
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("This is an empty tag!");
} catch (IOException ioe) {
throw new JspException("Error:"+ioe.getMessage ());
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
return SKIP_PAGE;
}
public void release() {
}
public void setPageContext(PageContext
pageContext) {
this.pageContext = pageContext;
}
public void setParent(Tag parent) {
this.parent = parent;
}
public Tag getParent() {
return parent;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
http://shivasoft.in/blog/java/how-to-create-jsp-custom-tag-%E2%80%93-using-tag-interface-or-tagsupport/
I am attaching same code and screen with project structure.
Open in new window
This example aslo gave similar error saying cannot find tag library descriptor as in attachment.
tldError.JPG