package com.example.servlet; import java.io.IOException; import java.sql.SQLException; import com.alibaba.fastjson.JSONObject; import com.example.bean.EntrepriseProfileBean; import com.example.bean.StudentProfileBean; import com.example.dao.EntrepriseProfileDAO; import com.example.dao.StudentProfileDAO; import com.example.utility.HttpGetJson; 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("/profile") public class ProfileServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html"); HttpSession session = request.getSession(); if (request.getParameter("authorization") != null) { // 管理页面用 String account = request.getParameter("account"); try { StudentProfileDAO studentProfileDAO = new StudentProfileDAO(); StudentProfileBean studentProfileBean = new StudentProfileBean(); studentProfileBean = studentProfileDAO.getProfile(account); if (studentProfileBean != null) { session.setAttribute("studentProfileBean", studentProfileBean); session.setAttribute("isStudentProfileExisted", "true"); } else { session.setAttribute("studentProfileBean", null); session.setAttribute("objectAccount", account); session.setAttribute("isStudentProfileExisted", "notExist"); } } catch (SQLException e) { e.printStackTrace(); } request.getRequestDispatcher("WEB-INF/jsp/studentProfile.jsp").forward(request, response); } else if (request.getParameter("companyID") != null) { String companyID = request.getParameter("companyID"); try { EntrepriseProfileDAO entrepriseProfileDAO = new EntrepriseProfileDAO(); EntrepriseProfileBean entrepriseProfileBean = new EntrepriseProfileBean(); entrepriseProfileBean = entrepriseProfileDAO.getProfile(companyID); if (entrepriseProfileBean != null) { session.setAttribute("entrepriseProfileBean", entrepriseProfileBean); session.setAttribute("isEntrepriseProfileExisted", "true"); } else { session.setAttribute("entrepriseProfileBean", null); session.setAttribute("isEntrepriseProfileExisted", "notExist"); } } catch (SQLException e) { e.printStackTrace(); } request.getRequestDispatcher("WEB-INF/jsp/entrepriseProfile.jsp").forward(request, response); } else { // 登录后检测 if (session.getAttribute("accountAuthorization") != null) { if (session.getAttribute("accountAuthorization").toString().equals("student")) { StudentProfileDAO studentProfileDAO = new StudentProfileDAO(); try { StudentProfileBean studentProfileBean = new StudentProfileBean(); studentProfileBean = studentProfileDAO.getProfile(session.getAttribute("account").toString()); if (studentProfileBean != null) { session.setAttribute("studentProfileBean", studentProfileBean); session.setAttribute("isStudentProfileExisted", "true"); } } catch (SQLException e) { e.printStackTrace(); } request.getRequestDispatcher("WEB-INF/jsp/studentProfile.jsp").forward(request, response); } else if (session.getAttribute("accountAuthorization").toString().equals("entreprise")) { EntrepriseProfileDAO entrepriseProfileDAO = new EntrepriseProfileDAO(); try { EntrepriseProfileBean entrepriseProfileBean = new EntrepriseProfileBean(); entrepriseProfileBean = entrepriseProfileDAO .getProfile(session.getAttribute("account").toString()); if (entrepriseProfileBean != null) { session.setAttribute("entrepriseProfileBean", entrepriseProfileBean); session.setAttribute("isEntrepriseProfileExisted", "true"); session.setAttribute("companyName", entrepriseProfileBean.getName()); session.setAttribute("contactEmail", entrepriseProfileBean.getContactEmail()); } } catch (SQLException e) { e.printStackTrace(); } request.getRequestDispatcher("WEB-INF/jsp/entrepriseProfile.jsp").forward(request, response); } } else request.getRequestDispatcher("WEB-INF/jsp/studentProfile.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=utf-8"); HttpSession session = request.getSession(); JSONObject status = new JSONObject(); if (session.getAttribute("accountAuthorization").toString().equals("student")) { StudentProfileDAO studentProfileDAO = new StudentProfileDAO(); JSONObject studentProfileJson = HttpGetJson.getJson(request); try { if (studentProfileJson.getString("method").toString().equals("create")) { studentProfileDAO.create(studentProfileJson.getString("name"), Integer.parseInt(studentProfileJson.getString("age")), studentProfileJson.getString("location"), studentProfileJson.getString("interest"), session.getAttribute("account").toString()); status.put("status", "创建成功"); } if (studentProfileJson.getString("method").toString().equals("update")) { studentProfileDAO.update(studentProfileJson.getString("name"), Integer.parseInt(studentProfileJson.getString("age")), studentProfileJson.getString("location"), studentProfileJson.getString("interest"), session.getAttribute("account").toString()); status.put("status", "更新成功"); } } catch (SQLException e) { status.put("status", "失败"); e.printStackTrace(); } } if (session.getAttribute("accountAuthorization").equals("entreprise")) { EntrepriseProfileDAO entrepriseProfileDAO = new EntrepriseProfileDAO(); JSONObject entrepriseProfileJson = HttpGetJson.getJson(request); try { if (entrepriseProfileJson.getString("method").toString().equals("create")) { entrepriseProfileDAO.create(entrepriseProfileJson.getString("name"), entrepriseProfileJson.getString("overview"), session.getAttribute("account").toString(), entrepriseProfileJson.getString("contactEmail")); status.put("status", "创建成功"); } if (entrepriseProfileJson.getString("method").toString().equals("update")) { entrepriseProfileDAO.update(entrepriseProfileJson.getString("name"), entrepriseProfileJson.getString("overview"), session.getAttribute("account").toString(), entrepriseProfileJson.getString("contactEmail")); status.put("status", "更新成功"); } } catch (SQLException e) { status.put("status", "失败"); e.printStackTrace(); } } response.getWriter().write(String.valueOf(status)); } }