/JSP_Base/WebContent/base.jsp:
<!-- JSP指令 -->
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!-- 声明 -->
<%!
int i =1; // 声明定义变量是全局的
// 只能声明方法,定义变量,不能书写具体的java代码实现
public void print() {
}
/*
声明格式日期的方法
*/
public String convertDateToString(String pattern,Date crrentDate) {
// 创建时间格式对象
SimpleDateFormat fmt = new SimpleDateFormat(pattern);
return fmt.format(crrentDate);
}
%>
<!-- 小脚本 -->
<%
int i =2;
%>
<!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>Insert title here</title>
</head>
<body>
<%
SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日");
%>
<!-- 表达式用于输出对象内容 -->
<%=fmt.format(new Date()) %>
<!-- 使用表达式调用方法 -->
<%=convertDateToString("yyyy/MM/dd", new Date()) %>
<%=convertDateToString("yyyy-MM-dd HH:mm:ss", new Date()) %>
</body>
</html>
/JSP_Base/WebContent/homework.jsp:
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@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>Insert title here</title>
</head>
<body>
<%-- 课堂练习1 --%>
<%
// 创建List集合对象
List<String> lst = new ArrayList<String>();
for(int i=1;i<11;i++) {
lst.add("第"+i+"次添加字符串");
}
out.print("添加完毕<br/>");
// 依次打印集合中内容
for(String value : lst) {
out.print(value+"<br/>");
}
%>
<br/>
<%-- 课堂练习2(打印成绩信息) --%>
<%
// 定义姓名数组
String[] names = {"李文","张三","王五"};
// 定义分数数组
int[] scores = {60,70,85};
// 输出页面内容out.print();
for(int i = 0; i<names.length ; i++) {
out.print(names[i]+": "+scores[i]+"分<br/>");
}
%>
</body>
</html>
***号码截取生日:
<%@ 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>Insert title here</title>
</head>
<body>
<%
String idcard = "500225198812121814";
String id = idcard.substring(6, 14);
// 获取年
String year = id.substring(0,4);
// 获取月
String month = id.substring(4,6);
// 获取日
String day = id.substring(6,8);
String date = year+"-"+month+"-"+day;
out.print("生日:"+date);
%>
</body>
</html>
转载于:https://blog.51cto.com/lhmjava/1623367