Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

From JSP 1.2 to JSP 2.0

Posted on 2005-04-25
7
Medium Priority
?
207 Views
Last Modified: 2010-04-01
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,
0
Comment
Question by:dkim18
  • 4
  • 2
7 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13860764
As far as I remember nothing
0
 

Author Comment

by:dkim18
ID: 13860813
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
 
LVL 28

Accepted Solution

by:
rrz earned 400 total points
ID: 13861574
>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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:dkim18
ID: 13861722
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
 

Author Comment

by:dkim18
ID: 13861872
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
 

Author Comment

by:dkim18
ID: 13861924
Never mind this error.
No tag "randomNumberJSP20" defined in tag library imported with prefix "test"
0
 
LVL 28

Expert Comment

by:rrz
ID: 13863593
>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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a matter of fact, Outlook OST files are of much importance in relation to Exchange mailbox. OST files are independent as they are simply copy of data of a user’s mailbox on Exchange Server. Though, if the server’s status is changed or it is dama…
Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question