-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManagerServlet.java
227 lines (173 loc) · 6.63 KB
/
ManagerServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.banksystem.controller;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.naming.directory.SearchControls;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.banksystem.entity.Loan;
import com.banksystem.entity.LoanScheme;
import com.banksystem.entity.Manager;
import com.banksystem.service.ManagerService;
import com.banksystem.service.impl.ManagerServiceImpl;
import com.banksystem.service.impl.UserServiceimpl;
import com.alibaba.fastjson.JSON;
@WebServlet("/ManagerServlet")
public class ManagerServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//set character encoding to solve messy code
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");
//call the corresponding function
if ("login".equals(method)) {
login(req, resp);
} else if ("loginOut".equals(method)) {
LoginOut(req, resp);
} else if ("selectAllLoans".equals(method)) {
selectAllLoans(req, resp);
}else if("selectRLoans".equals(method)) {
selectRLoans(req, resp);
}else if ("PassLoan".equals(method)) {
PassLoan(req, resp);
} else if ("RejectLoan".equals(method)) {
RejectLoan(req, resp);
}else if("QuOneLs".equals(method)) {
QuOneLs(req,resp);
}
}
/********************** login ******************/
protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userid = null;
String pswd = null;
// get the input data
userid = req.getParameter("userid");
pswd = req.getParameter("pswd");
//call the encapsulated logical layer function
ManagerService managers = new ManagerServiceImpl();
Manager manager=null;
try {
//pass parameters from the html page
manager = managers.ManagerLogin(userid, pswd);
if (manager != null) {
//get user information from session and don't need to get access to the date through database
// get the session object
HttpSession session = req.getSession();
//store the value into session
session.setAttribute("manager", manager);
//direct to the corresponding manager-main page
resp.sendRedirect("m-main.html");
return;
} else {
//direct to the corresponding login pages
req.setAttribute("msg", "input error!");
req.getRequestDispatcher("login.html").forward(req, resp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/********************** loginout ****************/
protected void LoginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//log out the session object
req.getSession().invalidate();
// direct to the corresponding login pages
resp.sendRedirect(req.getContextPath() + "login.html");
}
/****** check all outstanding records *********/
protected void selectAllLoans(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Manager manager=(Manager) req.getSession().getAttribute("manager");
String selfbankname=manager.getBankname();
ManagerService managerService= new ManagerServiceImpl();
List<Loan> loans = null;
try {
//call logical function and return the records in List format
loans = managerService.selectAllLoans(selfbankname);
} catch (Exception e) {
e.printStackTrace();
}
// transfer data in json format
sendMsgToHTMLWithFastJson(resp, loans);
return;
}
/****** check a single record through loanID *********/
protected void QuOneLs(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String loanID = req.getParameter("loanID");
UserServiceimpl users = new UserServiceimpl();
LoanScheme ls=null;
try {
ls= users.UserquloanByloanID(loanID);
} catch (SQLException e) {
e.printStackTrace();
}
//call function to convert the data into json format
sendMsgToHTMLWithFastJson(resp, ls);
return;
}
/******* query pending records ******/
protected void selectRLoans(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Manager manager=(Manager) req.getSession().getAttribute("manager");
String selfbankname=manager.getBankname();
ManagerService managerService= new ManagerServiceImpl();
List<Loan> rloans = null;
try {
rloans = managerService.selectRLoans(selfbankname);
} catch (Exception e) {
e.printStackTrace();
}
}
/******* pass the loan **********/
protected void PassLoan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String loanID = req.getParameter("loanID");
int status = 1; //1: pass
int update = -1; //init as pass operation error
ManagerService manager = new ManagerServiceImpl();
try {
update = manager.CheckLoan(loanID, status);
} catch (Exception e) {
e.printStackTrace();
}
if (update > 0) //the update is successful
{
System.out.println("sucess" + status);
} else {
req.setAttribute("msg", "error");
}
//return the corresponding data
sendMsgToHTMLWithFastJson(resp, update);
}
/****** loan rejection **********/
protected void RejectLoan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String loanID = req.getParameter("loanID");
int status = 2; //2:reject
int update = -1; //init as reject operation error
ManagerService manager = new ManagerServiceImpl();
try {
update = manager.CheckLoan(loanID, status);
} catch (Exception e) {
e.printStackTrace();
}
if (update > 0) //the update is successful
{
System.out.println("success" + status);
} else {
req.setAttribute("msg", "error");
}
//return the corresponding data
sendMsgToHTMLWithFastJson(resp, update);
}
/****** convert data into json format and render it on the front-end page *******/
private void sendMsgToHTMLWithFastJson(HttpServletResponse resp, Object obj) throws ServletException, IOException {
String msg = JSON.toJSONString(obj);
System.out.println(msg);
resp.setContentType("json/application;charset=UTF-8");
resp.getWriter().write(msg);
return;
}
}