Example usage for javax.servlet ServletContext getRequestDispatcher

List of usage examples for javax.servlet ServletContext getRequestDispatcher

Introduction

In this page you can find the example usage for javax.servlet ServletContext getRequestDispatcher.

Prototype

public RequestDispatcher getRequestDispatcher(String path);

Source Link

Document

Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

Usage

From source file:com.openkm.servlet.admin.LogCatServlet.java

/**
 * View log/*from  w w  w. j  av a  2s. c o  m*/
 */
private void view(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    log.debug("view({}, {})", request, response);
    int begin = WebUtils.getInt(request, "begin");
    int end = WebUtils.getInt(request, "end");
    String str = WebUtils.getString(request, "str");
    String file = WebUtils.getString(request, "file");
    ServletContext sc = getServletContext();
    File lf = new File(logFolder, file);
    sc.setAttribute("file", file);
    sc.setAttribute("begin", begin);
    sc.setAttribute("end", end);
    sc.setAttribute("str", str);
    sc.setAttribute("messages", FormatUtil.parseLog(lf, begin, end, str));
    sc.getRequestDispatcher("/admin/logcat_view.jsp").forward(request, response);

    // Activity log
    UserActivity.log(request.getRemoteUser(), "ADMIN_LOGCAT_VIEW", file, null, str);

    log.debug("view: void");
}

From source file:com.emc.plants.web.servlets.AccountServlet.java

/**
 * Request dispatch./*  w ww  . j  a  v a2 s .  c o  m*/
 */
private void requestDispatch(ServletContext ctx, HttpServletRequest req, HttpServletResponse resp, String page)
        throws ServletException, IOException {
    resp.setContentType("text/html");
    ctx.getRequestDispatcher("/" + page).include(req, resp);
}

From source file:com.ikon.servlet.admin.CronTabServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    log.debug("doGet({}, {})", request, response);
    request.setCharacterEncoding("UTF-8");
    String action = WebUtils.getString(request, "action");
    updateSessionManager(request);//  www .ja  v  a 2  s .c o m

    try {
        Map<String, String> types = new LinkedHashMap<String, String>();
        types.put(CronTab.BSH, "BSH");
        types.put(CronTab.JAR, "JAR");

        if (action.equals("create")) {
            ServletContext sc = getServletContext();
            CronTab ct = new CronTab();
            sc.setAttribute("action", action);
            sc.setAttribute("types", types);
            sc.setAttribute("ct", ct);
            sc.getRequestDispatcher("/admin/crontab_edit.jsp").forward(request, response);
        } else if (action.equals("edit")) {
            ServletContext sc = getServletContext();
            int ctId = WebUtils.getInt(request, "ct_id");
            CronTab ct = CronTabDAO.findByPk(ctId);
            sc.setAttribute("action", action);
            sc.setAttribute("types", types);
            sc.setAttribute("ct", ct);
            sc.getRequestDispatcher("/admin/crontab_edit.jsp").forward(request, response);
        } else if (action.equals("delete")) {
            ServletContext sc = getServletContext();
            int ctId = WebUtils.getInt(request, "ct_id");
            CronTab ct = CronTabDAO.findByPk(ctId);
            sc.setAttribute("action", action);
            sc.setAttribute("types", types);
            sc.setAttribute("ct", ct);
            sc.getRequestDispatcher("/admin/crontab_edit.jsp").forward(request, response);
        } else if (action.equals("execute")) {
            execute(request, response);
            list(request, response);
        } else {
            list(request, response);
        }
    } catch (DatabaseException e) {
        log.error(e.getMessage(), e);
        sendErrorRedirect(request, response, e);
    } catch (EvalError e) {
        log.error(e.getMessage(), e);
        sendErrorRedirect(request, response, e);
    }
}

From source file:com.ikon.servlet.admin.CssServlet.java

/**
 * Delete CSS//  w  ww .  j  a v a  2 s .  co  m
 */
