Example usage for javax.servlet RequestDispatcher forward

List of usage examples for javax.servlet RequestDispatcher forward

Introduction

In this page you can find the example usage for javax.servlet RequestDispatcher forward.

Prototype

public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException;

Source Link

Document

Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.

Usage

From source file:edu.lternet.pasta.portal.JournalCitationDeleteServlet.java

/**
 * The doPost method of the servlet. <br>
 * //  w  w  w .j a  v a  2 s .  c o  m
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request
 *          the request send by the client to the server
 * @param response
 *          the response send by the server to the client
 * @throws ServletException
 *           if an error occurred
 * @throws IOException
 *           if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession httpSession = request.getSession();

    String uid = (String) httpSession.getAttribute("uid");

    if (uid == null || uid.isEmpty())
        uid = "public";

    String journalCitationId = request.getParameter("journalcitationid");

    String deleteMessage = null;
    String messageType = null;

    if (uid.equals("public")) {
        messageType = "warning";
        request.setAttribute("message", LOGIN_WARNING);
    } else {

        try {
            JournalCitationsClient journalCitationsClient = new JournalCitationsClient(uid);
            journalCitationsClient.deleteByJournalCitationId(journalCitationId);
            deleteMessage = String.format(
                    "Journal citation entry with identifier '<b>%s</b>' has been deleted.", journalCitationId);
            messageType = "info";
        } catch (Exception e) {
            handleDataPortalError(logger, e);
        }

    }

    request.setAttribute("deleteMessage", deleteMessage);
    request.setAttribute("messageType", messageType);

    RequestDispatcher requestDispatcher = request.getRequestDispatcher(forward);
    requestDispatcher.forward(request, response);

}

From source file:edu.lternet.pasta.portal.EventReviewServlet.java

/**
 * The doPost method of the servlet. <br>
 * //from  w w w.j a va  2  s .c  o m
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request
 *          the request send by the client to the server
 * @param response
 *          the response send by the server to the client
 * @throws ServletException
 *           if an error occurred
 * @throws IOException
 *           if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession httpSession = request.getSession();
    String xml = null;
    String filter = "";
    String uid = (String) httpSession.getAttribute("uid");
    if (uid == null || uid.isEmpty())
        uid = "public";
    String subscriptionId = request.getParameter("subscriptionid");
    String message = null;
    String type = null;

    if (uid.equals("public")) {
        message = LOGIN_WARNING;
        type = "warning";
    } else {
        try {
            EventSubscriptionClient eventClient = new EventSubscriptionClient(uid);

            if (subscriptionId.isEmpty()) {
                xml = eventClient.readByFilter(filter);
            } else {
                xml = eventClient.readBySid(subscriptionId);
            }

            SubscriptionUtility subscriptionUtility = new SubscriptionUtility(xml);
            message = subscriptionUtility.xmlToHtml(cwd + xslpath);
            type = "info";
        } catch (Exception e) {
            handleDataPortalError(logger, e);
        }
    }

    request.setAttribute("reviewmessage", message);
    request.setAttribute("type", type);
    RequestDispatcher requestDispatcher = request.getRequestDispatcher(forward);
    requestDispatcher.forward(request, response);
}

From source file:com.itesm.test.servlets.TasksServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String[] description = request.getParameterValues("description");
    String[] priority = request.getParameterValues("priority");
    String[] task_time = request.getParameterValues("task_time");
    String[] end_time = request.getParameterValues("end_time");
    PersonaVO personavo = (PersonaVO) request.getSession().getAttribute("persona");
    TaskManager taskManager = new TaskManager();
    for (int i = 0; i < priority.length; i++) {
        TaskVO wh = new TaskVO();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        SimpleDateFormat sdfTimeStamp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        Date durationDate = null;
        Date end_date = null;//from  w ww . j  ava 2s  . c o m
        try {
            durationDate = sdf.parse(task_time[i]);
            end_date = sdfTimeStamp.parse(end_time[i]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        wh.setDuration(new Time(durationDate.getTime()));
        wh.setEnd_date(new Timestamp(end_date.getTime()));
        wh.setDescription(description[i]);
        wh.setPriority(Integer.parseInt(priority[i]));
        wh.setAgenda_id(personavo.getAgenda_id());
        System.out.println(wh.toString());
        taskManager.agregar(wh);
    }
    CreateSchedule createSchedule = new CreateSchedule(personavo);
    createSchedule.createSchedule();
    TaskDAO taskDAO = new TaskDAO();
    List<TaskVO> task_list = taskDAO.findByAgenda(personavo.getAgenda_id());
    ListIterator listIterator = task_list.listIterator();
    while (listIterator.hasNext()) {
        TaskVO task = (TaskVO) listIterator.next();
        if (task.getWork_hours_id() == null) {
            listIterator.remove();
        }
    }

    WorkHoursManager workHoursManager = new WorkHoursManager();
    List<WorkHoursVO> worksHours_list = workHoursManager.consultarPorAgenda(personavo.getAgenda_id());
    request.setAttribute("tasks", task_list);
    request.setAttribute("works", worksHours_list);
    RequestDispatcher rd = getServletContext().getRequestDispatcher("/schedule.jsp");
    rd.forward(request, response);

}

From source file:com.gigglinggnus.controllers.ManageExamsController.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");

    Clock clk = (Clock) (request.getSession().getAttribute("clock"));

    List<Term> terms = Term.getFutureTerms(em, Instant.now(clk));
    List<Exam> exams = terms.stream().flatMap(term -> term.getExams().stream())
            .filter(exam -> exam.getStatus() == ExamStatus.PENDING).collect(Collectors.toList());

    Map<Exam, ArrayList<String>> utilMap = new HashMap();
    for (Exam e : exams) {
        Interval<Instant> examInt = e.getInterval();

        LocalDate testDate = examInt.getStart().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = examInt.getEnd().atZone(ZoneId.systemDefault()).toLocalDate();

        Term t = e.getTerm();//w ww  . j ava2s. c  om
        Map<LocalDate, Long> examUtilMap = new HashMap();
        ArrayList<String> examList = new ArrayList();
        while (testDate.isBefore(endDate.plusDays(1))) {

            examList.add(testDate.toString() + "=" + t.utilizationForDay(testDate, clk) + "   ");

            testDate = testDate.plusDays(1);
        }
        utilMap.put(e, examList);

    }

    request.setAttribute("exams", exams);
    request.setAttribute("utilList", utilMap);
    RequestDispatcher rd = request.getRequestDispatcher("/admin/manage-exams.jsp");
    rd.forward(request, response);
}

From source file:edu.lternet.pasta.portal.EventDeleteServlet.java

/**
 * The doPost method of the servlet. <br>
 * /*from w ww. java 2s .co m*/
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request
 *          the request send by the client to the server
 * @param response
 *          the response send by the server to the client
 * @throws ServletException
 *           if an error occurred
 * @throws IOException
 *           if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession httpSession = request.getSession();

    String uid = (String) httpSession.getAttribute("uid");

    if (uid == null || uid.isEmpty())
        uid = "public";

    String subscriptionId = request.getParameter("subscriptionid");

    String message = null;
    String type = null;

    if (uid.equals("public")) {

        message = LOGIN_WARNING;
        type = "warning";

    } else {

        try {

            EventSubscriptionClient eventClient = new EventSubscriptionClient(uid);
            eventClient.deleteBySid(subscriptionId);
            message = "Event subscription with identifier '<b>" + subscriptionId + "</b>' has been deleted.";
            type = "info";

        } catch (Exception e) {
            handleDataPortalError(logger, e);
        }

    }

    request.setAttribute("deletemessage", message);
    request.setAttribute("type", type);

    RequestDispatcher requestDispatcher = request.getRequestDispatcher(forward);
    requestDispatcher.forward(request, response);

}

