Mike McElligott wrote:

>
>
> 1.  Last night I was attempting to use a RequestDispatcher object to
> forward to another page.  That didn't work (I think it may be a
> configuration issue).  When I used the <jsp:forward page='blah'>
> syntax instead things worked great.  I wondered what that tag got
> translated into so I looked at the generated source and found that the
> servlet actually uses PageContext.forward(String url).  Anyone know
> why that is the case vs. ServletContext.getRequestDispatcher(String
> resource).foward(request, response)?
>

I use RequestDispatcher.forward() heavily to forward from servlets to
JSP pages, and it works fine, as long as you get the path right.

What didn't work when you tried it?  Did you get an exception, or an
HTTP error?  One thing to remember is that the path you give a
RequestDispatcher must start with a "/", and be a context-relative URI
to the servlet or JSP page you want to forward to.

>From the Javadocs, it looks like PageContext.forward() will accept a
relative URL that doesn't start with "/" and interpret it as relative to
the current request -- other than that, I'd be pretty confident that it
uses RequestDispatcher.forward() underneath.


>
> 2.  Can I define a function in jsp?  I don't see any reference to
> doing so in the spec.  I'm aware that this is the 'wrong' way to do
> stuff and that I should do my logic in a bean, but I want to know if
> it's possible.  It seems like something that, even if it isn't in the
> spec, will probably be implemented by vendors who want to compete with
> the versatility of...ahh.. other scripting environments.
>

You can indeed, if you're using Java as the scripting language, using
the <%! ... %> mechanism.  I used this technique during presentation of
business objects in a table -- because there were so many variations and
decisions to be made, it was much easier to write a Java function in the
page that created each table row, something like this:

<%!
     private String generateRow(OrderLine orderLine) {
        StringBuffer sb = new StringBuffer();
        sb.append("<tr>");
        ;    // append the contents of each TD element for this row
        sb.append("</tr>");
        return (sb.toString());
    }
%>

and then call it in my table generating loop:

<table ...>
<%
    OrderLine lines[] = ....;
    for (int i = 0; i < lines.length; i++) {
        out.println(generateRow(lines[i]);
    }
%>
</table>

Yes, it's including Java code in a JSP page, which I don't really like
... but it's presentation logic only.  When JSP 1.1 is supported, I'm
more likely to create some application-specific custom tags for stuff
like this.

Note:  Because the Java compiler doesn't care where a method declaration
is compared to where you call it, you can put the <%! ... %> block shown
above anywhere you want in the JSP page.  I tend to put it at the
bottom, out of the way of the HTML text above.

>
> Michael McElligott

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to