/
search.jsp
396 lines (350 loc) · 10.8 KB
/
search.jsp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ include file="checkvalid.jsp" %>
<%@page import="java.util.*"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.*"%>
<%@page import="com.mysql.jdbc.ResultSetMetaData"%>
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
/** 链接数据库参数 **/
String driverName = "com.mysql.jdbc.Driver"; //驱动名称
String DBUser = "admin"; //mysql用户名
String DBPasswd = "1234567890"; //mysql密码
String DBName = "working"; //数据库名
String MySQLServer = "127.0.0.1"; //MySQL地址
String MySQLServerPort = "3306"; //MySQL端口号(默认为3306)
//数据库完整链接地址
String connUrl = "jdbc:mysql://"+MySQLServer+":"+MySQLServerPort+"/" + DBName + "?user="
+ DBUser + "&password=" + DBPasswd ;
//加载数据库驱动
Class.forName(driverName).newInstance();
//链接数据库并保存到 conn 变量中
Connection conn = DriverManager.getConnection(connUrl);
//申明~?
Statement stmt = conn.createStatement();
//设置字符集
stmt.executeQuery("SET NAMES UTF8");
//要执行的 sql 查询
String email=(String)session.getAttribute("email");
String searchName=(String)request.getParameter("searchName");
System.out.println(searchName);
String sql=null;
%>
<html>
<head>
<title>Find friends</title>
<meta http-equiv="content-Type" content="text/html;charset=UTF-8">
<style>
section {
height: 100vh;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
position: relative;
}
.blur-container {
background-size: cover;
background-position: center;
background-attachment: fixed;
}
.blur-container::before, .blur-container::after {
top: 0px;
left: 0px;
content: '';
width: 100%;
height: 100%;
display: block;
position: absolute;
}
.blur-container .container-fluid {
z-index: 10;
width: 100%;
height: 100%;
}
.blur-container .blur-box {
display: block;
position: relative;
}
.blur-container .blur-box::before, .blur-container .blur-box::after {
top: 0px;
left: 0px;
// content: '';
width: 100%;
height: 100%;
display: block;
position: absolute;
}
.blur-container.blur-8 {
--bg: url(background2.jpg);
overflow: hidden;
background-image: var(--bg);
}
.blur-container.blur-8::after {
background-size: cover;
background-position: center;
background-attachment: fixed;
opacity: 0.8;
-webkit-filter: blur(6px);
filter: blur(6px);
background-image: var(--bg);
}
.blur-container.blur-8 .blur-box {
background-size: cover;
background-position: center;
background-attachment: fixed;
color: #fff;
width: 100%;
height: 90%;
z-index: 11;
max-width: 600px;
max-height: 400px;
border-radius: 10px;
background-image: var(--bg);
box-shadow: 0px 0px 30px #333;
}
.btn {
display: inline-block;
font-weight: 400;
line-height: 1.25;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
border: 1px solid transparent;
padding: .5rem 1rem;
font-size: 1rem;
border-radius: .25rem;
-webkit-transition: all .2s ease-in-out;
-o-transition: all .2s ease-in-out;
transition: all .2s ease-in-out;
}
a {
background-color: transparent;
-webkit-text-decoration-skip: objects;
}
.blur-container.blur-5 {
--bg: url(background2.jpg);
background-image: var(--bg);
}
.blur-container.blur-5::after {
background-size: cover;
background-position: center;
background-attachment: fixed;
opacity: 0.1;
-webkit-filter: blur(6px);
filter: blur(6px);
background-image: var(--bg);
}
.blur-container.blur-5 .blur-box {
width: 100%;
height: 100px;
overflow: hidden;
}
.blur-container.blur-5 .blur-box .btn {
color: #fff;
-webkit-transition: 0s;
transition: 0s;
cursor: pointer;
overflow: hidden;
position: relative;
padding: 10px 40px;
border: 2px solid #fff;
text-decoration: none;
}
.blur-container.blur-5 .blur-box .btn span {
position: relative;
z-index: 10;
}
.blur-container.blur-5 .blur-box .btn::before, .blur-container.blur-5 .blur-box .btn::after {
content: '';
top: 0px;
left: 0px;
width: 100%;
height: 100%;
display: block;
-webkit-transition: 0.3s;
transition: 0.3s;
position: absolute;
}
.blur-container.blur-5 .blur-box .btn:hover::before {
opacity: 0.2;
background-color: #999;
}
.blur-container.blur-5 .blur-box .btn:hover::after {
-webkit-filter: blur(4px) brightness(120%);
filter: blur(4px) brightness(120%);
}
.blur-container.blur-5 .blur-box .btn::before {
z-index: 10;
opacity: 0.4;
background-color: #333;
}
.blur-container.blur-5 .blur-box .btn::after {
background-size: cover;
background-position: center;
background-attachment: fixed;
background-image: var(--bg);
-webkit-filter: blur(4px) brightness(70%);
filter: blur(4px) brightness(70%);
}
.blur-container.blur-5 .blur-box::before {
z-index: 10;
opacity: 0.4;
background-color: #333;
}
.blur-container.blur-5 .blur-box::after {
background-size: cover;
background-position: center;
background-attachment: fixed;
background-image: var(--bg);
-webkit-filter: blur(4px) brightness(70%);
filter: blur(4px) brightness(70%);
}
.align-items-center {
-webkit-box-align: center !important;
-webkit-align-items: center !important;
-ms-flex-align: center !important;
align-items: center !important;
}
.justify-content-center {
-webkit-box-pack: center !important;
-webkit-justify-content: center !important;
-ms-flex-pack: center !important;
justify-content: center !important;
}
}
</style>
<SCRIPT type="text/javascript">
function addFriend(email){
if (email!=""){
var xmlhttp=null;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null){
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
window.location.href="search.jsp?searchName=<%out.print(searchName);%>";
var s=document.getElementById(email);
s.innerHTML="Followed";
}
}
xmlhttp.open("GET","add.jsp?friendID="+email,true);
xmlhttp.send();
}
}else{
alert("Input something!");
}
}
</SCRIPT>
</head>
<body style="background:url(background2.jpg) fixed ;background-size:cover; background-position: center;">
<section class="blur-container blur-5 justify-content-center align-items-center" style="height:130px">
<div class="blur-box">
<div class="container-fluid d-flex justify-content-around align-items-center">
<a style="position:absolute; left:80px" href="main.jsp" rel="nofollow" rel="noreferrer"class="btn"><span>Hi,<%
sql= "SELECT * FROM `working`.`user` where email='"+email+"' LIMIT 15";
System.out.println(sql);
//取得结果
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()){
out.println(rs.getString("UserName"));
}%></</span></a>
<a style="position:absolute; left:600px" href="myFriends.jsp" rel="nofollow" rel="noreferrer"class="btn"><span>
Following
</span></a>
<a style="position:absolute; left:800px" href="personalInfo.jsp" rel="nofollow" rel="noreferrer"class="btn"><span>Profile</span></a>
<a style="position:absolute; left:1000px" value="Sign out" onclick="location.href='logout.jsp'" rel="nofollow" rel="noreferrer"class="btn"><span>Sign out</span></a>
</div>
</div>
</section>
<div style="position:absolute;left:1000px;top:100px">
<form action="search.jsp" method="post">
<input type="text" name="searchName" maxlength="20" style="width:120"/>
<input type="submit" value="Find friends"/>
</form>
</div>
<div style="height:5">
</div>
<%
if (searchName!=null){
HashSet<String> frd1 = new HashSet<String>();
sql= "SELECT * FROM `working`.`friends` as a, `working`.`user` as b, `working`.`userdetail` as c where a.email2 = c.email and a.email2 = b.email and a.email = '" + email + "'";
//取得结果
System.out.println(sql);
rs = stmt.executeQuery(sql);
while (rs.next()){
frd1.add(rs.getString("email2"));
}
HashMap<String, HashSet<String>> frd2 =
new HashMap<String, HashSet<String>>();
for(String frd : frd1){
sql= "SELECT * FROM `working`.`friends` as a, `working`.`user` as b, `working`.`userdetail` as c where a.email2 = c.email and a.email2 = b.email and a.email = '" + frd + "'";
System.out.println(sql);
rs = stmt.executeQuery(sql);
while(rs.next()){
String frd2eml = rs.getString("email2");
if(!frd2.containsKey(frd2eml))
frd2.put(frd2eml, new HashSet<String>());
frd2.get(frd2eml).add(frd);
}
}
frd2.keySet().removeAll(frd1);
frd2.keySet().remove(email);
sql= "SELECT * FROM `working`.`user` as a, `working`.`userdetail` as b "
+"where username like '%"+searchName+"%' "
+"and a.email = b.email "
+"and a.email !='"+email+"' "
+"and a.email not in ( "
+"select email2 from `working`.`friends` where email='"+email+"'"
+");";
//取得结果
System.out.println(sql);
rs = stmt.executeQuery(sql);
while (rs.next()){
%>
<section style="height:200px" class="blur-container blur-8 justify-content-center align-items-center">
<div style="position: reletive center" class="blur-box">
<a style="display:block;width:160px ;color:#FFFFFF;background-color:rgb(150,170,180);text-algn:center;text-decoration:none;padding:4px;;font-weight:bold;" href="view.jsp?email=<%out.print(rs.getString("email"));%>">
<%out.print(rs.getString("username"));%></a>
<%if(frd2.containsKey(rs.getString("email"))){%>
<a style="display:block; color:#FFFFFF;background-color:rgb(130,130,130);text-algn:center;text-decoration:none;padding:4px;">From<%
for(String inter:frd2.get(rs.getString("email"))){
sql= "SELECT * FROM `working`.`user` as b where b.email = '" + inter + "'";
System.out.println(sql);
Statement stmt2 = conn.createStatement();
ResultSet rs2 = stmt2.executeQuery(sql);
while(rs2.next())
out.print(" "+rs2.getString("username"));
stmt2.close();
rs2.close();
}%></a><%}%>
<br/>Gender: <%out.print(rs.getString("sex"));%></br></br>Date of birth: <%out.print(rs.getString("year"));%>/<%out.print(rs.getString("month"));%>/<%out.print(rs.getString("day"));%>
<span id="<%out.print(rs.getString("email"));%>"><input type="button" value="Follow" onclick="addFriend('<%out.print(rs.getString("email"));%>')" /></span>
</div>
</section>
<%
}
}
%>
</body>
</html>
<%
/** 关闭连接 **/
conn.close();
stmt.close();
rs.close();
%>