private void delete(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("delete({}, {}, {})", new Object[] { userId, request, response });
    ServletContext sc = getServletContext();
    long id = WebUtils.getLong(request, "css_id");

    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("persist", true);
    sc.setAttribute("css", CssDAO.getInstance().findByPk(id));
    sc.getRequestDispatcher("/admin/css_edit.jsp").forward(request, response);

    log.debug("edit: void");
}

From source file:com.ikon.servlet.admin.CssServlet.java

/**
 * Edit CSS//  w  w  w  .j a va2s. com
 */
private void edit(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("edit({}, {}, {})", new Object[] { userId, request, response });
    ServletContext sc = getServletContext();
    long id = WebUtils.getLong(request, "css_id");

    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("persist", true);
    sc.setAttribute("css", CssDAO.getInstance().findByPk(id));
    sc.getRequestDispatcher("/admin/css_edit.jsp").forward(request, response);

    log.debug("edit: void");
}

From source file:Servlet.Citizen_SubmitReport.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w  w  w . j a  v  a  2s  .  c  o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    HttpSession session = request.getSession();
    ActivityDAO actdao = new ActivityDAO();
    try {
        CitizenDAO citizenDAO = new CitizenDAO();
        OCPDDAO oc = new OCPDDAO();
        InputStream inputStream = null;
        Citizen c = (Citizen) session.getAttribute("user");

        String category = null;

        ArrayList<String> files = new ArrayList<String>();

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);

            try {
                //ArrayList<String> 

                List items = upload.parseRequest(request);
                Iterator iterator = items.iterator();

                String message = null;

                File path = null;
                File uploadedFiles = null;
                String fileName = null;
                Citizen user = (Citizen) session.getAttribute("user");
                String projectId = null;
                String projectName = null;

                Project proj = null;

                while (iterator.hasNext()) {
                    FileItem item = (FileItem) iterator.next();

                    if (item.isFormField()) {

                        //Returns the string inside the field
                        String value = item.getString();
                        //returns the name of the field
                        String value2 = item.getFieldName();

                        if (value2.equals("reportdescription")) {
                            message = value;
                        }
                        if (value2.equals("projectID")) {
                            projectId = value;
                            proj = oc.getBasicProjectDetails(projectId);
                        }
                        if (value2.equals("projectName")) {
                            projectName = value;
                        }

                    }

                    if (!item.isFormField()) {
                        fileName = item.getName();
                        if (!fileName.isEmpty()) {
                            String root = getServletContext().getRealPath("/");

                            //path where the file will be stored
                            path = new File("C:\\Users\\AdrianKyle\\Documents\\NetBeansProjects\\Cogito\\Upload"
                                    + "/" + projectName + " - " + user.getFirstName() + user.getLastName()
                                    + "'s report");
                            if (!path.exists()) {
                                boolean status = path.mkdirs();
                            }

                            uploadedFiles = new File(path + "/" + fileName);
                            item.write(uploadedFiles);
                            files.add(fileName);
                        } else {

                        }
                    }
                }

                for (int x = 0; x < files.size(); x++) {

                    String filename = files.get(x);
                    if (!filename.isEmpty()) {
                        String[] parts = filename.split(Pattern.quote("."));
                        String extension = parts[1];
                        //Videos
                        if (extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi")
                                || extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("flv")
                                || extension.equalsIgnoreCase("wmv")) {
                            category = "video";
                            break;
                        } //Images
                        else if (extension.equalsIgnoreCase("png") || extension.equalsIgnoreCase("jpeg")
                                || extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("bmp")) {
                            category = "image";
                            break;
                        }
                    }
                }

                //citizenDAO.uploadReport(r);
                //Testimonial t = new Testimonial(0, title, null, description, "chrome-extension://pediieahejagkkidddjjbiaocanpcjik/Citizen/" + title, location, locationdetails, category, null,"Pending", user);
                //Testimonial t = new Testimonial(0, title, null, description, "chrome-extension://hjllakiciieioomhnnoiljeofbacabpc/" + title, location, locationdetails, category, null, user);
                //Upload testimonial
                //citizenDAO.uploadTestimonial(t);

                Citizen_Report report = new Citizen_Report(0, message,
                        "chrome-extension://affgigbpilagplecbaihihkcaddmlkji/" + projectName + " - "
                                + user.getFirstName() + user.getLastName() + "'s report",
                        null, user, proj);

                citizenDAO.uploadCitizenReport(report);
                int id = citizenDAO.getRecentPostid(user, message);
                report.setId(id);

                //Add activity
                actdao.addActivity(new Activity(0, "you have submitted a report", null, user.getUser()));

                //Place in appropriate tables
                for (int x = 0; x < files.size(); x++) {

                    String filename = files.get(x);
                    if (!filename.isEmpty()) {

                        String[] parts = filename.split(Pattern.quote("."));
                        String extension = parts[1];
                        //Videos
                        if (extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi")
                                || extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("flv")
                                || extension.equalsIgnoreCase("wmv") || extension.equalsIgnoreCase("mkv")) {
                            Report_File rf = new Report_File();
                            rf.setFilename(files.get(x));
                            rf.setType("Video");

                            rf.setCitizenReport(report);
                            rf.setUploader(c.getFirstName());
                            rf.setDescription("Submitted video");
                            //Get the project
                            rf.setProject(proj);
                            //rf.setStatus("Approved");

                            //upload report files
                            citizenDAO.uploadFiles(rf);

                        } //Images
                        else if (extension.equalsIgnoreCase("png") || extension.equalsIgnoreCase("jpeg")
                                || extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("bmp")) {
                            Report_File rf = new Report_File();
                            rf.setFilename(files.get(x));
                            rf.setType("Image");
                            rf.setCitizenReport(report);
                            rf.setUploader(c.getFirstName());
                            rf.setDescription("Submitted image");
                            //Get the project
                            rf.setProject(proj);
                            //rf.setStatus("Approved");

                            //upload report files
                            citizenDAO.uploadFiles(rf);
                        }

                    }
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception ex) {
                Logger.getLogger(Citizen_SubmitReport.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        request.setAttribute("success", category + "Success");
        ServletContext context = getServletContext();
        RequestDispatcher dispatch = context.getRequestDispatcher("/Citizen_Home");
        dispatch.forward(request, response);

    } finally {

        out.close();

    }

}

From source file:com.openkm.servlet.admin.CronTabServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    log.debug("doGet({}, {})", request, response);
    request.setCharacterEncoding("UTF-8");
    String action = WebUtils.getString(request, "action");
    updateSessionManager(request);// ww w  . j  av a2 s  .com

    try {
        Map<String, String> types = new LinkedHashMap<String, String>();
        types.put(MimeTypeConfig.MIME_BSH, "BSH");
        types.put(MimeTypeConfig.MIME_JAR, "JAR");

        if (action.equals("create")) {
            ServletContext sc = getServletContext();
            CronTab ct = new CronTab();
            sc.setAttribute("action", action);
            sc.setAttribute("types", types);
            sc.setAttribute("ct", ct);
            sc.getRequestDispatcher("/admin/crontab_edit.jsp").forward(request, response);
        } else if (action.equals("edit")) {
            ServletContext sc = getServletContext();
            int ctId = WebUtils.getInt(request, "ct_id");
            CronTab ct = CronTabDAO.findByPk(ctId);
            sc.setAttribute("action", action);
            sc.setAttribute("types", types);
            sc.setAttribute("ct", ct);
            sc.getRequestDispatcher("/admin/crontab_edit.jsp").forward(request, response);
        } else if (action.equals("delete")) {
            ServletContext sc = getServletContext();
            int ctId = WebUtils.getInt(request, "ct_id");
            CronTab ct = CronTabDAO.findByPk(ctId);
            sc.setAttribute("action", action);
            sc.setAttribute("types", types);
            sc.setAttribute("ct", ct);
            sc.getRequestDispatcher("/admin/crontab_edit.jsp").forward(request, response);
        } else if (action.equals("execute")) {
            execute(request, response);
            list(request, response);
        } else if (action.equals("download")) {
            download(request, response);
        } else {
            list(request, response);
        }
    } catch (DatabaseException e) {
        log.error(e.getMessage(), e);
        sendErrorRedirect(request, response, e);
    } catch (EvalError e) {
        log.error(e.getMessage(), e);
        sendErrorRedirect(request, response, e);
    }
}

