Exercise
1.1. Create a new Java Enterprise project named ex1.1
, and modify index.jsp
to enable it to
- display current month.
- display the day of the week.
1.2. Create an HTML page me.html
as your personal page to introduce your name, age, hobbies, and avatar.
1.3. Copy me.html
to the webapp
folder of ex1.1
, and then access this page after starting the server.
1.4. Try to add your personal introduction into index.jsp
. You can use as many kinds of elements as you can.
1.5. Like plain Java class, we could import packages in JSP to simplify code. For example,
<h2><%= java.time.LocalDate.now() %></h2>
can be shortened into
<h2><%= LocalDate.now() %></h2>
if java.time.LocalDate
is imported. Try to add import statements in index.jsp
.
1.6. Please summarize the differences between the files that you see in IntelliJ IDEA (developing
stage) and those that you see in Tomcat (deploying
stage).
1.7. Let's try to add a classical for-loop
into JSP, and display a list of String
s on the web page.
List<String> list = new ArrayList<>();
list.add("java");
list.add("ee");
list.add("is");
list.add("fun");
for (String s : list) {
System.out.println(s);
}
In JSP, all plain-old Java codes should be put between <%
and >%
. For example,
<%
List<String> list = new ArrayList<>();
list.add("java");
list.add("ee");
list.add("is");
list.add("fun");
for (String s : list) {
System.out.println(s);
}
%>
Unluckily, you will never see output on the web page because System.out.println
will display the text on the standard output. One solution is to use JSP expression tag:
<%= s %>
Based on the above, try to output the content of list
object in index.jsp
.
1.8. You may notice a small image shown displayed next to the page title in the browser tab when you visit a website. Formally, it is a favicon. Try to add your own favicon for your personal home page.
1.9. Try to rename your first Java web project's URL to first-<your name initials>
in IntelliJ IDEA such that you can access the welcome page via a shorter URL. For example, suppose you are Li Xiaohua, the renamed one should be http://localhost:8080/first-lxh.
1.10. Based on Ex 1.7, try to output the ArrayList
using a servlet.