This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes JSP and the fly likes JSP Tags - Attributes passed returns NULL [SOS Please help] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Tags - Attributes passed returns NULL [SOS Please help]" Watch "JSP Tags - Attributes passed returns NULL [SOS Please help]" New topic
Author

JSP Tags - Attributes passed returns NULL [SOS Please help]

Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
Hi all,
I ve a problem with jsp tags.
I ve defined my attributes in my JSP file..which is :
<%@ taglib uri="/sample1" prefix="sample" %>
<sample:sampletest name='<%= NAME %>' iter = '3'>
and my TLD for the attributes are like :
<attribute>
<name>name</name>
<required>true</required> <rtexpvalue>true</rtexpvalue>
</attribute>
irrespective of whether i put true/false for the required and rtexpvalue tags in my TLD file, the attribute name is not set. Can you tell me what might be the problem.
The problem is an example given in
http://www.onjava.com/pub/a/onjava/2000/12/15/jsp_custom_tags.html?page=2
This example is not working if we exactly follow all the steps given to develop a tag library with attribute and body .
Please help !!
v_aruna@csshome.net
Sam Dalton
Author
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
I think we would need to see your tag source code to be able to solve this one....
Post it and maybe we can help
S


<a href="http://www.samjdalton.com" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of [http://www.amazon.com/exec/obidos/tg/detail/-/1590592255/qid=1068633302//ref=sr_8_xs_ap_i0_xgl14/104-4904002-9274339?v=glance&s=books&n=507846]Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/1861007701/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/186100561X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Did you observe the following stmt
If our JSP page was called like hello.jsp?NAME=Sue our output would look like
in the example?
I would imagine the complete URL to be:
http://www.localhost:8080/jsp-directory/hello.jsp?NAME=Sue
something like that for a default web-app.
Pl. note that the attribute name is "name" and the parameter name is "NAME", Kinda odd but anyways.... . Case-sensitivity is important.
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
My Tag details looks like this
sample1.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>sample1</shortname>
<info>test sample1</info>
<uri>/sample1</uri>
<tag>
<name>sampletest</name>
<tagclass>test.Sample1</tagclass>
<bodycontent>JSP</bodycontent>
<uri>/sample1</uri>
<info>
This is a simple hello tag.
</info>


<attribute>
<name>name</name>
<required>true</required>
<rtexpvalue>true</rtexpvalue>
</attribute>

<attribute>
<name>iter</name>
<required>true</required>
<rtexpvalue>true</rtexpvalue>
</attribute>

</tag>

</taglib>
and my JSP File looks like this..
<%@ taglib uri="/sample1" prefix="sample" %>
<%!
String userNAME = null;
%>
<html>

<%
userNAME=request.getParameter("name1");
%>
<body bgcolor="#ffffff">
<hr />
<sample:sampletest name='<%= userNAME %>' iter='3'>
<tr>
<td>Have a nice day </td></tr>
</sample:sampletest>

<hr />
</body>
</html>
I hope this helps. If i give the same parameter 'userNAME' in my body tags ..it seems to work. But not as an attribute ! Please explain .
-A-
[ March 11, 2002: Message edited by: Aruna V ]
Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
No reply Guys ?? I am still calling for SOS !
hob hartman
Greenhorn

Joined: Mar 04, 2002
Posts: 11
Aruna,
Try double quotes around <%= userNAME%> in your call to the tag.
hob
Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
Nope !
Does not work . I tried both single and double quotes !
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
What is the exact error message you got? Did the tag handler throw an exception complaining about NULL?
Have you tried simpler tags to see if your JSP container is working properly?


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
There was no error message. The attribute sent a NULL to my servlet. And my Servlet printed the null value on to the page through out.println .
The same tag given in the body field seemed be printed.
<html>
<%
userNAME=request.getParameter("name1");
%>
<body bgcolor="#ffffff">
<hr />
<sample:sampletest name="<%= userNAME %>" iter='3'>
<tr>
<td>"<%= userNAME %>"</td> </tr>
</sample:sampletest>
<hr />
</body>
</html>

Also both single and double quotes seems to work . There isint any difference.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61755
    
  67

You still haven't shown us the code for your tag. Specifically, the signature of your attribute setter may be suspect.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
Ok i will paste my entire code once again .
1. index.html
<html>
<head>
<title> Using Body Contenet </title>
</head>
<body>
<form name="form1" action = "jsp/sample1.jsp" method = get>
NAME: <input type="text" name="name1" value="Aruna">
<input type="Submit" name="Submit" value="Submit">

</form>
</body>
</html>
2. sample1.jsp
<%@ taglib uri="/sample1" prefix="sample" %>
<%!
String userName = null;
%>
<html>

<head>
<title>Your Standard Hello World Demo</title>
</head>
<%
userName=request.getParameter("name1");
//out.println("userName : " +java.util.date());
%>
<body bgcolor="#ffffff">
<hr />
NAME : <%= userName %>
<sample:sampletest name="<%=userName%>" iter = '3'>
<tr>
<td>Have a nice day, "<%=userName%>" </td>
</tr>

</sample:sampletest>

<hr />
</body>
</html>

3. my Tag Library Descriptor (sample1.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>sample1</shortname>
<info>test sample1</info>
<uri>/sample1</uri>
<tag>
<name>sampletest</name>
<tagclass>test.Sample1</tagclass>
<bodycontent>JSP</bodycontent>
<uri>/sample1</uri>
<info>
This is a simple hello tag.
</info>


<attribute>
<name>name</name>
<required>true</required>
<rtexpvalue>true</rtexpvalue>
</attribute>

<attribute>
<name>iter</name>
<required>true</required>
<rtexpvalue>true</rtexpvalue>
</attribute>

</tag>

</taglib>

4. My servlet code : Sample1.java
package test;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class Sample1 extends BodyTagSupport
{
private String name;
private int iteration;
public void setName(String value){
System.out.println("value: " + value);
name = value;
}
public String getName(){
return(name);
}

public void setIter(String s1)
{
try
{
this.iteration = Integer.parseInt(s1);
}
catch(NumberFormatException nfe)
{
iteration = 1;
}
}
public String getIter()
{
return(Integer.toString(iteration));
}

public int doStartTag() throws JspTagException
{
try
{
JspWriter out = pageContext.getOut();
out.println("<table border = 1>");
out.println("<tr><td> *****Hello -" + name + "- </td></tr>");
if (!name.toString().equals(null))
out.println("<tr><td> Hello -" + name + "- </td></tr>");
else
out.println("<tr><td> Hello World </td></tr>");
}
catch(Exception e)
{
throw new JspTagException("Exception from doStart" + e);
}
return EVAL_BODY_TAG;
}
public int doEndTag() throws JspTagException
{
try
{
JspWriter out = pageContext.getOut();
out.println("</table>");
}
catch(Exception e)
{
throw new JspTagException("Exception from doStart" + e);
}
return EVAL_PAGE ;
}
public int doAfterBody() throws JspTagException
{
if (iteration-- >= 1)
{
BodyContent body = getBodyContent();
try
{
JspWriter out = body.getEnclosingWriter();
out.println(body.getString());
body.clearBody();
}
catch(Exception ioe)
{
throw new JspTagException("Error in Hello tag doAfterBody " + ioe);
}
return(EVAL_BODY_TAG);
}
else
{
return(SKIP_BODY);
}
}
}
5. My Web.XML File
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<description>
Example web application illustrating the use of tags in the
"utility" custom tag library, from the JAKARTA-TAGLIBS project.
</description>
<taglib>
<taglib-uri>
/sample1
</taglib-uri>
<taglib-location>
/WEB-INF/sample1.tld
</taglib-location>
</taglib>
</web-app>
Hope this helps
[ March 17, 2002: Message edited by: Aruna V ]
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
OK, try this:
Change your
JspWriter out = pageContext.getOut();
line to something like this:
BodyContent bc = getBodyContent();
JspWriter out = bc.getEnclosingWriter();

Then you can out.print() as usual. I have found pageContext.getOut() does not always work for BodyTag. At least for Tomcat 4.0 ...
Let me know if this solves your problem.
Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
Hi Michael,
The BodyContent is used only within doInitBody() or doAfterBody(). I needed to print my attributes that i passed. Not my Body content. I am able to print any attributes passed as Body Content.
The Part :
<sample:sampletest name="<%=userName%>" iter = '3'> is not working.
The Body of the JSP :
<tr>
<td>Have a nice day, "<%=userName%>" </td>
</tr> seems to work fine !!!
Revert back please
prashanth nagaraj
Ranch Hand

Joined: Mar 11, 2002
Posts: 43
Change the attribute name from "name" to something else like for ex:UserName


regards,<br />prashanth nagaraj
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
In your tag handler, you have:
public void setName(String value){
System.out.println("value: " + value);
name = value;
}
Does it print the correct value to STDOUT (or STDOUT.txt) when you invoke the tag?
Also, can you post the HTML code your "sample1.jsp" generated when you submit the "index.html" form?
That would help us understand what you meant by "NULL" ...
Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
The Html generated is as follows :
<html>

<head>
<title>Your Standard Hello World Demo</title>
</head>

<body bgcolor="#ffffff">
<hr />
NAME : Aruna
<table border = 1>
<tr><td> *****Hello -<%=userName%>- </td></tr>
<tr><td> Hello -<%=userName%>- </td></tr>
<tr>
<td>Have a nice day, "Aruna" </td>
</tr>


<tr>
<td>Have a nice day, "Aruna" </td>
</tr>


<tr>
<td>Have a nice day, "Aruna" </td>
</tr>


</table>

<hr />
</body>
</html>
If you see here.. the attribute is not replaced by its value. It is passed as a string. But the iteration alone worked and the body is repeated thrice. How is that ?
Also i tried changing the name of the attribute. The effect is all the same. The attribute passed is NULL.
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
It did not pass "NULL" to your tag attributes, it merely refused to substitute the script variable with it value. So, I had a closer look at the relevant parts of your tld file ...
In your tld file, you used
<rtexpvalue>true</rtexpvalue>
But I think the correct tag is:
<rtexprvalue>true</rtexprvalue>
Note the missing "r" !!!
Let me know if this is the problem!
Aruna V
Greenhorn

Joined: Mar 11, 2002
Posts: 9
Ooops Bingo !
Mike , thanx pal . U are a real GEM !
its the spell after all.The attributes are fine now.
Thanx a real bunch !

v_aruna@csshome.net
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
This is educational for me too. You'd think the JSP container would first validate the tlds using the DTD ... That would have saved us all the trouble!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP Tags - Attributes passed returns NULL [SOS Please help]