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:DispatchServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    // Forward to a display page
    String display = "/servlet/SearchView";
    RequestDispatcher dispatcher = req.getRequestDispatcher(display);
    dispatcher.forward(req, res);
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    ServletContext context = getServletContext();
    RequestDispatcher dispatcher = context.getRequestDispatcher("/myServlet");
    dispatcher.forward(req, res);
}

From source file:Main.java

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

    RequestDispatcher dispatcher = request.getRequestDispatcher("/p.jsp");

    dispatcher.forward(request, response);
}

From source file:dk.netarkivet.common.webinterface.HTMLUtils.java

/**
 * Forward to our standard error message page with an internationalized message. Note that this <em>doesn't</em>
 * throw ForwardedToErrorPage, it is the job of whoever calls this to do that if not within a JSP page (a JSP page
 * can just return immediately). All text involved will be HTML-escaped.
 *
 * @param context The context that the error happened in (the JSP-defined pageContext, typically)
 * @param I18N The i18n information/*  w w  w  .j  av a2 s . c o  m*/
 * @param label An i18n label for the error. This label should begin with "errormsg;".
 * @param args Any extra args for i18n
 * @throws IOFailure If the forward fails
 */
public static void forwardWithErrorMessage(PageContext context, I18n I18N, String label, Object... args) {
    // Note that we may not want to be to strict here
    // as otherwise information could be lost.
    ArgumentNotValid.checkNotNull(context, "context");
    ArgumentNotValid.checkNotNull(I18N, "I18N");
    ArgumentNotValid.checkNotNull(label, "label");
    ArgumentNotValid.checkNotNull(args, "args");

    String msg = HTMLUtils.escapeHtmlValues(I18N.getString(context.getResponse().getLocale(), label, args));
    context.getRequest().setAttribute("message", msg);
    RequestDispatcher rd = context.getServletContext().getRequestDispatcher("/message.jsp");
    final String errormsg = "Failed to forward on error " + msg;
    try {
        rd.forward(context.getRequest(), context.getResponse());
    } catch (IOException e) {
        log.warn(errormsg, e);
        throw new IOFailure(errormsg, e);
    } catch (ServletException e) {
        log.warn(errormsg, e);
        throw new IOFailure(errormsg, e);
    }
}

From source file:dk.netarkivet.common.webinterface.HTMLUtils.java

/**
 * Forward to our standard error message page with an internationalized message, in case of exception. Note that
 * this <em>doesn't</em> throw ForwardedToErrorPage, it is the job of whoever calls this to do that if not within a
 * JSP page (a JSP page can just return immediately). All text involved will be HTML-escaped.
 *
 * @param context The context that the error happened in (the JSP-defined pageContext, typically)
 * @param i18n The i18n information//  w w w  .  j a  v a  2  s.co  m
 * @param e The exception that is being handled.
 * @param label An i18n label for the error. This label should begin with "errormsg;".
 * @param args Any extra args for i18n
 * @throws IOFailure If the forward fails
 */
public static void forwardWithErrorMessage(PageContext context, I18n i18n, Throwable e, String label,
        Object... args) {
    // Note that we may not want to be to strict here
    // as otherwise information could be lost.
    ArgumentNotValid.checkNotNull(context, "context");
    ArgumentNotValid.checkNotNull(I18N, "I18N");
    ArgumentNotValid.checkNotNull(label, "label");
    ArgumentNotValid.checkNotNull(args, "args");

    String msg = HTMLUtils.escapeHtmlValues(i18n.getString(context.getResponse().getLocale(), label, args));
    context.getRequest().setAttribute("message", msg + "\n" + e.getLocalizedMessage());
    RequestDispatcher rd = context.getServletContext().getRequestDispatcher("/message.jsp");
    final String errormsg = "Failed to forward on error " + msg;
    try {
        rd.forward(context.getRequest(), context.getResponse());
    } catch (IOException e1) {
        log.warn(errormsg, e1);
        throw new IOFailure(errormsg, e1);
    } catch (ServletException e1) {
        log.warn(errormsg, e1);
        throw new IOFailure(errormsg, e1);
    }
}

From source file:QueryModifier.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    String requestUrl = request.getRequestURL().toString();
    String querystr = request.getQueryString();
    if (querystr != null) {
        querystr = querystr + "&inspector-name=Jen&inspector-email=Jenniferq@yahoo.com";
    } else {/*from   w  w w  . j a v a  2s  .c om*/
        querystr = "inspector-name=Jen&inspector-email=Jenniferq@yahoo.com";
    }

    RequestDispatcher dispatcher = request.getRequestDispatcher("/viewPost.jsp?" + querystr);
    dispatcher.forward(request, response);
}

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

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

    try {//from  ww w.  jav  a 2 s  .  com

        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:org.apache.struts.chain.servlet.PerformInclude.java

/**
 * <p>Perform the appropriate processing on the specified
 * include uri.</p>/*from   w  w w  .  j av a2 s  .c om*/
 *
 * @param context The context for this request
 * @param uri The uri to be included
 */
protected void perform(Context context, String uri) throws Exception {

    ServletWebContext swcontext = (ServletWebContext) context;

    // Get the underlying request in the case of a multipart wrapper
    HttpServletRequest request = swcontext.getRequest();
    if (request instanceof MultipartRequestWrapper) {
        request = ((MultipartRequestWrapper) request).getRequest();
    }

    RequestDispatcher rd = swcontext.getContext().getRequestDispatcher(uri);
    rd.forward(request, swcontext.getResponse());
}

From source file:com.gisgraphy.webapp.filter.MockFilterChain.java

public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    String uri = ((HttpServletRequest) request).getRequestURI();
    String requestContext = ((HttpServletRequest) request).getContextPath();

    if (StringUtils.isNotEmpty(requestContext) && uri.startsWith(requestContext)) {
        uri = uri.substring(requestContext.length());
    }/* w  ww  . j  a va 2  s  .  c  o  m*/

    this.forwardURL = uri;
    log.debug("Forwarding to: " + uri);

    RequestDispatcher dispatcher = request.getRequestDispatcher(uri);
    dispatcher.forward(request, response);
}

From source file:Controladores.AutorizadorAcesso.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object controller)
        throws Exception {

    String uri = request.getRequestURI();

    if (uri.toUpperCase().contains("RESTRITO")) {
        if (request.getSession().getAttribute("logado") == null
                || (boolean) request.getSession().getAttribute("logado") == false) {

            if (request.getQueryString() != null) {
                uri += "?" + request.getQueryString();
            }//from w w  w. j a  v a 2 s. c  o m

            request.getSession().setAttribute("endereco", uri);

            RequestDispatcher dispacher = request.getRequestDispatcher("exibe-login");
            dispacher.forward(request, response);
            return false;
        }
    }
    return true;
}