How do I forward to other page using jsp:forward?

The <jsp:forward/> tag forward user request to other page. For example, a user request page1.jsp and in this page the server found a <jsp:forward page="page2.jsp"/>. The server immediately stop the processing of page1.jsp and jump to the page2.jsp.

Let see an example of using <jsp:forward/> tag.

When you try to run the example above by accessing the URL http://localhost:8080/page1.jsp you are going to see the content of page2.jsp instead of page1.jsp. It’s happen because on the server side page1.jsp forward your request to the page2.jsp. But if you look at your browser URL address it will still pointing to page1.jsp.

How do I include other pages using jsp:include?

The <jsp:include/> tag is use to include another page fragment of a JSP page into another page. This is useful when you have a common page such as header, footer or a menu that applied to many of all of your pages.

Here are the page fragment of the header.jsp, footer.jsp and menu.jsp. All of them are placed in the common folder in the same location with the index.jsp file.




When you access your page from the servlet container such as Apache Tomcat you’ll have a complete display of a page that contains header, menu, content and footer.

How do use c:forEach JSTL tag?

The c:forEach tag in the core JSTL tag library is a useful tag when we want to iterate over a collection of data such as array. It is commonly use to render a tabular data in our web pages in form of HTML table.

In the example below we display a weather data that we stored as two dimensional array of string. After declaring and initializing the data with some value we put it into the request scope. Later on the c:forEach tag can use the data, iterates it row by row to form an HTML table. Our weather data consist of the date, condition and the high and low temperature.

Our JSP page above creates the following output:

5-Days Weather for Denpasar, Indonesia

Nov 6 Sunny 32℃ 26℃
Nov 7 Sunny 32℃ 26℃
Nov 8 Sunny 32℃ 26℃
Nov 9 Partly Cloudy 32℃ 26℃
Nov 10 Isolated T-Storms 32℃ 26℃