O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A conditional forward...

So imagine you’re a JSP and you assume you’re being called from a request that includes a userName parameter. Since you’re counting on that parameter, you want to first check that the userName parameter isn’t null. If it’s not, no problem—finish the response. But if the userName parameter is null, you want to stop right here and turn the whole request over to something else—like a different JSP that will ask for the userName.

For now, we know we can do it with scripting:

JSP with a conditional forward (Hello.jsp)

image with no caption

JSP to which the request is forwarded (HandleIt.jsp)

<html><body>

We're sorry... you need to log in again.

<form action="Hello.jsp" method="get">
Name: <input name="userName" type="text">
<input name="Submit" type="submit">
</form>

</body></html>

Note

This is just a plain old page that gets the request parameter input from the user and then requests the JSP we were just on... Hello.jsp.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required