From source file:edu.lternet.pasta.portal.ProvenanceGeneratorServlet.java

/**
 * The doPost method of the servlet. <br>
 * //  ww w . jav a2 s.c o m
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request
 *          the request send by the client to the server
 * @param response
 *          the response send by the server to the client
 * @throws ServletException
 *           if an error occurred
 * @throws IOException
 *           if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        HttpSession httpSession = request.getSession();
        String uid = (String) httpSession.getAttribute("uid");
        if (uid == null || uid.isEmpty())
            uid = "public";
        String packageId = request.getParameter("packageid");

        if (packageId != null) {
            ProvenanceFactoryClient provenanceFactoryClient = new ProvenanceFactoryClient(uid);
            String provenanceXML = provenanceFactoryClient.getProvenanceByPid(packageId);
            String provenanceHTML = transformToHTML(provenanceXML);
            String encodedProvenanceXML = XmlUtility.xmlEncode(provenanceXML);
            request.setAttribute("provenanceHTML", provenanceHTML);
            request.setAttribute("provenanceXML", encodedProvenanceXML);
            request.setAttribute("packageid", packageId);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher(forward);
            requestDispatcher.forward(request, response);
        } else {
            throw new UserErrorException("Package identifier is null.");
        }

    } catch (Exception e) {
        handleDataPortalError(logger, e);
    }
}

From source file:com.gigglinggnus.controllers.ModifyAppointmentController.java

/**
 *
 * @param request servlet request/*from  w  ww .j av  a  2  s .co  m*/
 * @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 {
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");
    Clock clk = (Clock) (request.getSession().getAttribute("clock"));

    boolean modified = false;
    String userId = request.getParameter("userid");
    User user = em.find(User.class, userId);
    String examId = request.getParameter("examid");
    Exam exam = em.find(Exam.class, examId);
    Appointment appt = user.getAppointmentByExam(exam);
    String startTime = request.getParameter("startTime");
    String seatZone = request.getParameter("seatingZone");
    String seatNum = request.getParameter("seatNum");
    String cancel = request.getParameter("cancel");
    try {
        em.getTransaction().begin();
        if (startTime != "") {
            appt.changeStartTime(Instant.parse(startTime), clk);
            em.persist(appt);
            modified = true;
        }
        if (seatZone != "") {
            appt.setSeatingZone(Seating.parse(seatZone));
            em.persist(appt);
            modified = true;
        }
        if (seatNum != "") {
            appt.setSeatNum(Integer.parseInt(seatNum));
            em.persist(appt);
            modified = true;
        }
        em.getTransaction().commit();
    } catch (Exception e) {
        em.getTransaction().rollback();
        request.setAttribute("msg", e);
        RequestDispatcher rd = request.getRequestDispatcher("/home.jsp");
        rd.forward(request, response);
    }
    if (cancel != null) {

        request.setAttribute("msg", "Appointment cancelled");
        RequestDispatcher rd = request.getRequestDispatcher("/home.jsp");
        rd.forward(request, response);
    }
    if (modified == true) {
        request.setAttribute("msg", "Appointment modified");
        RequestDispatcher rd = request.getRequestDispatcher("/home.jsp");
        rd.forward(request, response);
    } else {
        request.setAttribute("msg", "Appointment unchanged");
        RequestDispatcher rd = request.getRequestDispatcher("/home.jsp");
        rd.forward(request, response);
    }
}

