views:

29

answers:

1

I'm attempting to create a custom jsp tag. Everything is working fine, except for the fact that I the request seems to be out-of-scope for my custom function.

Here is the relevant bit from the .tag file:

<%!
private  String process(String age, BigDecimal amount)
        {
//Attempting to access request here results in an compile time error trying to:
String url=request.getURL;
        }
%>

I'm very new to JSP so I'm sure I'm missing something obvious..but I can't seem to figure out what. Any help is appreciated.

+1  A: 

I suspect that's because the custom function itself is not defined within the main execution of the JSP's service call, but is defined as a separate method within the generated JSP class. As such, the request variable is not visible tot it implicitly.

To clarify, if you had a look at the java source that the JSP compiler generates (which is appserver specific), you'll see how it hangs together.

I think you'll have to declare the request object as a parameter to your function, and pass it in when you invoke it.

<%!
private String process(String age, BigDecimal amount, ServletRequest request) {
   String url=request.getURL;
   ....
}
%>
skaffman
That may be what I need to do. I basing my assumptions on this article:http://today.java.net/article/2003/11/13/easy-custom-tags-tag-files-part-1I think it may be that I'm not understanding the difference between:<% String url=request.getURL %> (which works)and <%!String url=request.getURL%>Which does not (diff is opening tag <% vs <%!, using <% I can't declare a function..
David Hamilton
@David: `<%!` declares a new function within the generated JSP class, whereas `<%` just inlines some arbitrary java fragment into the JSP class's main `service()` method. It does make sense when you consider what the JSP compiler is actually doing, even if it seems arbitrary on the surface.
skaffman
+1, but fix the `getURL` - there is no such 'method' ;) and perhaps `HttpServletRequest`
Bozho