-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectApplicationsServlet.java
More file actions
96 lines (69 loc) · 2.5 KB
/
Copy pathProjectApplicationsServlet.java
File metadata and controls
96 lines (69 loc) · 2.5 KB
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
package controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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;
@WebServlet("/ProjectApplicationsServlet")
public class ProjectApplicationsServlet extends HttpServlet {
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
if (session == null ||
session.getAttribute("userId") == null) {
response.sendRedirect("index.jsp");
return;
}
int ownerId =
(Integer) session.getAttribute("userId");
String action = request.getParameter("action");
int requestId =
Integer.parseInt(
request.getParameter("request_id")
);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn =
DriverManager.getConnection(
"jdbc:mysql://localhost:3306/devconnect_db",
"root",
"Harsh@@@123"
);
String sql;
if ("approve".equals(action)) {
sql =
"UPDATE project_requests r " +
"JOIN projects p ON r.project_id=p.id " +
"SET r.status='APPROVED' " +
"WHERE r.id=? AND p.posted_by=?";
} else if ("reject".equals(action)) {
sql =
"UPDATE project_requests r " +
"JOIN projects p ON r.project_id=p.id " +
"SET r.status='REJECTED' " +
"WHERE r.id=? AND p.posted_by=?";
} else {
response.sendError(400);
return;
}
PreparedStatement ps =
conn.prepareStatement(sql);
ps.setInt(1, requestId);
ps.setInt(2, ownerId);
ps.executeUpdate();
ps.close();
conn.close();
response.sendRedirect("my_projects.jsp");
} catch(Exception e) {
e.printStackTrace();
response.sendError(500, e.getMessage());
}
}
}