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:eu.earthobservatory.org.StrabonEndpoint.DescribeBean.java

/**
 * Processes the request made from the HTML visual interface of Strabon Endpoint.
 * /* w w w.j  av a2s. co m*/
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
private void processVIEWRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // get the dispatcher for forwarding the rendering of the response
    RequestDispatcher dispatcher = request.getRequestDispatcher("describe.jsp");

    String query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
    String format = request.getParameter("format");
    String handle = request.getParameter("handle");
    RDFFormat rdfFormat = RDFFormat.valueOf(format);

    if (format == null || query == null || rdfFormat == null) {
        request.setAttribute(ERROR, PARAM_ERROR);
        dispatcher.forward(request, response);

    } else {
        // set the query, format and handle to be selected in the rendered page
        //request.setAttribute("query", URLDecoder.decode(query, "UTF-8"));
        //request.setAttribute("format", URLDecoder.decode(reqFormat, "UTF-8"));
        //request.setAttribute("handle", URLDecoder.decode(handle, "UTF-8"));

        if ("download".equals(handle)) { // download as attachment
            ServletOutputStream out = response.getOutputStream();

            response.setContentType(rdfFormat.getDefaultMIMEType());
            response.setHeader("Content-Disposition", "attachment; filename=results."
                    + rdfFormat.getDefaultFileExtension() + "; " + rdfFormat.getCharset());

            try {
                strabonWrapper.describe(query, format, out);
                response.setStatus(HttpServletResponse.SC_OK);

            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                out.print(ResponseMessages.getXMLHeader());
                out.print(ResponseMessages.getXMLException(e.getMessage()));
                out.print(ResponseMessages.getXMLFooter());
            }

            out.flush();

        } else //plain
        {
            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                strabonWrapper.describe(query, format, bos);

                request.setAttribute(RESPONSE, StringEscapeUtils.escapeHtml(bos.toString()));

            } catch (Exception e) {
                request.setAttribute(ERROR, e.getMessage());
            }
            dispatcher.forward(request, response);
        }
    }
}

From source file:com.jigsforjava.web.controller.MultiActionController.java

/**
 * Handle the case where no request handler method was found.
 * <p>//from w w  w  .jav  a 2  s  .c o m
 * The default implementation logs a warning and sends an HTTP 404 error.
 * Alternatively, a fallback view could be chosen, or the
 * NoSuchRequestHandlingMethodException could be rethrown as-is.
 * 
 * @param ex the NoSuchRequestHandlingMethodException to be handled
 * @param request current HTTP request
 * @param response current HTTP response
 * @throws Exception an Exception that should be thrown as result of the servlet
 * request.
 */
private void handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
    ex.printStackTrace();
    request.getRequestDispatcher(PAGE_NOT_FOUND).forward(request, response);
}

From source file:org.cnbi.web.interceptors.LicenseInterceptor.java

/**
 * controller?//from   w  w  w  .j a  v  a  2  s .c o m
 */
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object)
        throws Exception {
    MsgBean errorMsg = (MsgBean) ContextHolderUtils.getServletContext()
            .getAttribute(Constants.LICENSE_CHECK_INFO);
    boolean flag = false;
    if (errorMsg.getFlag() == false) {
        flag = true;
    } else {
        request.setAttribute("errorMsg", errorMsg.getText());
        request.getRequestDispatcher("/noLicense.jsp").forward(request, response);
    }
    return flag;
}

