Example usage for javax.servlet.http HttpServletRequest getRequestDispatcher

List of usage examples for javax.servlet.http HttpServletRequest getRequestDispatcher

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest 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:org.openmrs.module.burdetteportal.web.controller.PortalRedirectController.java

private void respondUsingPOST(HttpServletRequest request, HttpServletResponse response, User user) {

    // get the url, username and password
    String url = Context.getAdministrationService().getGlobalProperty(BurdettePortalConstants.GP_URL);
    String username = Context.getAdministrationService().getGlobalProperty(BurdettePortalConstants.GP_USERNAME);
    String password = Context.getAdministrationService().getGlobalProperty(BurdettePortalConstants.GP_PASSWORD);

    // TODO send a real response with a helpful UI message
    if (url == null || username == null || password == null) {
        throw new APIException("URL, username or password not configured yet.");
    }/*w ww  . j  av  a2s .c  om*/

    // password should be encoded; decode it
    password = Security.decrypt(password);

    // session attributes may be useful later
    request.setAttribute("username", username);
    request.setAttribute("password", password);
    request.setAttribute("omrs_user", user.getUsername());

    RequestDispatcher rd = request.getRequestDispatcher(url);
    try {
        rd.forward(request, response);
    } catch (ServletException e) {
        throw new APIException("cannot redirect.", e);
    } catch (IOException e) {
        throw new APIException("cannot redirect.", e);
    }
}

From source file:com.parallax.server.blocklyprop.servlets.HelpServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String filePath = req.getParameter("f");

    if (filePath == null) {
        filePath = "index";
    }/*w  w w.java  2  s  . c o m*/

    File file = new File(destinationDirectoryFile, filePath);
    if (!filePath.contains(".")) {
        file = new File(destinationDirectoryFile, filePath + ".xml");

        String fileName = Files.getNameWithoutExtension(file.getName());
        String extension = Files.getFileExtension(file.getName());

        File localeFile = new File(destinationDirectoryFile, fileName + "_" + getLocale(req) + "." + extension);
        if (localeFile.exists() && localeFile.isFile()) {
            file = localeFile;
        }

        if (isSubDirectory(destinationDirectoryFile, file)) {
            if (file.exists() && file.isFile()) {
                req.setAttribute("html", Files.toString(file, Charset.forName("UTF-8")));
                req.getRequestDispatcher("/WEB-INF/servlet/help/help.jsp").forward(req, resp);
            } else {
                req.setAttribute("help-not-found", true);
                req.getRequestDispatcher("/WEB-INF/servlet/help/help-error.jsp").forward(req, resp);
                ;
            }
        } else {

        }
    } else {
        String fileName = Files.getNameWithoutExtension(file.getName());
        String extension = Files.getFileExtension(file.getName());

        File localeFile = new File(destinationDirectoryFile, fileName + "_" + getLocale(req) + "." + extension);
        if (localeFile.exists() && localeFile.isFile()) {
            file = localeFile;
        }

        if (isSubDirectory(destinationDirectoryFile, file)) {
            if (file.exists() && file.isFile()) {
                resp.setContentType(java.nio.file.Files.probeContentType(file.toPath()));
                Files.copy(file, resp.getOutputStream());
            } else {
                req.setAttribute("help-not-found", true);
                req.getRequestDispatcher("/WEB-INF/servlet/help/help-error.jsp").forward(req, resp);
                ;
            }
        } else {
            req.setAttribute("help-invalid-path", true);
            req.getRequestDispatcher("/WEB-INF/servlet/help/help-error.jsp").forward(req, resp);
        }
    }
}

From source file:org.xine.marketplace.frontend.views.security.JsfLoginUrlAuthenticationEntryPoint.java

/**
 * Performs the redirect (or forward) to the login form URL.
 *//*from  www  . j a v a2s  .  com*/
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response,
        final AuthenticationException authException) throws IOException, ServletException {

    String redirectUrl = null;

    if (this.useForward) {

        if (this.forceHttps && "http".equals(request.getScheme())) {
            // First redirect the current request to HTTPS.
            // When that request is received, the forward to the login page will be used.
            redirectUrl = buildHttpsRedirectUrlForRequest(request);
        }

        if (redirectUrl == null) {
            final String loginForm = determineUrlToUseForThisRequest(request, response, authException);

            if (logger.isDebugEnabled()) {
                logger.debug("Server side forward to: " + loginForm);
            }

            final RequestDispatcher dispatcher = request.getRequestDispatcher(loginForm);

            dispatcher.forward(request, response);

            return;
        }
    } else {
        // redirect to login page. Use https if forceHttps true

        redirectUrl = buildRedirectUrlToLoginPage(request, response, authException);

    }

    this.redirectStrategy.sendRedirect(request, response, redirectUrl);
}

From source file:com.afspq.web.ProcessQuery.java