From source file:com.google.step2.example.consumer.servlet.LoginServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    RequestDispatcher d = req.getRequestDispatcher(TEMPLATE_FILE);
    d.forward(req, resp);
}

From source file:Controller.ControllerImageCustomer.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//  w ww .j  a  va2  s  .c  om
 * @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 {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (!isMultipart) {

    } else {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = null;
        try {
            items = upload.parseRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Iterator iter = items.iterator();
        Hashtable params = new Hashtable();
        String fileName = null;
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            if (item.isFormField()) {
                params.put(item.getFieldName(), item.getString());
            } else {

                try {
                    String itemName = item.getName();
                    fileName = itemName.substring(itemName.lastIndexOf("\\") + 1);
                    System.out.println("path" + fileName);
                    String RealPath = getServletContext().getRealPath("/") + "upload\\" + fileName;
                    System.out.println("Rpath" + RealPath);
                    File savedFile = new File(RealPath);
                    item.write(savedFile);
                    String username = (String) params.get("txtusername");
                    String password = (String) params.get("txpassword");
                    String hoten = (String) params.get("txthoten");
                    String gioitinh = (String) params.get("txtgioitinh");
                    String email = (String) params.get("txtemail");
                    String role = "false";
                    String Register = (String) params.get("Register");
                    String url = "CustomerDao.jsp";
                    if (Register.equals("Register")) {
                        url = "index.jsp";
                    }
                    Customer cus = new Customer(username, password, hoten, gioitinh, email, role,
                            "upload\\" + fileName);
                    CustomerDAO.ThemKhachHang(cus);
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }

}

From source file:Controller.ControllerImageCustomerIndex.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*  w w  w  . ja va2s .c  om*/
 * @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 {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (!isMultipart) {

    } else {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = null;
        try {
            items = upload.parseRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Iterator iter = items.iterator();
        Hashtable params = new Hashtable();
        String fileName = null;
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            if (item.isFormField()) {
                params.put(item.getFieldName(), item.getString());
            } else {

                try {
                    String itemName = item.getName();
                    fileName = itemName.substring(itemName.lastIndexOf("\\") + 1);
                    System.out.println("path" + fileName);
                    String RealPath = getServletContext().getRealPath("/") + "upload\\" + fileName;
                    System.out.println("Rpath" + RealPath);
                    File savedFile = new File(RealPath);
                    item.write(savedFile);
                    String username = (String) params.get("txtusername");
                    String password = (String) params.get("txpassword");
                    String hoten = (String) params.get("txthoten");
                    String gioitinh = (String) params.get("txtgioitinh");
                    String email = (String) params.get("txtemail");
                    String role = "false";
                    Customer cus = new Customer(username, password, hoten, gioitinh, email, role,
                            "upload\\" + fileName);

                    CustomerDAO.ThemKhachHang(cus);
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                    rd.forward(request, response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }

}