Example #1
0
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");

    HttpSession session = request.getSession();
    String subject = request.getParameter("subject");
    String course = new String(request.getParameter("course").getBytes("iso-8859-1"), "utf-8");
    course = "(" + course + ")";
    Studentinfo studentinfo = (Studentinfo) session.getAttribute("user");
    subject = course + studentinfo.getName() + ":" + subject;

    String content = request.getParameter("content");
    String emailAddr = "\n\n\n 发信者邮箱地址:" + studentinfo.getEmail();
    content = content + emailAddr;

    String t_id = request.getParameter("to");
    TeacherinfoDAO teacherinfoDAO = new TeacherinfoDAO();
    Teacherinfo teacherinfo = teacherinfoDAO.findById(t_id);
    String to = teacherinfo.getEmail();

    SendMailServlet sendMailServlet = new SendMailServlet();
    if (!sendMailServlet.sendMail(to, subject, content)) {
      request.getRequestDispatcher("EmailFailure.html").forward(request, response);
    } else {
      request.getRequestDispatcher("EmailSuccess.html").forward(request, response);
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    System.out.println("EditPerson!!!!!!!!!!!!!!!!!");

    String Way = request.getParameter("act");
    System.out.println(Way);
    String ID = request.getParameter("id");
    String Name = request.getParameter("name");
    String Sex = request.getParameter("sex");
    String Birthday = request.getParameter("birthday");
    String Nation = request.getParameter("nation");
    // String Mail = request.getParameter("mail");
    String Address = request.getParameter("address");
    String Phone = request.getParameter("phone");

    //		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //		String datetime = formatter.format(new java.util.Date());

    StudentinfoDAO MyStudentDAO = new StudentinfoDAO();
    Studentinfo MyStudentInfo;

    Session s = (Session) HibernateSessionFactory.getSession();
    if (Way.equalsIgnoreCase("edit")) {
      System.out.println("编辑!");
      MyStudentInfo = MyStudentDAO.findById(ID);
      MyStudentInfo.setName(Name);
      MyStudentInfo.setSex(Sex);
      MyStudentInfo.setNation(Nation);
      MyStudentInfo.setEmail("");
      MyStudentInfo.setAddress(Address);

      /*	String TempString[]=Birthday.split("年");
      int Year=Integer.parseInt(TempString[0]);
      System.out.println(Year);

      String TempString1[]=TempString[1].split("月");
      int Month=Integer.parseInt(TempString1[0]);
      System.out.println(Month);
      String TempString2[]=TempString1[1].split("日");
      int Day=Integer.parseInt(TempString2[0]);

      System.out.println(Day);

      */
      DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");
      Date d = null;
      Date MyDate = null;

      try {
        d = formater.parse(Birthday);
      } catch (ParseException e) {

      } finally {
        MyDate = d;
      }

      // System.out.println(MyDate.getYear());
      MyStudentInfo.setBirthday(MyDate);
      MyStudentInfo.setPhoneNumber(Phone);
      System.out.println(Name);
      System.out.println(Sex);

      MyStudentDAO.save(MyStudentInfo);
    } else if (Way.equalsIgnoreCase("add")) {
      System.out.println("添加!");
      MyStudentInfo = new Studentinfo();
      MyStudentInfo.setSId(ID);
      MyStudentInfo.setPassword("1234");
      MyStudentInfo.setPinCode("1234");
      MyStudentInfo.setName(Name);
      MyStudentInfo.setSex(Sex);
      MyStudentInfo.setNation(Nation);
      MyStudentInfo.setEmail("");
      MyStudentInfo.setAddress(Address);

      DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");
      Date d = null;
      Date MyDate = null;

      try {
        d = formater.parse(Birthday);
      } catch (ParseException e) {

      } finally {
        MyDate = d;
      }

      MyStudentInfo.setBirthday(MyDate);
      MyStudentInfo.setPhoneNumber(Phone);
      System.out.println(Name);
      System.out.println(Sex);

      MyStudentDAO.save(MyStudentInfo);

      ServletContext sc = request.getSession().getServletContext();
      String filePath = "";

      filePath = sc.getRealPath("/") + "file/" + ID + "/";

      File file = new File(filePath);
      file.mkdirs();

    } else if (Way.equalsIgnoreCase("delete")) {
      MyStudentInfo = MyStudentDAO.findById(ID);
      StudentCourseDAO MyStudentCourseDAO = new StudentCourseDAO();

      List<StudentCourse> StudentCourseList = MyStudentCourseDAO.findBySId(ID);
      for (int i = 0; i < StudentCourseList.size(); i++) {
        StudentCourse temp = (StudentCourse) StudentCourseList.get(i);
        MyStudentCourseDAO.delete(temp);
      }

      MyStudentDAO.delete(MyStudentInfo);
    }

    try {
      Transaction ts = s.beginTransaction();
      ts.commit();
    } catch (Exception e) {

    } finally {
      s.close();
      List<Studentinfo> StudentList = MyStudentDAO.findAll();

      request.setAttribute("pageNumber", 1);
      request.setAttribute("Jump", 2);
      HttpSession hs = request.getSession();

      if (!Way.equalsIgnoreCase("delete")) {
        hs.setAttribute("StudentID", ID);
        MyStudentInfo = MyStudentDAO.findById(ID);
        hs.setAttribute("StudentName", MyStudentInfo.getName());
        hs.setAttribute("Sex", MyStudentInfo.getSex());

        Date TempDate = MyStudentInfo.getBirthday();
        String TempString;
        DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");
        TempString = formater.format(TempDate);
        hs.setAttribute("StudentBirthday", TempString);
        hs.setAttribute("StudentNation", MyStudentInfo.getNation());
        hs.setAttribute("StudentAddress", MyStudentInfo.getAddress());
        hs.setAttribute("StudentEmail", MyStudentInfo.getEmail());
        hs.setAttribute("StudentPhone", MyStudentInfo.getPhoneNumber());
      }

      hs.setAttribute("MyStudentList", StudentList);

      request.getRequestDispatcher("admin.jsp").forward(request, response);
    }
  }