实验目的
- 掌握JSP基本语法;
- 掌握JSP常见用法。
实验内容
【1】按照实验1配置Eclipse+Tomcat的环境,然后实现下图的JSP页面,每次刷新时,能够对访问次数进行记录,并显示“你是本网页第x个访问者访问ip是”。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>统计访问次数</title>
</head>
<body style="background: url('./wallhaven-p97e9e.jpg')">
<%
// 获取当前IP地址的访问次数,如果是第一次访问则初始化为1
String ip = request.getRemoteAddr();
Integer count = (Integer) application.getAttribute(ip);
if (count == null) {
count = 1;
} else {
count++;
}
// 更新访问次数
application.setAttribute(ip, count);
%>
<h1 align="center">原神启动</h1>
<h2 align="center">您是本网页第<%= count %>位米孝子,访问IP是 <%= ip %></h2>
</body>
</html>
【2】利用JSP嵌入Java代码段的方法,绘制斐波那契数列,底色和字体样式要一致。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>斐波那契数列</title>
<style>
table {
border-collapse: collapse;
margin-top: 20px;
}
td {
padding: 10px;
font-size: 16px;
text-align: center;
}
.blue {
background-color: #00E3E3;
}
.pink {
background-color: pink;
color: white;
}
</style>
</head>
<body>
<h1 >斐波那契数列</h1>
<h2 align="center">原神自走棋</h2>
<br>
<table align="center">
<%
long n1=0;
long n2=1;
long n=1;
String color="";
for (int i=0;i<7;i++){
out.println("<tr>");
for(int j=0;j<7;j++){
if(n%2==0){
color="blue";
}else{
color="pink";
}
if(n==1){
out.println("<td class='"+color+"'>("+n1+")</td>");
}else if(n==2){
out.println("<td class='"+color+"'>("+n2+")</td>");
}else{
long n3=n2+n1;
out.println("<td class='"+color+"'>"+n3+"</td>");
n1=n2;
n2=n3;
}
n++;
}
out.println("</tr>");
}
%>
</table>
</body>
</html>