This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Question about passing parameters from a custom tag to a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Question about passing parameters from a custom tag to a JSP" Watch "Question about passing parameters from a custom tag to a JSP" New topic
Author

Question about passing parameters from a custom tag to a JSP

Samarth Kumar
Greenhorn

Joined: Mar 30, 2004
Posts: 12
Folks,

I have a custom tag that displays values based on a certain conditions in an object. One of the condition is that if a date is set to a date in the past then the value should be a link to a page that says this page has expired. At the sametime, this page also has a link that allows users to remove that link. The links are stored in a database.

I have already written the condition for date checking, however I am not sure as to how I can implement URL rewriting or if I can even use URL rewriting to pass the parameters from the custom tag to the new page. All I have is just a text field.

I am also attaching the code for the custom tag and also how it is used in the JSP. And yes, I forgot to mention that the custom tag was written so that there is no java code in the JSP.

Any thoughts/ways on how this can be done would be greatly appreciated.

Thanks,
Samarth





import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class UrlDisplayTag extends TagSupport
{
private String contentId = null;
private String sortSequenceNumber = null;
private String title = null;
private String url = null;
private Date storedTime = null;

private String mObjectName = null;
private String mElementName = null;

public int doStartTag() throws JspException
{
int status;
MyURL urlObject = (MyURL) pageContext.getAttribute(getObjectName());
JspWriter out = pageContext.getOut();
contentId = urlObject.getId();
sortSequenceNumber = urlObject.getSortSequence();
title = urlObject.getTitle();
url = urlObject.getHref();
storedTime = urlObject.getEndTimeStamp();
try
{
if(mElementName.equalsIgnoreCase("contentId"))
{
out.print(contentId);
}
else if(mElementName.equalsIgnoreCase("sequence"))
{
out.print(sortSequenceNumber);
}
else if(mElementName.equalsIgnoreCase("title"))
{
out.print(title);
}
else if(mElementName.equalsIgnoreCase("href"))
{
//here you check the timestamp values
//if the current time is greater than the object's time in db2
//then the object has been made inactive
//return the url to the inactive page and put the contentId,
//and sortSequenceNumber in the HttpSession
//if the object's time in db2 is greater than the current time
//then the object is active return the url
Calendar c = new GregorianCalendar();
Date currentTime = c.getTime();
int isLinkValid = currentTime.compareTo(storedTime);
if(isLinkValid >= 0)
{
out.print(url);
}
else
{
//print location of the expired page
//somehow get the contentId and the sortSequenceNumber as url parameters that can be used by the JSP
//to get the two values which can be then used by the servlet for deleting the link.
out.print("http://www.anything.com");
}
}
status = SKIP_BODY;
}
catch(Exception e)
{
TEHelper.logMessage(MessageType.error, "UrlDisplayTag.doStartTag(): Exception encountered", e, 8);
e.printStackTrace() ;
status = SKIP_BODY;
}
return status;
}

public int doEndTag() throws JspException
{
return SKIP_BODY;
}

/**
* the setter for the element name found in the jsp tag
* @param elementName
*/
public void setElementName(String elementName)
{
mElementName = elementName;
}

public String getElementName()
{
return mElementName;
}

/**
* the setter for the object name found in the PageContext
* @param objectName
*/
public void setObjectName(String objectName)
{
mObjectName = objectName;
}

public String getObjectName()
{
return mObjectName;
}

public Date getStoredTime()
{
return storedTime;
}

public void setStoredTime(Date d)
{
storedTime = d;
}
}





<%@ page session = "false" contentType = "text/html" %>
<%@ taglib uri = "/WEB-INF/url.tld" prefix = "url" %>
<%@ taglib uri = "http://java.sun.com/portlet" prefix = "portlet" %>
<portlet efineObjects />

<b>View Page</b>

<script>
myReferencesArray = new Array();

function ViewReferenceObject(id, title, url)
{
if (id != "1")
{
this.ID = id;
this.TITLE = title;
this.URL = url;
}
else
{
this.ID = id;
this.TITLE = "--------";
this.URL = url;
}
}

function loadReferences(id, title, url)
{
myReferencesArray[myReferencesArray.length++] = new ViewReferenceObject(id, title, url);
}

function printReferences()
{
for (var ii = 0; ii < myReferencesArray.length; ii++)
{
if(myReferencesArray[ii].ID != "1")
{
document.write("<br><a href = \"" + myReferencesArray[ii].URL + "\">" + myReferencesArray[ii].TITLE + "</a>");
}
else
{
document.write("<br>" + myReferencesArray[ii].TITLE);
}
}
}

</script>

<url:showLink listName = "urlList" objectName = "url">
<script>
loadReferences("<url:showUrl objectName = 'url' elementName = 'contentId' />", "<url:showUrl objectName = 'url' elementName = 'title' />", "<url:showUrl objectName = 'url' elementName = 'href' />");
</script>
</url:showLink>

<script>
printReferences();
</script>
 
Don't get me started about those stupid light bulbs.
 
subject: Question about passing parameters from a custom tag to a JSP