静态内容,指令(导包,导库,导入文件),表达式(在页面上打印信息<%=Java表达式 %>),小脚本(将HTML和Java融合在一起),声明(声明一个方法),注释
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>计算两个数的和</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 小脚本中可以声明变量,可以编写流程控制语句 -->
<%
int a=10,b=20;
int result=a+b;
String num=null;
if(result%2==0){
num="偶数";
}else{
num="奇数";
}
%>
<!--表达式:将java表达式的值显示在页面上 -->
两个数的和是<%=result %>,这个数是<%=num %>
</body>
</html>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用声明来显示时间</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!--声明的作用:可以定义变量,可以定义方法 -->
<%--声明的作用:可以定义变量,可以定义方法 --%>
<%
//可以定义在小脚本中
//单行注释
/*
多行注释
*/
%>
<%!//也可以定义在声明中
//单行注释
/*
多行注释
*/%>
<%!String a;
String getTime(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
a = sdf.format(date);
return a;
}%>
当前时间是:
<%=getTime(new Date(), "yyyy年MM月dd日 HH:mm:ss")%><br>
<%=a%>
</body>
</html>