-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava 3.1
More file actions
244 lines (197 loc) · 7.02 KB
/
java 3.1
File metadata and controls
244 lines (197 loc) · 7.02 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
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
//Part (a): User Login Using Servlet and HTML Form
//login.html
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
</head>
<body>
<h2>Login Form</h2>
<form action="LoginServlet" method="post">
Username: <input type="text" name="username" required><br><br>
Password: <input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
//LoginServlet.java
package com.example;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
// Hardcoded credentials for simplicity
if ("admin".equals(username) && "12345".equals(password)) {
out.println("<h2>Welcome, " + username + "!</h2>");
} else {
out.println("<h3 style='color:red;'>Invalid credentials! Please try again.</h3>");
}
out.close();
}
}
//web.xml (Deployment Descriptor)
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
version="5.0">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.example.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
Part (b): Display Employee Records with JDBC and Servlet
Database Setup
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Salary DOUBLE
);
INSERT INTO Employee VALUES (101, 'Alice', 50000), (102, 'Bob', 60000), (103, 'Charlie', 70000);
//search.html
<!DOCTYPE html>
<html>
<head>
<title>Employee Search</title>
</head>
<body>
<h2>Search Employee by ID</h2>
<form action="EmployeeServlet" method="get">
Enter Employee ID: <input type="text" name="empid">
<input type="submit" value="Search">
</form>
<br>
<a href="EmployeeServlet">View All Employees</a>
</body>
</html>
//EmployeeServlet.java
package com.example;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class EmployeeServlet extends HttpServlet {
private static final String URL = "jdbc:mysql://localhost:3306/testdb";
private static final String USER = "root";
private static final String PASSWORD = "your_password";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String empIdParam = request.getParameter("empid");
try (Connection con = DriverManager.getConnection(URL, USER, PASSWORD)) {
Statement st = con.createStatement();
String query = (empIdParam != null && !empIdParam.isEmpty())
? "SELECT * FROM Employee WHERE EmpID=" + empIdParam
: "SELECT * FROM Employee";
ResultSet rs = st.executeQuery(query);
out.println("<html><body>");
out.println("<h2>Employee Details</h2>");
out.println("<table border='1'><tr><th>EmpID</th><th>Name</th><th>Salary</th></tr>");
boolean found = false;
while (rs.next()) {
found = true;
out.println("<tr><td>" + rs.getInt("EmpID") + "</td><td>"
+ rs.getString("Name") + "</td><td>"
+ rs.getDouble("Salary") + "</td></tr>");
}
if (!found) out.println("<tr><td colspan='3'>No records found!</td></tr>");
out.println("</table>");
out.println("</body></html>");
} catch (SQLException e) {
out.println("<p style='color:red;'>Database error: " + e.getMessage() + "</p>");
}
}
}
//web.xml
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" version="5.0">
<servlet>
<servlet-name>EmployeeServlet</servlet-name>
<servlet-class>com.example.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeServlet</servlet-name>
<url-pattern>/EmployeeServlet</url-pattern>
</servlet-mapping>
</web-app>
//Part (c): Student Attendance Portal Using JSP and Servlet
//Database Setup
CREATE TABLE Attendance (
StudentID INT,
Date DATE,
Status VARCHAR(10)
);
//attendance.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Student Attendance</title>
</head>
<body>
<h2>Mark Attendance</h2>
<form action="AttendanceServlet" method="post">
Student ID: <input type="text" name="studentId" required><br><br>
Date: <input type="date" name="date" required><br><br>
Status:
<select name="status">
<option value="Present">Present</option>
<option value="Absent">Absent</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
//AttendanceServlet.java
package com.example;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class AttendanceServlet extends HttpServlet {
private static final String URL = "jdbc:mysql://localhost:3306/testdb";
private static final String USER = "root";
private static final String PASSWORD = "your_password";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String studentId = request.getParameter("studentId");
String date = request.getParameter("date");
String status = request.getParameter("status");
try (Connection con = DriverManager.getConnection(URL, USER, PASSWORD)) {
String sql = "INSERT INTO Attendance (StudentID, Date, Status) VALUES (?, ?, ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(studentId));
ps.setDate(2, Date.valueOf(date));
ps.setString(3, status);
int rows = ps.executeUpdate();
if (rows > 0) {
RequestDispatcher rd = request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
} else {
response.getWriter().println("<h3>Failed to record attendance.</h3>");
}
} catch (SQLException e) {
response.getWriter().println("<h3>Database Error: " + e.getMessage() + "</h3>");
}
}
}
//success.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head><title>Success</title></head>
<body>
<h2>Attendance recorded successfully!</h2>
<a href="attendance.jsp">Mark another</a>
</body>
</html>