一、jsp声明
在jsp程序段中变量必须先定义后使用,其使用方法如下:
<%! 代码 %>
下面的declaration01.jsp是一个简单的例子,代码如下:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/2/25
Time: 14:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>jsp声明</title>
</head>
<body>
<%
String str="你好";
%>
<%
out.print(str);
%>
</body>
</html>
运行结果如下:
二、url传值
web页面之间专递数据是web程序的重要功能,其流程图如图所示:
url通俗来讲就是网址,例如:"http://localhost:8888/javaweb_jsp_war_exploded/declaration01.jsp"表示访问javaweb-jsp项目的declaration01.jsp
用户还可以在该页面后面给出一些参数,格式是在url后面添加:
?参数名1=参数值1&参数名2=参数值2....
方法如下:
<%
//获取参数A,赋值给str
String str=request.getParameter("A");
%>
例如:
url01的num传值到url02
url01.jsp代码如下:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/2/25
Time: 15:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>url02</title>
</head>
<body>
<%
String str02=request.getParameter("num");
int num=Integer.parseInt(str02);
%>
该数字三个相加<%=num+num+num%><br>
</body>
</html>
url02的代码如下:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/2/25
Time: 14:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>url01</title>
</head>
<body>
<%
//先定义变量
String str="123";
int num=Integer.parseInt(str);
%>
该数字自己相加<%=num+num%><br>
<a href="url02.jsp?num="<%=num%>">到达url02</a>
</body>
</html>
运行结果如下:url01可以把num传值给url02
点击超链接到达url02