当前位置: 首页 > news >正文

准备我们心爱的IDEA写Jsp

JSP学习

一、准备我们心爱的IDEA

在这里插入图片描述

new一个项目:New Project --> Next -->Next -->Finsh

在这里插入图片描述

二、配置好服务器Tomcat-9.0.30

在这里插入图片描述

1.> 在WEB-INF下创建一个Lib包

将jsp-api.jar复制进去,并使其生效

在这里插入图片描述

未生效前:

在这里插入图片描述

生效过程:

在这里插入图片描述

2.> 用锤子配置汤姆猫TomCat

点击+ 号 选择本地的汤姆猫
在这里插入图片描述

在Deployment中的 + 号 选择Artifat

在这里插入图片描述

将多余的名称删去,为了方便找到

在这里插入图片描述

三、编写JSP文件

1.> 在web包下创建以.jsp为后缀名的文件

在这里插入图片描述

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>$Title$</title></head><body>$END$</body>
</html>
2.>语法

JSP注释:为代码作注释以及将某段代码注释掉。

<%-- 该部分注释在网页中不会被显示--%>

<%-- 注释 --%> JSP注释,注释内容不会被发送至浏览器甚至不会被编译

脚本程序的语法格式:

<% 代码片段 %>

JSP表达式

<%= 表达式 %>

四、写个代码爽一下

运行–前提网址正确

1.>求字符串的长度

<%--Created by IntelliJ IDEA.User: 86156Date: 2023/9/4Time: 10:28To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>案例1--求字符串的长度</title>
</head>
<body><%--java代码在下面的标识里面写入<%%>中--%>
<%String str = "hello";out.println("字符串长度为:" + str.length() + ",字符串:" + str);%>
</body>
</html>
运行结果:

网络:http://localhost:8080/FirstWeb2_war_exploded/strlen.jsp

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

2.>计算数组平均值

<%--Created by IntelliJ IDEA.User: 86156Date: 2023/9/18Time: 9:15To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>计算平均值</title>
</head>
<body>
<%int arraySize = 5;int[] numbers = new int[arraySize];numbers[0] = 10;numbers[1] = 20;numbers[2] = 30;numbers[3] = 40;numbers[4] = 50;int sum = 0;for (int i = 0; i < arraySize; i++) {sum += numbers[i];}double average = (double) sum / arraySize;
%>平均值是:<%= average %>
</body>
</html>

http://localhost:8080/FirstWeb2_war_exploded/IntARRay.jsp

在这里插入图片描述

3.>九九乘法表

方式一:
<%--Created by IntelliJ IDEA.User: 86156Date: 2023/9/11Time: 11:16To change this template use File | Settings | File Templates.
--%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head><style>table {border-collapse: collapse;}th, td {border: 1px solid black;padding: 10px;text-align: center;}.odd {background-color: lightblue;}.even {background-color: lightgreen;}</style>
</head>
<body>
<table><tr><th></th><% for (int i = 1; i <= 9; i++) { %><th><%= i %></th><% } %></tr><% for (int i = 1; i <= 9; i++) { %><tr><th><%= i %></th><% for (int j = 1; j <= 9; j++) { %><% int result = i * j; %><td class="<%= (i + j) % 2 == 0 ? "even" : "odd" %>"><%= i %> * <%= j %> = <%= result %></td><% } %></tr><% } %>
</table>
</body>
</html>

http://localhost:8080/FirstWeb2_war_exploded/multitable1.jsp

在这里插入图片描述

方式二:
<%--Created by IntelliJ IDEA.User: 86156Date: 2023/9/11Time: 10:55To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head><style>table {border-collapse: collapse;}th, td {border: 1px solid black;padding: 10px;text-align: center;}.odd {background-color: lightblue;}.even {background-color: lightgreen;}</style>
</head>
<body>
<table><tr><th></th><% for (int i = 1; i <= 9; i++) { %><th><%= i %></th><% } %></tr><% for (int i = 1; i <= 9; i++) { %><tr><th><%= i %></th><% for (int j = 1; j <= 9; j++) { %><% int result = i * j; %><% if (j >= i) { %><td class="<%= (i + j) % 2 == 0 ? "even" : "odd" %>"><%= i %> * <%= j %> = <%= result %></td><% } else { %><td></td><% } %><% } %></tr><% } %>
</table>
</body>
</html>

