-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostProjectServlet.java
More file actions
75 lines (56 loc) · 2.07 KB
/
Copy pathPostProjectServlet.java
File metadata and controls
75 lines (56 loc) · 2.07 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
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("/PostProjectServlet")
public class PostProjectServlet 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 userId =
(Integer) session.getAttribute("userId");
String title = request.getParameter("title");
String description = request.getParameter("description");
String skills = request.getParameter("required_skills");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn =
DriverManager.getConnection(
"jdbc:mysql://localhost:3306/devconnect_db",
"root",
"Harsh@@@123"
);
PreparedStatement ps =
conn.prepareStatement(
"INSERT INTO projects " +
"(title,description,required_skills,posted_by,status) " +
"VALUES (?,?,?,?, 'ACTIVE')"
);
ps.setString(1, title);
ps.setString(2, description);
ps.setString(3, skills);
ps.setInt(4, userId);
ps.executeUpdate();
ps.close();
conn.close();
response.sendRedirect("marketplace.jsp");
} catch(Exception e) {
e.printStackTrace();
response.sendError(500, e.getMessage());
}
}
}