Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Content in Tag Lib

 
Visha R
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,
We are just starting out with tag libs with a Hello World Example.
We have 2 pages:
main.jsp with a textbox and a Submit button.
The value from this textbox gets submitted to Hello.jsp
In Hello.jsp,we have a tag with attribute "empname" which prints "hello" + name.
But the problem here is that when we give a static value to the attribute,it prints the required output.
But when we attempt to pass the value received from the text box,only "Hello" gets printed without the name.
We have tried all possible stuff but to no avail.
We would be very grateful if anyone would help us solve this simple problem.
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you using your tag on the JSP page?
Simon
 
Visha R
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all let me thank you,Mr.Brown, for the valuable time which you have spent for responding to our question.
The Hello.jsp goes like this :
<%@ taglib uri="/SayHello.jsp" prefix="anyprefix"%>
<%
String name = request.getParameter("txtName");

out.println("Name is " + name);
%>
<html>
<body>
<anyprefix:Hello empname="<%=name%>">
</body>
</html>
The out.println works fine.But the value does not get passed to the Servlet.
We have also given a System.out.println(empname) in our servlet. That displays <%=name%>
By the way, we are using IBM Websphere 4.0 as our aplication server.
This problem may sound silly but till this is solved,we cannot start with our actual project.
Thanks in Advance,
Visha
 
Pawel Bobrow
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sh., but test:
<%! String name = request.getParameter("txtName");
%>
Bye
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the TLD, have you set the rtexprvalue of that attribute to true?
 
Visha R
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have given below the code of the Hello.java file,SecondHello.jsp and second.tld file.
Hello.java
-------------------------------------------------
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class Hello extends TagSupport
{
String empname;
public void setEmpname(String n)
{
this.empname = n;
System.out.println("Name is " + this.empname);
}
public String getEmpname()
{
return empname;
}
public int doStartTag()
{
try
{
JspWriter out = pageContext.getOut();
out.println("<table border=1>");
if(empname!=null)
{
out.println("<tr><td>");
}
else
{
out.print("<tr><td> Hello World </td></tr>");
}
}
catch(Exception ex)
{
throw new Error("All is not well in the world,");
}
return SKIP_BODY;
}
public int doEndTag()
{
try
{
JspWriter out = pageContext.getOut();
out.println("Hello" + empname);
out.println("</td></tr>");
out.println("</table>");
}
catch(Exception e){
throw new Error("All is not well in the world.");
}
return EVAL_PAGE;
}
}
SecondHello.jsp
--------------------------------------------------
<%@ taglib uri="/second.tld" prefix="anyprefix" %>
<% String name2 = request.getParameter("text1");%>
<html>
<head>
<title>Your Standard Hello World</title>
</head>
<body>
<anyprefix:Hello empname="<%=name2%>">
</body>
</html>
second.tld
-------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>anyprefix</shortname>
<tag>
<name>Hello</name>
<tagclass>Hello</tagclass>
<bodycontent>JSP</bodycontent>
<info>simple Hello World Example</info>
<attribute>
<name>empname</name>
<required>true</required>
<rtexpvalue>true</rtexpvalue>
</attribute>
</tag>
</taglib>
I am sorry but the following code does not seem to work :
<%! String name2 = request.getParameter("txtName") %>

Thanks in Advance,
Visha R
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of
"<%! String name2 = request.getParameter("txtName") %>
"
use
<% String name2 = request.getParameter("txtName") %>
as the request object is not available in the <%! %> tag.
 
Visha R
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Devesh but as you can see we have used
<%
String name2 = request.getParameter("txtName")
%>
instead of
<%!
String name2 = request.getParameter("txtName")
%>
It still does not work.
You are right. When we use the second one, we get the error :
Undefined variable or class name: request
Thanks,
Visha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic