Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Aruna V
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aruna V
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No reply Guys ?? I am still calling for SOS !
 
hob hartman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aruna,
Try double quotes around <%= userNAME%> in your call to the tag.
hob
 
Aruna V
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope !
Does not work . I tried both single and double quotes !
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Aruna V
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still haven't shown us the code for your tag. Specifically, the signature of your attribute setter may be suspect.
bear
 
Aruna V
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the attribute name from "name" to something else like for ex:UserName
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic