靜態內容,指令(導包,導庫,匯入檔案),表示式(在頁面上列印資訊<%=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>