http://localhost:8080/FirstWeb2_war_exploded/multitalbe2.jsp

在这里插入图片描述

4.>获取圆的面积:

STEP1:编写一个圆的类

在src下,new一个包,包下new一个class文件为circle

如图:

在这里插入图片描述

写好私有域的r,利用ptg快速生成JavaBean (ptg可以在插件中下载)

在这里插入图片描述

package cn.heima.circle2;public class Circle {private  double r;public Circle() {}public Circle(double r) {this.r = r;}/*** 获取* @return r*/public double getR() {return r;}/*** 设置* @param r*/public void setR(double r) {this.r = r;}public String toString() {return "Circle{r = " + r + "}";}public  double getAreacIR(){return  Math.PI*this.r*this.r;}
}
STEP2:在JSP文件下导入Circle文件:

导入方法

<%@ page import="cn.heima.circle2.Circle" %>
<%--Created by IntelliJ IDEA.User: 86156Date: 2023/9/11Time: 11:27To change this template use File | Settings | File Templates.
--%>
<%--Created by IntelliJ IDEA.User: 86156Date: 2023/9/11Time: 11:27To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="cn.heima.circle2.Circle" %>
<html>
<head><title>CircleDemo</title>
</head>
<body>
<%Circle c = new Circle(1.0);out.print(c.getAreacIR());
%>
</body>
</html>

结果

在这里插入图片描述

http://www.lryc.cn/news/171621.html

相关文章:

  • 将近 5 万字讲解 Python Django 框架详细知识点(更新中)
  • Arcgis提取每个像元的多波段反射率值
  • JavaScript面试题整理(一)
  • 数据结构:树和二叉树之-堆排列 (万字详解)
  • 爬虫入门基础:深入解析HTTP协议的工作过程
  • k8备份与恢复-Velero
  • 基于Python开发的火车票分析助手(源码+可执行程序+程序配置说明书+程序使用说明书)
  • 旺店通·企业奇门与金蝶云星空对接集成订单查询连通销售订单新增(旺店通销售-金蝶销售订单-小红书)
  • 卡尔曼滤波应用在数据处理方面的应用
  • PROFIBUS主站转ETHERCAT协议网关
  • Vue路由的使用及node.js下载安装和环境搭建
  • 【算法训练-二叉树 三】【最大深度与直径】求二叉树的最大深度、求二叉树的直径
  • 查看linux是centos还是Ubuntu
  • win10怎么关闭自动更新,这个方法你知道吗?
  • 「语音芯片」常见的OTP芯片故障分析
  • 孩子写作业买什么样台灯合适?适合孩子读写台灯推荐
  • DBAPI插件开发指南
  • 线程池使用之自定义线程池
  • Puppeteer无头浏览器:开启自动化之门,掌握浏览器世界的无限可能
  • Ubuntu 23.10/24.04 LTS 放弃默认使用 snap 版 CUPS 打印堆栈
  • Linux CentOS7 history命令
  • XC5350A 单节锂电池保护芯片 过放2.9V/2.8V/2.4V保护IC
  • 单片机论文参考:1、基于单片机的电子琴
  • Opencv源码解析(2)算法
  • 让Mac菜单栏变得更加美观整洁——Bartender 5
  • 服务器迁移:无缝过渡指南
  • 安卓开发中ViewBinding的使用
  • 【初阶数据结构】树(tree)的基本概念——C语言
  • 二叉树知识点
  • Day69:283. 移动零、11. 盛最多水的容器、42. 接雨水