Package com.tubeonfire.controller.admin

Source Code of com.tubeonfire.controller.admin.EditTubeServlet

package com.tubeonfire.controller.admin;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tubeonfire.entity.Tube;
import com.tubeonfire.model.admin.TubeModel;
import com.tubeonfire.util.TubeValidate;

@SuppressWarnings("serial")
public class EditTubeServlet extends HttpServlet {

  private static final Logger log = Logger.getLogger(EditTubeServlet.class
      .getName());

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    try {
      HttpSession session = req.getSession();
      String id = req.getParameter("id");
      System.out.println(id);
      Tube obj = TubeModel.getById(id, true);
      if (obj != null) {
        session.setAttribute("action", "edit");
        session.setAttribute("obj", obj);
        req.getRequestDispatcher("/admin/form_tube.jsp").forward(req,
            resp);
      } else {
        session.setAttribute("error",
            "Tube's not exists or has been deleted !");
        resp.sendRedirect("/admin/tube/list");
      }
    } catch (Exception e) {
      log.warning(e.toString());
      e.printStackTrace();
      resp.sendError(4004,
          "We are sorry for the inconvenience ! Please try again later !");
    }
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    try {
      HttpSession session = req.getSession();
      String id = req.getParameter("id");
      Tube obj = TubeModel.getById(id, true);
      if (obj != null) {
        if (TubeValidate.checkForm(req, resp, session, "edit", obj)) {
          TubeModel.update(obj);
          session.setAttribute("success", "Action success !");
          resp.sendRedirect("/admin/tube/edit?id=" + id);
        } else {
          return;
        }

      } else {
        session.setAttribute("error",
            "Tube's not exists or has been deleted !");
        resp.sendRedirect("/admin/tube/list");
      }
    } catch (Exception e) {
      log.warning(e.toString());
      e.printStackTrace();
      resp.sendError(4004,
          "We are sorry for the inconvenience ! Please try again later !");
    }
  }
}
TOP

Related Classes of com.tubeonfire.controller.admin.EditTubeServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.