@SuppressWarnings({ "unused", "rawtypes" })
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    if (isMultipart) {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {//w w w .j a v a  2 s.  com
            String root = getServletContext().getRealPath("/");
            File path = new File(root + "/uploads");
            List items = upload.parseRequest(request);

            if (!path.exists()) {
                boolean status = path.mkdirs();
            }

            if (items.size() > 0) {
                DiskFileItem file = (DiskFileItem) items.get(0);
                File uploadedFile = new File(path + "/" + file.getName());

                file.write(uploadedFile);
                request.setAttribute("results",
                        new ImageResults().getResults(uploadedFile, imageSearch, 0, true));
                request.getRequestDispatcher("imageResults.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:me.uni.sushilkumar.geodine.util.CuisineHandler.java

/**
 * Processes requests for both HTTP/*  w w w .j a  v  a2 s.  c  om*/
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @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, ParseException {
    //response.setContentType("text/html;charset=UTF-8");
    //PrintWriter out = response.getWriter();
    DBConnection con = null;
    String path = request.getRequestURI();
    int index = path.lastIndexOf("/");
    String cuisine = path.substring(index + 1);
    cuisine = cuisine.replace("-", " ");
    con = new DBConnection();
    CuisineObject obj = con.getCuisineData(cuisine);
    String name = obj.getName();
    String country = obj.getCountry();
    String ytid = obj.getYtid();
    ArrayList<String> directions = obj.getDirections();
    ArrayList<String> ingredients = obj.getIngredients();
    request.setAttribute("name", name);
    request.setAttribute("country", country);
    request.setAttribute("directions", directions);
    request.setAttribute("ingredients", ingredients);
    request.setAttribute("ytid", ytid);
    RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/recipe.jsp");
    rd.forward(request, response);

}

From source file:com.juicioenlinea.application.servlets.particular.DemandaServlet.java

private void update(HttpServletRequest request, HttpServletResponse response) {
    String descripcion = request.getParameter("descripcion");
    int idDemanda = Integer.parseInt(request.getParameter("idDemanda"));

    DemandaDAO dedao = new DemandaDAO();
    Demanda demanda = dedao.read(idDemanda);

    demanda.setDescripcion(descripcion);

    dedao.update(demanda);//  ww w .j  a va2s .c om

    try {
        request.setAttribute("messages", new Messages().getClientMessage("ok", 1));
        request.getRequestDispatcher("Demanda?action=form").forward(request, response);
    } catch (ServletException | IOException ex) {
        Logger.getLogger(DemandaServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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

/**
 * The doPost method of the servlet. <br>
 * /*  w w  w  . j ava2s.  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.lrodriguez.SVNBrowser.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setAttribute(ERROR, "this functionality is disabled");
    request.getRequestDispatcher(INDEX_JSP).forward(request, response);
    return;/* w w  w .  j a  va  2 s.  c  om*/
    //request.getRequestDispatcher("SVNClient").forward(request, response);
}

From source file:io.muic.ooc.webapp.servlet.UserServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String username = request.getParameter("username");

    User user = userService.getUser(username);

    if (user != null) {
        request.setAttribute("user", username);

        StringBuilder userDetails = new StringBuilder("<table class=\"table table-bordered table-striped\">"
                + "<thead class=\"thead-inverse\"><tr><th>UserInfo</th><th></th></thead>");
        userDetails.append("<tr><td>Username</td><td>" + user.getUserName() + "</td></tr>");
        //            userDetails.append("<tr><td>Password</td><td>"+user.getPassword()+"</td></tr>");
        userDetails.append("<tr><td>Email</td><td>" + user.getEmail() + "</td></tr>");
        userDetails.append("<tr><td>FirstName</td><td>" + user.getFirstName() + "</td></tr>");
        userDetails.append("<tr><td>LastName</td><td>" + user.getLastName() + "</td></tr>");
        userDetails.append("</tbody></table>");
        request.setAttribute("userDetails", userDetails);

        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/user.jsp");
        rd.include(request, response);/*from   w w w  .j  a  v  a2s. c  o  m*/
    } else {
        response.sendRedirect("/");
    }
}

From source file:com.netcracker.tss.web.servlet.customer.CustomerOrderTaxiEditDeleteServlet.java

private void redirectToEdit(HttpServletRequest req, HttpServletResponse resp, String error)
        throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    resp.setCharacterEncoding("UTF-8");
    DateFormat format = new SimpleDateFormat("HH:mm, dd MM yyyy", Locale.ENGLISH);
    UserBeanLocal userBeanLocal = getUserBean(req);
    req.setAttribute("personal_addr", userBeanLocal.toPersonalAddress(UserUtils.findCurrentUser()));
    TaxiOrder taxiOrder = getTaxiOrderBean(req).getOrderById(taxiOrderId);
    TaxiOrderHistory toh = getTaxiOrderBean(req).getOrderForEdit(taxiOrder);
    req.setAttribute("orderTime", format.format(toh.getOrderTime()));
    req.setAttribute("toh", toh);
    req.setAttribute("errorMessage", error);
    req.setAttribute("pageContent", "content/editTaxiOrder.jsp");
    req.setAttribute("pageType", "editpage");
    req.getRequestDispatcher("/WEB-INF/views/customer/customer-template.jsp").forward(req, resp);
}