From JSP 1.2 to JSP 2.0

hi!

This is a random number tag that inserts a random number between and 0 and 1 into the page. If I want to redo this tags with JSP 2.0 tag files, what has to be changed?
++++++++++++++++++

package moreservlets.tags;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;


public class RandomNumberTag extends TagSupport {
 
  public int doStartTag() {
    try {
      JspWriter out = pageContext.getOut();       
      double doubleNum = ((double)(Math.random()));
      out.print(doubleNum);
    } catch(IOException ioe) {
      System.out.println("Error generating random number: " + ioe);
    }
    return(SKIP_BODY);
  }
}
++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
<HEAD>
<TITLE>HW11 - #1 Random Number</TITLE>
</HEAD>
<BODY>
<H1>#1 Random Number</H1>
<%@ taglib uri="/WEB-INF/mytags-taglib.tld" prefix="mytags" %>
<UL>
  <LI><mytags:randomNumber />
 
</UL>
</BODY>
</HTML>


thanks,
dkim18Asked:
Who is Participating?
 
rrzCommented:
>If I want to redo this tags with JSP 2.0 tag files  
Here is example to demostrate using tag files.
-----------testTag.jsp--------------------------------------------------
<%@ taglib prefix="test" tagdir="/WEB-INF/tags/mytags" %>
<test:randomNumber/>
-----------------------------------------------------------------------

and in  the folder at yourContextPath/WEB-INF/tags/mytags  we could have

-----------------------randomNumber.tag----------------------
<%=Math.random()%>
------------------------------------------------------------------

>If I used attribute, don't I need to  refer to  attributes with ${attributeName}
Which attributes are you talking about ?        rrz
0
 
KuldeepchaturvediCommented:
As far as I remember nothing
0
 
dkim18Author Commented:
I am so clear about what to change...

Doesn't outline result should refer to tag body with <jsp:doBody>?
If I used attribute, don't I need to  refer to  attributes with ${attributeName}?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dkim18Author Commented:
Can I use tld file instead of tag file?

Here is what I want to change for attributes stuff.

package moreservlets.tags;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.*;

public class RandomNumberLimitTag extends TagSupport {
      protected int len = 10;
      
  public int doStartTag() {
    try {
      JspWriter out = pageContext.getOut();       
     
      Random generator = new Random();
      int intNum = generator.nextInt( len );
      out.print(intNum);
    } catch(IOException ioe) {
      System.out.println("Error generating random number: " + ioe);
    }
    return(SKIP_BODY);
  }
}
+++++++++
package moreservlets.tags;

public class RandomIntegerTag extends RandomNumberLimitTag {

  public void setLimit(String limit) {
    try {
      len = Integer.parseInt(limit);
    } catch(NumberFormatException nfe) {
      len = 10;
    }
  }
}
++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
<HEAD>
<TITLE>HW 11 - #2 Random Number</TITLE>
</HEAD>
<BODY>
<H1>#2 Random Number Limit</H1>
<%@ taglib uri="/WEB-INF/mytags-taglib.tld" prefix="mytags" %>
<UL>
  <LI>Limit - (782): <mytags:randomNumberLimit limit="782" />
  <LI>Default -(10): <mytags:randomNumberLimit />
</UL>
</BODY>
</HTML>
++++++++++
  <tag>
    <name>randomNumberLimit</name>
    <tagclass>moreservlets.tags.RandomIntegerTag</tagclass>
    <bodycontent>empty</bodycontent>
    <info>Random Number: inserts a random number(integer)</info>
    <attribute>
      <name>limit</name>
      <required>false</required>
    </attribute>
  </tag>
0
 
dkim18Author Commented:
I am having this error. Do I have to declare in tag library?
 
No tag "randomNumberJSP20" defined in tag library imported with prefix "test"
0
 
dkim18Author Commented:
Never mind this error.
No tag "randomNumberJSP20" defined in tag library imported with prefix "test"
0
 
rrzCommented:
>Can I use tld file instead of tag file?
Yes, you can do it either way. The tag file would have code lines something like  
<%@ attribute name="limit" required="false"%>
${limit}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.