开始学习
Dynamic Web Project
番外篇!毒蛇吟来篇章!
Dynamic Web Project:
- 是 Eclipse 中的一种项目类型,用于开发基于 Servlet、JSP 的简单 Java Web 应用。
- 提供了基础的 Web 结构,包括 WebContent 文件夹、
WEB-INF
、web.xml
等,适合开发基于 Java EE 规范的动态 Web 应用。
Dynamic Web Project:
- 架构简单,一般只涉及 Servlet、JSP、JavaBean。
- 适合小型或简单的 Web 应用,不需要复 杂的架构或额外的库。
使用 Dynamic Web Project + C# .NET API 组合的项目
创建 Eclipse 项目
File
-> New
-> Project
Web
-> Dynamic Web Project
-> 选择 apache-tomcat-9.0.65
-> 打勾 Generate web.xml deployment descriptor
文件目录
java
- AppDBContext
- Controller
- Models
webapp
- Assets
- Images
- Layout
- Other JSP File
css
js
view
resource
放配件
开始
创建 Servlet 文件
Java Resources -> src/main/java -> controller 右键 new -> servlet
AppDBContext
以下是连接 API NET
的写法例子
api_Admin
例子
package AppDBContext;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.mindrot.jbcrypt.BCrypt;
import Models.Account;
import Models.Admin;
import Models.BankAccount;
public class api_Admin {
public static String tokem_id = null;
public static String token_Password = null;
// 获取 Admin URL
public static String URL()throws Exception {
final URL url = new URL("http://localhost:5200/api/Admin/");
return url.toString();
}
// 获取 Token
public static void getKEY(String id , String Password) {
tokem_id = id;
token_Password = Password;
}
// 验证 Token
public static String Token()throws Exception {
URL _url = new URL("http://localhost:5200/api/AdminToken");
HttpURLConnection connection = null;
connection = (HttpURLConnection) _url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
JSONObject obj = new JSONObject();
obj.put("Username", tokem_id);
obj.put("Password", token_Password);
String data = obj.toString();
byte[] dataBytes = data.getBytes();
OutputStream outputStream = connection.getOutputStream();
outputStream.write(dataBytes);
outputStream.close();
if(connection.getResponseCode() != 400) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
String Token = result.toString();
return Token;
}
return null;
}
// 获取 所有 Admin 数据
public static List<Admin> getAllAdmin()throws Exception{
JSONObject obj = new JSONObject();
String _url = URL();
URL url = new URL(_url);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
String Token = Token();
connection.setRequestProperty("Authorization", "Bearer " + Token);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while((line=reader.readLine()) != null) {
result.append(line);
}
reader.close();
JSONArray array = new JSONArray(result.toString());
List<Admin> list = new ArrayList<>();
for(int i = 0 ; i < array.length();i++) {
obj = array.getJSONObject(i);
int id = obj.getInt("adminID");
String fullname = obj.getString("fullName");
String img = obj.getString("img");
String username = obj.getString("username");
String password = obj.getString("password");
list.add(new Admin(id,fullname,img,username,password));
}
return list;
}
// 创建 Admin 数据
public static void CreateAdmin(Admin admin)throws Exception {
String _url = URL();
URL url = new URL(_url);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
String Token = Token();
connection.setRequestProperty("Authorization", "Bearer " + Token);
connection.setDoOutput(true);
JSONObject obj = new JSONObject();
obj.put("AdminID", 0);
obj.put("FullName", admin.getFullName());
obj.put("IMG", admin.getIMG());
obj.put("Username", admin.getUsername());
obj.put("Password", admin.getPassword());
String data = obj.toString();
byte[] dataBytes = data.getBytes();
OutputStream outputStream = connection.getOutputStream();
outputStream.write(dataBytes);
outputStream.close();
if(connection.getResponseCode() == 201) {
return;
}else {
return;
}
}
// 更新 Admin 数据
public static void UpdateAccount(Admin admin)throws Exception {
String url = URL()+admin.getAdminID();
URL _url = new URL(url);
HttpURLConnection connection = (HttpURLConnection) _url.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
String Token = Token();
connection.setRequestProperty("Authorization", "Bearer " + Token);
connection.setDoOutput(true);
JSONObject obj = new JSONObject();
obj.put("AdminID", admin.getAdminID());
obj.put("FullName", admin.getFullName());
obj.put("IMG", admin.getIMG());
obj.put("Username", admin.getUsername());
obj.put("Password", admin.getPassword());
String data = obj.toString();
byte[] dataBytes = data.getBytes();
OutputStream outputStream = connection.getOutputStream();
outputStream.write(dataBytes);
outputStream.close();
System.out.println(connection.getResponseCode() == 204);
if(connection.getResponseCode() == 204) {
return;
}else {
return;
}
}
// 获取 一个 Admin 数据
public static Admin getAdmin(String username,String password)throws Exception {
List<Admin> list_admin = getAllAdmin();
Admin admin = null;
for(var i : list_admin) {
if(i.getUsername().equals(username) && BCrypt.checkpw(password, i.getPassword())) {
admin = i;
break;
}
}
return admin;
}
// 获取 所有 Admin 数据 List
public static List<Admin> getAdmin_list(int id)throws Exception {
List<Admin> list_admin = getAllAdmin();
List<Admin> list_admin2 = new ArrayList<>();
for(var i : list_admin) {
if(i.getAdminID()!=id) {
list_admin2.add(i);
}
}
return list_admin2;
}
// 验证 Admin 名字
public static boolean getAdmin_CheckFullname(String fullname)throws Exception {
List<Admin> list_admin = getAllAdmin();
boolean b = false;
for(var i : list_admin) {
if(i.getFullName().equals(fullname)) {
b = true;
break;
}
}
return b;
}
// 验证 Admin id
public static boolean getAdmin__Checkid(String username)throws Exception {
List<Admin> list_admin = getAllAdmin();
boolean b = false;
for(var i : list_admin) {
if(i.getUsername().equals(username)) {
b = true;
break;
}
}
return b;
}
// 验证 Admin 密码
public static boolean getAdmin__Checkpassword(String password)throws Exception {
List<Admin> list_admin = getAllAdmin();
boolean b = false;
for(var i : list_admin) {
if(BCrypt.checkpw(password, i.getPassword())) {
b = true;
break;
}
}
return b;
}
}
其他例子 包含 CRUD
package AppDBContext;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import Models.Account;
import Models.BankAccount;
import Models.Transaction;
public class api_Transaction {
static api_Admin api_admin = new api_Admin();
static api_Account api_account = new api_Account();
// 获取 URL
public static String URL()throws Exception {
final URL url = new URL("http://localhost:5200/api/Transaction/");
return url.toString();
}
// 获取 所有 数据
public static List<Transaction> getAllTransaction()throws Exception{
JSONObject obj = new JSONObject();
String _url = URL();
URL url = new URL(_url);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
String Token = api_account.Token();
connection.setRequestProperty("Authorization", "Bearer " + Token);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while((line=reader.readLine()) != null) {
result.append(line);
}
reader.close();
JSONArray array = new JSONArray(result.toString());
List<Transaction> list = new ArrayList<>();
for(int i = 0 ; i < array.length();i++) {
obj = array.getJSONObject(i);
int id = obj.getInt("transactionID");
int BankAccountID = obj.getInt("bankAccountID");
String TransactionType = obj.getString("transactionType");
String TransactionStatus = obj.getString("transactionStatus");
String Date= obj.getString("date");
String Description = obj.getString("description");
String Credit = obj.getString("credit");
String Debit = obj.getString("debit");
double Amount = obj.getDouble("amount");
boolean IsIBG = obj.getBoolean("isIBG");
double SMSNotification = obj.getDouble("smsNotification");
String PhoneNumber = obj.getString("phoneNumber");
String Email = obj.getString("email");
String OtherDescription = obj.getString("otherDescription");
list.add(new Transaction(id,BankAccountID,TransactionType,TransactionStatus,Date,Description,Credit,Debit,Amount,
IsIBG,SMSNotification,PhoneNumber,Email,OtherDescription));
}
return list;
}
// 获取 所有 数据 方式 2
public static List<Transaction> getAllTransaction2()throws Exception{
JSONObject obj = new JSONObject();
String _url = URL();
URL url = new URL(_url);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
String Token = api_admin.Token();
connection.setRequestProperty("Authorization", "Bearer " + Token);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while((line=reader.readLine()) != null) {
result.append(line);
}
reader.close();
JSONArray array = new JSONArray(result.toString());
List<Transaction> list = new ArrayList<>();
for(int i = 0 ; i < array.length();i++) {
obj = array.getJSONObject(i);
int id = obj.getInt("transactionID");
int BankAccountID = obj.getInt("bankAccountID");
String TransactionType = obj.getString("transactionType");
String TransactionStatus = obj.getString("transactionStatus");
String Date= obj.getString("date");
String Description = obj.getString("description");
String Credit = obj.getString("credit");
String Debit = obj.getString("debit");
double Amount = obj.getDouble("amount");
boolean IsIBG = obj.getBoolean("isIBG");
double SMSNotification = obj.getDouble("smsNotification");
String PhoneNumber = obj.getString("phoneNumber");
String Email = obj.getString("email");
String OtherDescription = obj.getString("otherDescription");
list.add(new Transaction(id,BankAccountID,TransactionType,TransactionStatus,Date,Description,Credit,Debit,Amount,
IsIBG,SMSNotification,PhoneNumber,Email,OtherDescription));
}
return list;
}
// 创建
public static void CreateTransaction(Transaction tr)throws Exception {
String _url = URL();
URL url = new URL(_url);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
String Token = api_account.Token();
connection.setRequestProperty("Authorization", "Bearer " + Token);
connection.setDoOutput(true);
JSONObject obj = new JSONObject();
obj.put("TransactionID", 0);
obj.put("BankAccountID", tr.getBankAccountID());
obj.put("TransactionType", tr.getTransactionType());
obj.put("TransactionStatus", tr.getTransactionStatus());
obj.put("Date", tr.getDate());
obj.put("Description", tr.getDescription());
obj.put("Credit", tr.getCredit());
obj.put("Debit", tr.getDebit());
obj.put("Amount", tr.getAmount());
obj.put("IsIBG", tr.isIsIBG());
obj.put("SMSNotification", tr.getSMSNotification());
obj.put("PhoneNumber", tr.getPhoneNumber());
obj.put("Email", tr.getEmail());
obj.put("OtherDescription", tr.getOtherDescription());
String data = obj.toString();
byte[] dataBytes = data.getBytes();
OutputStream outputStream = connection.getOutputStream();
outputStream.write(dataBytes);
outputStream.close();
if(connection.getResponseCode() == 201) {
return;
}else {
return;
}
}
// 更新
public static void UpdateTransaction(Transaction tr)throws Exception {
String url = URL()+tr.getTransactionID();
URL _url = new URL(url);
HttpURLConnection connection = (HttpURLConnection) _url.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
String Token = api_admin.Token();
connection.setRequestProperty("Authorization", "Bearer " + Token);
connection.setDoOutput(true);
JSONObject obj = new JSONObject();
obj.put("TransactionID", tr.getTransactionID());
obj.put("BankAccountID", tr.getBankAccountID());
obj.put("TransactionType", tr.getTransactionType());
obj.put("TransactionStatus", tr.getTransactionStatus());
obj.put("Date", tr.getDate());
obj.put("Description", tr.getDescription());
obj.put("Credit", tr.getCredit());
obj.put("Debit", tr.getDebit());
obj.put("Amount", tr.getAmount());
obj.put("IsIBG", tr.isIsIBG());
obj.put("SMSNotification", tr.getSMSNotification());
obj.put("PhoneNumber", tr.getPhoneNumber());
obj.put("Email", tr.getEmail());
obj.put("OtherDescription", tr.getOtherDescription());
String data = obj.toString();
byte[] dataBytes = data.getBytes();
OutputStream outputStream = connection.getOutputStream();
outputStream.write(dataBytes);
outputStream.close();
System.out.println(connection.getResponseCode());
if(connection.getResponseCode() == 204) {
return;
}else {
return;
}
}
// 获取 数据
public static Transaction getBankAccount_ByCardnum(String date,String des, String cre)throws Exception {
List<Transaction> list_tr = getAllTransaction();
Transaction tr = null;
for(var i : list_tr) {
if(i.getDate().equals(date) && i.getDescription().equals(des) && i.getCredit().equals(cre) ) {
tr = i;
break;
}
}
return tr;
}
// 获取 数据 BY id
public static Transaction gettr_Byid(int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
Transaction tr = null;
for(var i : list_tr) {
if(i.getTransactionID() == id ) {
tr = i;
break;
}
}
return tr;
}
// 获取 数据 BY id
public static Transaction gettr_Byid2(int id)throws Exception {
List<Transaction> list_tr = getAllTransaction2();
Transaction tr = null;
for(var i : list_tr) {
if(i.getTransactionID() == id ) {
tr = i;
break;
}
}
return tr;
}
// 获取 List
public static List<Transaction> getBankAccount_ByList(String debit,String date,String name, String pass)throws Exception {
api_account.getKEY(name, pass);
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
String da = i.getDate();
String time = da.substring(0, 10);
if(date.equals(time) && i.getDebit().equals(debit) && (i.getTransactionType().equals("MB Other Account") || i.getTransactionType().equals("IBG Transfer")) && i.getTransactionStatus().equals("true") && i.isIsIBG() == true) {
tr.add(i);
}
}
return tr;
}
// 获取 List
public static List<Transaction> getBankAccount_ByBankAccountID(int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
if(i.getBankAccountID() == id && i.getTransactionStatus().equals("true") && i.isIsIBG() == true) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_Own(int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
if(i.getBankAccountID() == id && i.getTransactionType().equals("Transfer Own Account") && i.getTransactionStatus().equals("true")) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_IBG(int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
if(i.getBankAccountID() == id && i.getTransactionType().equals("IBG Transfer") && i.isIsIBG() == true && i.getTransactionStatus().equals("true")) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_Payment(int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
if(i.getBankAccountID() == id && i.getTransactionType().equals("Payment Transfer") && i.getTransactionStatus().equals("true")) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_Other(int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
if(i.getBankAccountID() == id && i.getTransactionType().equals("MB Other Account") && i.getTransactionStatus().equals("true")) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getIBG()throws Exception {
List<Transaction> list_tr = getAllTransaction2();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
if(i.isIsIBG() == false && i.getTransactionType().equals("IBG Transfer") && i.getTransactionStatus().equals("true")) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_All()throws Exception {
List<Transaction> list_tr = getAllTransaction2();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
if(i.getTransactionStatus().equals("true") && i.isIsIBG() == true && (i.getTransactionType().equals("IBG Transfer") || i.getTransactionType().equals("MB Other Account"))) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_Select(String year,String month,int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
String month1 = i.getDate().substring(5, 7);
String year1 = i.getDate().substring(0, 4);
if(i.getBankAccountID() == id && i.getTransactionStatus().equals("true") && i.isIsIBG() == true &&
month1.equals(month) && year.equals(year)) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_Own_Select(String year,String month,int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
String month1 = i.getDate().substring(5, 7);
String year1 = i.getDate().substring(0, 4);
if(i.getBankAccountID() == id && i.getTransactionType().equals("Transfer Own Account") && i.getTransactionStatus().equals("true") &&
month1.equals(month) && year.equals(year)) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_IBG_Select(String year,String month,int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
String month1 = i.getDate().substring(5, 7);
String year1 = i.getDate().substring(0, 4);
if(i.getBankAccountID() == id && i.getTransactionType().equals("IBG Transfer") && i.isIsIBG() == true && i.getTransactionStatus().equals("true") &&
month1.equals(month) && year.equals(year)) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_Payment_Select(String year,String month,int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
String month1 = i.getDate().substring(5, 7);
String year1 = i.getDate().substring(0, 4);
if(i.getBankAccountID() == id && i.getTransactionType().equals("Payment Transfer") && i.getTransactionStatus().equals("true") &&
month1.equals(month) && year.equals(year)) {
tr.add(i);
}
}
return tr;
}
public static List<Transaction> getBankAccount_ByBankAccountID_Other_Select(String year,String month,int id)throws Exception {
List<Transaction> list_tr = getAllTransaction();
List<Transaction> tr = new ArrayList<>();
for(var i : list_tr) {
String month1 = i.getDate().substring(5, 7);
String year1 = i.getDate().substring(0, 4);
if(i.getBankAccountID() == id && i.getTransactionType().equals("MB Other Account") && i.getTransactionStatus().equals("true") &&
month1.equals(month) && year.equals(year)) {
tr.add(i);
}
}
return tr;
}
}
Models
Admin 例子
package Models;
public class Admin {
private int AdminID;
private String FullName;
private String IMG;
private String Username;
private String Password;
public Admin() {
}
public Admin(int adminID, String fullName, String iMG, String username, String password) {
AdminID = adminID;
FullName = fullName;
IMG = iMG;
Username = username;
Password = password;
}
public int getAdminID() {
return AdminID;
}
public void setAdminID(int adminID) {
AdminID = adminID;
}
public String getFullName() {
return FullName;
}
public void setFullName(String fullName) {
FullName = fullName;
}
public String getIMG() {
return IMG;
}
public void setIMG(String iMG) {
IMG = iMG;
}
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
}
Controller
Admin_login 例子
package Controller;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import AppDBContext.api_Account;
import AppDBContext.api_Admin;
import AppDBContext.api_BankAccount;
import AppDBContext.api_Transaction;
import Models.Account;
import Models.Admin;
import Models.BankAccount;
import Models.Transaction;
/**
* Servlet implementation class admin_login
*/
public class admin_login extends HttpServlet {
private static final long serialVersionUID = 1L;
public static api_Admin api_admin = new api_Admin();
public static api_Transaction api_tr = new api_Transaction();
public static api_BankAccount api_bank = new api_BankAccount();
public static api_Account api_acc = new api_Account();
/**
* @see HttpServlet#HttpServlet()
*/
public admin_login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String userid = request.getParameter("userid");
String password = request.getParameter("password");
try {
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
Admin admin = api_admin.getAdmin(userid, password);
if(admin != null) {
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
List<Admin> list = api_admin.getAdmin_list(admin.getAdminID());
session.setAttribute("Admin", admin);
session.setAttribute("Adminlist", list);
session.setAttribute("adminlogin", "");
new_Change();
limit_Change();
LocalDate localDate = LocalDate.now();
Month month = localDate.getMonth();
String[] str = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString().split("-");
String day = str[2];
Date date = new Date();
session.setAttribute("month", month.toString());
session.setAttribute("day", day.toString());
session.setAttribute("mytime", date.toString());
response.sendRedirect("Admin/page/admin_home.jsp");
return;
}else {
session.setAttribute("adminlogin", "Login failed.");
response.sendRedirect("Admin/page/Login.jsp");
return;
}
}catch(Exception e) {
session.setAttribute("adminlogin", "Login failed.");
response.sendRedirect("Admin/page/Login.jsp");
return;
}
}
// 自动 更新 登入 次数
public static void new_Change() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.now();
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
List<Transaction> tr = api_tr.getIBG();
if(tr.size() != 0) {
for(var i : tr) {
String date = i.getDate().substring(0, 10);
Date bt = sdf.parse(dtf.format(now).toString());
Date et = sdf.parse(date);
if(bt.before(et)) {}else{
//加钱
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
BankAccount bank = api_bank.getBankAccount_ByCardnum(i.getCredit());
double balance = bank.getBalance() + i.getAmount();
bank.setBalance(balance);
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
api_bank.UpdateBankAccountID(bank);
//IBG true
i.setIsIBG(true);
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
api_tr.UpdateTransaction(i);
}
}
}
return;
}catch(Exception e) {
return;
}
}
// 登入 次数
public static void limit_Change() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.now();
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
List<Account> a = api_acc.getAllAccount();
for(var i : a) {
String date = i.getLoginTime().substring(0, 10);
Date bt = sdf.parse(dtf.format(now).toString());
Date et = sdf.parse(date);
if(bt.before(et)) {}else{
//变回limit
//变回次数 false
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
List<BankAccount> Allbank = api_bank.getListBankAccount_By_AccountID(i.getAccountID());
for(var j : Allbank) {
double limit = j.getAllLimit();
j.setCardLimit(limit);
j.setChange(false);
api_admin.getKEY("Hoo123", "$2a$10$DlgC8wjFMWO0f1uYCyuKPeu3ADtNZNFmNJNBPglE1/NVCwWohERdC");
api_bank.UpdateBankAccountID(j);
}
}
}
return;
}catch(Exception e) {
return;
}
}
}
Controller/Ajax
package Controller;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import AppDBContext.api_Account;
import AppDBContext.api_Admin;
import AppDBContext.api_BankAccount;
import AppDBContext.api_User;
import Models.Admin;
import Models.BankAccount;
import Models.User;
/**
* Servlet implementation class AjaxFunction
*/
public class AjaxFunction extends HttpServlet {
private static final long serialVersionUID = 1L;
public static api_Account api_account = new api_Account();
public static api_Admin api_admin = new api_Admin();
public static api_BankAccount api_bank = new api_BankAccount();
public static api_User api_user = new api_User();
/**
* @see HttpServlet#HttpServlet()
*/
public AjaxFunction() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String Action = request.getParameter("Action");
if(Action.equals("getAllBankAccount_To_View")) {
getAllBankAccount_To_View(request,response);
return;
}else if(Action.equals("getAllUser")) {
getAllUser(request,response);
return;
}else if(Action.equals("SearchAllUser")) {
SearchAllUser(request,response);
return;
}else if(Action.equals("update")) {
HttpSession session = request.getSession();
session.setAttribute("updateuseradmin", "");
String Fullname = request.getParameter("Fullname");
String email = request.getParameter("email");
String nric = request.getParameter("nric");
String age = request.getParameter("age");
String gender = request.getParameter("gender");
String birth = request.getParameter("birth");
Admin admin = (Admin)session.getAttribute("Admin");
User u = (User)session.getAttribute("upuser");
Gson gson = new Gson();
List<User> b_list = new ArrayList<>();
try {
api_admin.getKEY(admin.getUsername(), admin.getPassword());
b_list = api_user.getAllUser();
if(!u.getFullName().equals(Fullname)) {
for(var i : b_list) {
if(i.getFullName().equals(Fullname)) {
session.setAttribute("updateuseradmin", "This fullname is already in use");
// response.sendRedirect("Admin/page/user_select.jsp");
// return;
String json = gson.toJson(false);
response.getWriter().write(json);
return;
}
}
u.setFullName(Fullname);
}
if(!u.getEmail().equals(email)) {
for(var i : b_list) {
if(i.getEmail().equals(email)) {
session.setAttribute("updateuseradmin", "This email is already in use");
// response.sendRedirect("Admin/page/user_select.jsp");
// return;
String json = gson.toJson(false);
response.getWriter().write(json);
return;
}
}
u.setEmail(email);
}
if(!u.getNRIC().equals(nric)) {
for(var i : b_list) {
if(i.getNRIC().equals(nric)) {
session.setAttribute("updateuseradmin", "This nric is already in use");
// response.sendRedirect("Admin/page/user_select.jsp");
// return;
String json = gson.toJson(false);
response.getWriter().write(json);
return;
}
}
int ageint = Integer.valueOf(age);
if(age != null) {
if(ageint < 18 && ageint > 60) {
session.setAttribute("updateuseradmin", "Age must be 18 - 60");
// response.sendRedirect("Admin/page/user_select.jsp");
// return;
String json = gson.toJson(false);
response.getWriter().write(json);
return;
}else {
u.setNRIC(nric);
u.setEmail(email);
u.setAge(ageint);
u.setGender(gender);
u.setBirthDate(birth);
}
}
}
api_admin.getKEY(admin.getUsername(), admin.getPassword());
api_user.UpdateAccount(u);
session.setAttribute("upuser", u);
session.setAttribute("updateuseradmin", "");
}catch(Exception e) {
e.getMessage();
}
String json = gson.toJson(true);
response.getWriter().write(json);
return;
}else if(Action.equals("blocked")) {
Block(request,response);
return;
}
return;
}
private void getAllBankAccount_To_View(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String dataUsername = request.getParameter("AccountData");
Gson gson = new Gson();
List<BankAccount> b_list = new ArrayList<>();
int id = Integer.valueOf(dataUsername);
try {
b_list = api_bank.getListBankAccount_By_AccountID(id);
}catch(Exception e) {
e.getMessage();
}
String json = gson.toJson(b_list);
response.getWriter().write(json);
}
private void getAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Admin admin = (Admin)session.getAttribute("Admin");
Gson gson = new Gson();
List<User> b_list = new ArrayList<>();
try {
api_admin.getKEY(admin.getUsername(), admin.getPassword());
b_list = api_user.getAllUser();
}catch(Exception e) {
e.getMessage();
}
String json = gson.toJson(b_list);
response.getWriter().write(json);
}
private void SearchAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String dataUsername = request.getParameter("Value");
Admin admin = (Admin)session.getAttribute("Admin");
Gson gson = new Gson();
List<User> b_list = new ArrayList<>();
try {
api_admin.getKEY(admin.getUsername(), admin.getPassword());
b_list = api_user.getUser_list(dataUsername);
if(b_list.size() == 0) {
api_admin.getKEY(admin.getUsername(), admin.getPassword());
b_list = api_user.getAllUser();
}
}catch(Exception e) {
e.getMessage();
}
String json = gson.toJson(b_list);
response.getWriter().write(json);
}
private void Block(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String id = request.getParameter("id");
Admin admin = (Admin)session.getAttribute("Admin");
Gson gson = new Gson();
List<User> b_list = new ArrayList<>();
try {
api_admin.getKEY(admin.getUsername(), admin.getPassword());
b_list = api_user.getAllUser();
User u = new User();
int mid = Integer.valueOf(id);
for(var i : b_list) {
if(i.getUserID() == mid) {
u = i;
break;
}
}
if(u.isActive() == true) {
u.setActive(false);
}else {
u.setActive(true);
}
api_admin.getKEY(admin.getUsername(), admin.getPassword());
api_user.UpdateAccount(u);
}catch(Exception e) {
e.getMessage();
}
String json = gson.toJson(b_list);
response.getWriter().write(json);
return;
}
}
HttpServletRequest
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String fullname = request.getParameter("fullname");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String nric = request.getParameter("nric");
String text = request.getParameter("text");
sendRedirect
response.sendRedirect("MeyBank_Home/page/contact.jsp");
return;
Session 转换
Account acc = (Account)session.getAttribute("Account"); // 取出数据
session.setAttribute("AllHistory", tr_list); // 保存数据
List
List<BankAccount> b_list = new ArrayList<>();
List<Transaction> tr_list = new ArrayList<>();
for(var i : b_list) {
tr_list.addAll(api_tr.getBankAccount_ByBankAccountID(i.getBankAccountID()));
}
// 添加 数据