Learn Java for Web Development

(Tina Meador) #1

84 CHAPTER 2: Building Web Applications Using Servlets and JSP


Listing 2-23. Using the Forward Action in main.jsp


1.
2.
3.
4.
5.<%out.print("Inside main.jsp"); %>

6.
7.
8.


   Line 6: Uses <jsp:forward> to forward to the target JSP page (sub.jsp).

Listing 2-24. sub.jsp


1.
2.
3.
4.
5.<%out.print("Inside sub.jsp"); %>

6.
7.


Figure 2-29 shows the output when main.jsp is accessed.


Figure 2-29. Usage of the forward action


To understand the difference between the include action and the forward action, compare Listing 2-21
with Listing 2-23 and compare Figure 2 -2 8 with Figure 2 -2 9. In Listing 2-23, we use the forward
action in main.jsp, instead of the include action. The forward action transfers the control to sub.jsp,
just like the include action. But when sub.jsp completes, unlike the include action, control does not
go back to main.jsp.


The , , and Actions


These three standard actions can eliminate a great deal of scripting code including declarations,
scriptlets, and expressions.

Free download pdf