From source file:at.gv.egovernment.moa.id.auth.servlet.AuthServlet.java

/**
 * Handles a <code>WrongParametersException</code>.
 * //from w  w w .  j  av a  2 s .  co m
 * @param req
 *            servlet request
 * @param resp
 *            servlet response
 */
protected void handleWrongParameters(WrongParametersException ex, HttpServletRequest req,
        HttpServletResponse resp) {
    Logger.error(ex.toString());
    req.setAttribute("WrongParameters", ex.getMessage());

    // forward this to errorpage-auth.jsp where the HTML error page is
    // generated
    ServletContext context = getServletContext();
    RequestDispatcher dispatcher = context.getRequestDispatcher("/errorpage-auth.jsp");
    try {
        resp.setHeader(MOAIDAuthConstants.HEADER_EXPIRES, MOAIDAuthConstants.HEADER_VALUE_EXPIRES);
        resp.setHeader(MOAIDAuthConstants.HEADER_PRAGMA, MOAIDAuthConstants.HEADER_VALUE_PRAGMA);
        resp.setHeader(MOAIDAuthConstants.HEADER_CACHE_CONTROL, MOAIDAuthConstants.HEADER_VALUE_CACHE_CONTROL);
        resp.addHeader(MOAIDAuthConstants.HEADER_CACHE_CONTROL,
                MOAIDAuthConstants.HEADER_VALUE_CACHE_CONTROL_IE);

        dispatcher.forward(req, resp);
    } catch (ServletException e) {
        Logger.error(e);
    } catch (IOException e) {
        Logger.error(e);
    }
}

