Untitled
unknown
plain_text
2 years ago
12 kB
7
Indexable
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
*/
package Controllers;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import DAL.DAO;
import Models.*;
import java.util.Map;
/**
*
* @author admin
*/
public class LoadDB extends HttpServlet {
DAO d;
public void init() {
d = new DAO();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet LoadDB</title>");
out.println("<link rel = 'Stylesheet' href = 'css/mycss.css'>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet LoadDB at " + request.getContextPath() + "</h1>");
d.loadStudent();
d.loadDepart();
String s = "<table>";
s += "<tr>";
s += "<th>ID</th>";
s += "<th>Name</th>";
s += "<th>Gender</th>";
s += "<th>Department Name</th>";
s += "<th>Age</th>";
s += "<th>GPA</th>";
s += "<th>Add</th>";
s += "<th>Update</th>";
s += "<th>Delete</th>";
s += "</tr>";
String idUpdate = "";
if (request.getAttribute("Update") != null) {
idUpdate = request.getAttribute("Update") + "";
}
Student stUpdate = null;
for (Student st : d.getStd()) {
if (idUpdate != "" && st.getId().equals(idUpdate)) {
stUpdate = st;
}
s += "<tr>";
s += "<td>" + st.getId() + "</td>";
s += "<td>" + st.getName() + "</td>";
s += "<td>" + (st.isGender() ? "Male" : "Female") + "</td>";
// s += "<td><input type = 'checkbox'" + (st.isGender()?"checked":"")+ "</td>"; // dung check box
s += "<td>" + d.getDepart().get(st.getDepartId()).getName() + "</td>";
s += "<td>" + st.getAge() + "</td>";
s += "<td>" + st.getGpa() + "</td>";
s += "<td>" + st.getAdd() + "</td>";
s += "<td><a href = 'LoadDB?type=1&id=" + st.getId() + "'>Update</a></td>";
s += "<td><a href = 'LoadDB?type=0&id=" + st.getId() + "'>Delete</a></td>";
s += "</tr>";
}
s += "</table>";
out.println(s);
out.print("<hr/>");
out.println("<h1> Insert student </h1>");
String id = stUpdate == null ? "" : stUpdate.getId();
String name = stUpdate == null ? "" : stUpdate.getName();
boolean gender = stUpdate == null ? true : stUpdate.isGender();
String departId = stUpdate == null ? "SE" : stUpdate.getDepartId();
int age = stUpdate == null ? 20 : stUpdate.getAge();
String gpa = stUpdate == null ? "" : stUpdate.getGpa() + "";
String Add = stUpdate == null ? "" : stUpdate.getAdd();
String form = "";
form += "<form action=\"LoadDB\" method='post'>\n"
+ " <table>\n"
+ " <tr>\n"
+ " <td>ID</td>\n"
+ " <td><input type='text' name ='id' value = '" + id + "'></td>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Name</td>\n"
+ " <td><input type='text' name ='name' value = '" + name + "'></td>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Gender</td>\n"
+ " <td><input type='radio' name ='gender' value = 'm' checked>Male <input type='radio' name ='gender' value = 'f'>Female</td>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Department</td>\n"
+ " <td><select name = 'departId'>";
for (Map.Entry<String, Department> en : d.getDepart().entrySet()) {
String key = en.getKey();
Department value = en.getValue();
form += "<option value = " + key + " " + (key.equals(departId) ? "selected" : "") + ">" + value.getName() + "</option>";
}
form += "</select></td>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Age</td>\n"
+ " <td><input type='number' name ='age' value = '" + age + "' min = 0 max = 150></td>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>GPA</td>\n"
+ " <td><input type='text' name ='gpa' value = '" + gpa + "'></td>\n"
+ " </tr>\n"
+ " <tr>\n"
+ " <td>Address</td>\n"
+ " <td><textarea name = 'add' rows='10' cols = '20'>" + Add + "</textarea></td>\n"
+ " </tr>\n"
+ " </table>\n"
+ " <input type='submit' value='Insert' name ='btnInsert'> \n"
+ "<input type='submit' value='Update' name ='btnUpdate'> \n"
+ "\n"
+ " </form>";
out.print(form);
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object o1 = request.getParameter("type");
Object o2 = request.getParameter("id");
if (o1 != null) {
if ((o1 + "").equals("1")) {
//update
if (o2 != null) {
request.setAttribute("Update", o2 + "");
} else {
request.removeAttribute("Update");
}
} else if ((o1 + "").equals("0")) {
//delete
if (o2 != null) {
d.del(o2 + "");
}
} else {
}
}
d.loadDepart();
d.loadStudent();
int index = -1;
int nrpp = 1;
Object obj = request.getParameter("index");
if (obj != null) {
try {
index = Integer.parseInt(obj + "");
nrpp = Integer.parseInt(request.getAttribute("nrpp") + "");
} catch (Exception e) {
}
}
PagingModels page = new PagingModels(d.getStd().size(), nrpp, index);
page.Calc();
request.setAttribute("nrpp", nrppArr);
request.setAttribute("page", page);
request.setAttribute("stu", d.getStd());
request.setAttribute("dept", d.getDepart());
request.getRequestDispatcher("View/ViewDB.jsp").forward(request, response);
}
int[] nrppArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object obj = request.getParameter("Home");
boolean btn = true;
int nrpp = Integer.parseInt(request.getParameter("nrpp"));
int index = Integer.parseInt(request.getParameter("index"));
int totalPage = Integer.parseInt(request.getParameter("totalPage"));
if (obj != null) {//button Home clicked
index = 0;
btn = false;
} else if (request.getParameter("End") != null) {//click end
index = totalPage - 1;
btn = false;
} else if (request.getParameter("Prev") != null) {//click prev
index -= 1;
btn = false;
} else if (request.getParameter("Next") != null) {//click next
index += 1;
btn = false;
} else if (btn) {
for (int i = 0; i < totalPage; i++) {
if (request.getParameter("btn" + i) != null) {
index = i;
btn = false;
break;
}
}
}
request.setAttribute("index", index);
request.setAttribute("nrpp", nrpp);
if (btn) {
String id = request.getParameter("id");
String name = request.getParameter("name");
boolean gender = request.getParameter("gender").equals("m");
String departId = request.getParameter("departId");
int age = Integer.parseInt(request.getParameter("age"));
double gpa = Double.parseDouble(request.getParameter("gpa"));
String add = request.getParameter("add");
boolean checkAdd = true;
for (Student st : d.getStd()) {
if (st.getId().equals(id)) {
checkAdd = false;
}
}
Object o1 = request.getParameter("btnInsert");
Object o2 = request.getParameter("btnUpdate");
if (o1 != null) {
if (checkAdd) {
d.insert(id, name, gender, departId, age, gpa, add);
} else {
d.update(id, name, gender, departId, age, gpa, add);
}
}
if(o2 != null){
if(checkAdd){
d.insert(id, name, gender, departId, age, gpa, add);
}else{
d.update(id, name, gender, departId, age, gpa, add);
}
}
doGet(request, response);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}Editor is loading...