JavaWeb实验 JSP基本语法

本文介绍了如何在JSP中配置Eclipse和Tomcat环境,实现一个记录访问次数的功能,并展示了如何使用JSP嵌入Java代码段绘制斐波那契数列,以原神主题为背景样式。
摘要由CSDN通过智能技术生成

实验目的

  1. 掌握JSP基本语法;
  2. 掌握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>




  • 26
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值