From source file:com.ikon.servlet.admin.MimeTypeServlet.java

/**
 * Create mime type//  w w  w  .j  ava 2  s  .c o m
 */
private void create(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("create({}, {}, {})", new Object[] { userId, request, response });
    ServletContext sc = getServletContext();
    MimeType mt = new MimeType();
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("extensions", null);
    sc.setAttribute("mt", mt);
    sc.getRequestDispatcher("/admin/mime_edit.jsp").forward(request, response);
    log.debug("create: void");
}

From source file:com.cyclopsgroup.waterview.jsp.JspEngine.java

/**
 * @param path Absolute JSP path//from   w  ww .ja va2 s. c o  m
 * @param data Runtime data
 * @param viewContext View context
 * @throws Exception Throw it out
 */
public void renderJsp(String path, RuntimeData data, Context viewContext) throws Exception {
    HttpServletRequest request = (HttpServletRequest) data.getRequestContext().get("request");
    HttpServletResponse response = (HttpServletResponse) data.getRequestContext().get("response");
    ServletContext servletContext = (ServletContext) data.getRequestContext().get("servletContext");
    if (request == null || response == null || servletContext == null) {
        data.getOutput().println("Jsp " + path + " is not rendered correctly with request " + request
                + ", response " + response + ", context " + servletContext);
    }
    request.setAttribute("viewContext", viewContext);
    RequestDispatcher dispatcher = servletContext.getRequestDispatcher(path);
    File jspFile = new File(servletContext.getRealPath(path));
    if (dispatcher == null || !jspFile.isFile()) {
        data.getOutput().println("Jsp " + path + " doesn't exist");
    } else {
        dispatcher.include(request, response);
        response.getWriter().flush();
    }
    request.removeAttribute("viewContext");
}