views:

226

answers:

1

I'm trying to make a set of custom tags that encapsulate form elements (markup and validation).

There's a method given to retrieve the "Out" object easily:

JspWriter out = getJspContext().getOut();

However I can't figure out how to get the request object. I want to be able to directly access the submitted form values from within the Tag class so that I can validate each field.

The documentation is quite sparse, so I thought maybe I could use the JspContext object to somehow get the request attributes. But I don't understand the different scopes.

System.out.println(getJspContext().findAttribute("field1"));

always prints "null".

Enumeration e = getJspContext().getAttributeNamesInScope(1);

Looping through and printing out the enumeration just gives me a list of classes that don't exist:

javax.servlet.jsp.jspOut
javax.servlet.jsp.jspPage
javax.servlet.jsp.jspSession
javax.servlet.jsp.jspApplication
javax.servlet.jsp.jspPageContext
javax.servlet.jsp.jspConfig
javax.servlet.jsp.jspResponse
javax.servlet.jsp.jspRequest

So is this even possible?

If not, could anyone point me to a tag library that deals with form display and validation? I searched the internet for a couple hours and it seemed every single one was discontinued and I couldn't download them. Either that or suggest a better alternative for handling forms.

Edit: The tags extend the SimpleTagSupport class.

+2  A: 

If your class is extending TagSupport, you can access the protected pageContext variable. From that you're able to retrieve the request object.

http://java.sun.com/webservices/docs/1.5/api/javax/servlet/jsp/tagext/TagSupport.html#pageContext

unsquared
Is there any way to do it from a class extending SimpleTagSupport?
Lotus Notes
If you need access to more than the tag's attributes, you should consider extending TagSupport. Is there a reason you're avoiding this?"JspContext serves as the base class for the PageContext class and abstracts all information that is not specific to servlets. This allows for Simple Tag Extensions to be used outside of the context of a request/response Servlet."From Sun: http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/JspContext.html
unsquared
Alternatively, you can cast your JspContext to a PageContext. Described here, http://stackoverflow.com/questions/2098796/how-to-access-request-in-jsptags.
unsquared
Thank you, the cast to PageContext worked perfectly.
Lotus Notes