Introduction
In this project, you will learn how to use the out object and a for loop in JSP (Java Server Pages) to output a triangle pattern on a web page.
👀 Preview

🎯 Tasks
In this project, you will learn:
- How to use the
outobject to print output to the web page - How to implement a
forloop to iterate and generate the triangle pattern - How to utilize the
<br>tag to start a new line after each row of the triangle
🏆 Achievements
After completing this project, you will be able to:
- Generate dynamic output on a web page using JSP
- Understand the basics of using loops in JSP to create repetitive patterns
- Recognize the importance of using appropriate HTML tags, such as
<br>, to format the output
Outputting the Triangle
In this step, you will learn how to use the out object and a for loop to output a triangle with 10 rows in the index.jsp file.
Open the
index.jspfile located in the/home/labex/project/TriangleProjectdirectory.Inside the
<body>section, add the following code:
<%
for(int i = 0;i < 10; i++){
for(int j = 0 ; j <= i ; j++){
out.print("*");
}
out.print("<br>");
}
%>
This code will use a nested for loop to output the triangle. The outer loop will iterate 10 times, representing the 10 rows of the triangle. The inner loop will iterate from 0 to the current row index i, printing an asterisk (*) for each iteration.
After each row is output, the <br> tag is used to start a new line.
- Save the
index.jspfile.
Verifying the Output
To verify the output, follow these steps:
- Open a terminal and navigate to the Tomcat directory:
cd /opt/apache-tomcat-8.5.54/bin
- Copy the
TriangleProjectproject to Tomcat'swebappsdirectory.
cp -r ~/project/TriangleProject/ /opt/apache-tomcat-8.5.54/webapps/
- Start the Tomcat server:
./startup.sh
- Open a web browser and navigate to the following URL:
http://localhost:8080/TriangleProject/
You should see the triangle pattern printed on the page, as shown in the example screenshot in the original challenge.
Congratulations! You have successfully completed the project by using the out object and a for loop to output a triangle in the index.jsp file.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