From source file:com.bdnc.ecommercebdnc.command.PerfilProduto.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {

    try {/*w ww  . j  a v  a  2 s  . co m*/

        LojaService lojaService = new LojaService();
        Produto produto = lojaService.buscarProduto(Long.parseLong(request.getParameter("idProduto")));
        request.setAttribute("produto", produto);
        request.setAttribute("produtosSugeridos", lojaService.buscarProdutosSugeridos(produto));

        RequestDispatcher dispather = request.getRequestDispatcher("produto.jsp");
        dispather.forward(request, response);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:de.knurt.ajajajava.control.ChangeLocation.java

/**
 * Returns the path to the file (included) of the actual LocationPart.
 * @param config a org.ajajajava.model.Config instance as defined in the application context
 * @param location The actual Location//  ww  w.  j  a  va 2s.c o  m
 * @param locationPart The part of the actual location requested here
 * @see org.ajajajava.model.Location
 * @return the path to the file (included) of the actual LocationPart.
 */
private RequestDispatcher getDispatcher(HttpServletRequest request, Config config, Location location,
        LocationPart locationPart) {
    String path = config.getPath2pages() + separator + location.getLocationName() + Config.LOCATION_SEPARATOR
            + locationPart.getPartName() + "." + locationPart.getSuffix();
    RequestDispatcher result = request.getRequestDispatcher(path);

    if (result == null) { // no content set for location and location part
        result = request.getRequestDispatcher(EMPTY_LOCATION_CONTENT_FILE);
    }
    return result;
}

From source file:controllers.IndexServlet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String action = request.getParameter("type");

    if (action == null) {
        request.getRequestDispatcher("index.jsp").forward(request, response);
        return;//from ww w .j ava 2s.c o m
    }

    DocumentManager.Init(request, response);
    PrintWriter writer = response.getWriter();

    switch (action.toLowerCase()) {
    case "upload":
        Upload(request, response, writer);
        break;
    case "convert":
        Convert(request, response, writer);
        break;
    case "track":
        Track(request, response, writer);
        break;
    }

}

From source file:eionet.cr.web.util.StripesExceptionHandler.java

public void handle(Throwable t, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Throwable newThrowable = (t instanceof ServletException) ? getRootCause((ServletException) t) : t;
    if (newThrowable == null)
        newThrowable = t;//  www  .  j a va  2  s . c o  m

    logger.error(newThrowable.getMessage(), newThrowable);
    request.setAttribute(EXCEPTION_ATTR, newThrowable);
    request.getRequestDispatcher(ERROR_PAGE).forward(request, response);
}

From source file:controllers.ServerController.java

public void logout(HttpServletRequest request, HttpServletResponse response)
        throws IOException, InterruptedException, ServletException {
    HttpSession session = request.getSession(false);
    session.invalidate();//from   w ww . j  av a  2 s. co m
    RequestDispatcher rd = request.getRequestDispatcher("index.html");
    rd.forward(request, response);
}

From source file:Service.java

/**
 * When the servlet receives a GET request.
 *//*from w  ww.j  a  v  a2s. c  o m*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //It always returns the service.jps page.
    RequestDispatcher view = request.getRequestDispatcher(SERVICE_JSP);
    view.forward(request, response);
}

From source file:com.look.ChangeAccountServlet.java

/**
 * Processes post request for changing user account data
 * @param request//from   www . j  a v a 2 s  . c om
 * @param response
 * @throws IOException
 * @throws ServletException 
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String newFirstName = request.getParameter("firstName");
    String newLastName = request.getParameter("lastName");

    if (!StringUtils.isAlpha(newFirstName)) {
        request.setAttribute("message", "First name cannot contain numbers or special characters");
        request.getRequestDispatcher("/account.jsp").forward(request, response);
    } else if (!StringUtils.isAlpha(newLastName)) {
        request.setAttribute("message", "Last name cannot contain numbers or special characters");
        request.getRequestDispatcher("/account.jsp").forward(request, response);
    }

    String username = request.getSession().getAttribute("user").toString();
    String oldFirstName = DatabaseUserUtils.getFirstNameFromUsername(username);
    String oldLastName = DatabaseUserUtils.getLastNameFromUsername(username);

    if (newFirstName.equals(oldFirstName) || newLastName.equals(oldLastName)) {
        response.sendRedirect("account.jsp");
    }

    //change the name
    Connection conn;
    try {
        conn = LookDatabaseUtils.getNewConnection();
        PreparedStatement updateStatement = conn
                .prepareStatement("UPDATE users " + "SET first_name=?, " + "last_name=? " + "WHERE username=?");
        updateStatement.setString(1, newFirstName);
        updateStatement.setString(2, newLastName);
        updateStatement.setString(3, username);
        updateStatement.executeUpdate();
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(ChangeAccountServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
}