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.PropertyGroupsServlet.java

/**
 * List property groups//  w  w w .  j  av  a  2  s .  c o m
 */
private void list(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, ParseException, RepositoryException, DatabaseException {
    log.debug("list({}, {})", new Object[] { request, response });
    ServletContext sc = getServletContext();
    FormUtils.resetPropertyGroupsForms();
    OKMPropertyGroup okmPropGroups = OKMPropertyGroup.getInstance();
    List<PropertyGroup> groups = okmPropGroups.getAllGroups(null);
    Map<PropertyGroup, List<Map<String, String>>> pGroups = new LinkedHashMap<PropertyGroup, List<Map<String, String>>>();

    for (PropertyGroup group : groups) {
        List<FormElement> mData = okmPropGroups.getPropertyGroupForm(null, group.getName());
        List<Map<String, String>> fMaps = new ArrayList<Map<String, String>>();

        for (FormElement fe : mData) {
            fMaps.add(FormUtils.toString(fe));
        }

        pGroups.put(group, fMaps);
    }

    sc.setAttribute("pGroups", pGroups);
    sc.getRequestDispatcher("/admin/property_groups_list.jsp").forward(request, response);

    // Activity log
    UserActivity.log(request.getRemoteUser(), "ADMIN_PROPERTY_GROUP_LIST", null, null, null);
    log.debug("list: void");
}

From source file:ForwardException.java

/** Called in response to a GET request (data encoded in the URL) */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ServletContext application = getServletContext();

    // BOILERPLATE beginning 
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    try {/* w  ww .  j  a  v  a 2s .  com*/
        // to do: logic code and main HTML goes here.

        // simulate an error condition happening at run time.
        if (this instanceof Servlet)
            throw new RuntimeException("Test exception");

        out.println("<!DOCTYPE html PUBLIC " + "\"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
                + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"\n" + ">");
        out.println("<html>");
        out.println("<head><title>Servlet Output</title></head>");
        out.println("<body>");

        // BOILERPLATE ending
        out.println("</body>");
        out.println("</html>");
        out.close();

    } catch (Exception exc) {

        // dispatch to JSP to display the error.
        RequestDispatcher rd = application.getRequestDispatcher("/oops.jsp");
        request.setAttribute("javax.servlet.jsp.jspException", exc);
        rd.forward(request, response);
    }
}

From source file:org.focusns.common.web.page.engine.widget.WidgetPageEngine.java

public void doAction(HttpServletRequest request, HttpServletResponse response) throws PageEngineException {
    //// w  w w .ja  va2s .c om
    try {
        //
        PageConfig pageConfig = (PageConfig) request.getSession().getAttribute("pageConfig");
        String widgetId = (String) request.getAttribute("widgetId");
        WidgetConfig widgetConfig = pageConfig.getWidgetConfigById(widgetId);
        //
        WidgetRequest widgetRequest = new WidgetRequest(request, widgetConfig, "action");
        WidgetResponse widgetResponse = new WidgetResponse(response);
        //
        String lookupPath = urlPathHelper.getLookupPathForRequest(request);
        String queryString = urlPathHelper.getOriginatingQueryString(request);
        String actionPath = "/widget" + lookupPath + "?" + queryString;
        ServletContext widgetContext = getServletContext();
        if (StringUtils.hasText(widgetConfig.getContext())) {
            widgetContext = getServletContext().getContext(widgetConfig.getContext());
            if (widgetContext == null) {
                return;
            }
        }
        //
        widgetRequest.setAttribute("requestType", "action");
        widgetRequest.setAttribute("widgetConfig", widgetConfig);
        //
        Navigator navigator = Navigator.reset();
        //
        for (WidgetActionInterceptor actionInterceptor : widgetActionInterceptors) {
            actionInterceptor.beforeAction(request, response);
        }
        widgetContext.getRequestDispatcher(actionPath).forward(widgetRequest, widgetResponse);
        //
        for (WidgetActionInterceptor actionInterceptor : widgetActionInterceptors) {
            actionInterceptor.afterAction(request, response);
        }
        //
        if (!StringUtils.hasText(navigator.getNavigateTo())) {
            widgetResponse.flushBuffer();
            return;
        }
        //
        String pathExpr = widgetConfig.getNavigationMap().get(navigator.getNavigateTo());
        if (StringUtils.hasText(pathExpr)) {
            Expression expression = expressionParser.parseExpression(pathExpr,
                    ParserContext.TEMPLATE_EXPRESSION);
            EvaluationContext evaluationContext = createEvaluationContext();
            String path = (String) expression.getValue(evaluationContext, request);
            //
            request.getSession().setAttribute("redirectAttributes", navigator.getRedirectAttributes());
            response.sendRedirect(request.getContextPath() + path);
        }
        //
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new PageEngineException(e.getMessage(), e);
    } finally {
        Navigator.reset();
    }
}

From source file:at.gv.egovernment.moa.id.proxy.servlet.ProxyServlet.java

/**
 * Handles an error. <br>//www .ja v a  2s.  c o  m
 * <ul>
 * <li>Logs the error</li>
 * <li>Places error message and exception thrown into the request 
 *        as request attributes (to be used by <code>"/errorpage-proxy.jsp"</code>)</li>
 * <li>Sets HTTP status 500 (internal server error)</li>
 * </ul>
 * 
 * @param errorMessage error message
 * @param exceptionThrown exception thrown
 * @param req servlet request
 * @param resp servlet response
 */
protected void handleError(String errorMessage, Throwable exceptionThrown, HttpServletRequest req,
        HttpServletResponse resp) {

    if (null != errorMessage) {
        Logger.error(errorMessage);
        req.setAttribute("ErrorMessage", errorMessage);
    }

    if (null != exceptionThrown) {
        if (null == errorMessage)
            errorMessage = exceptionThrown.getMessage();
        Logger.error(errorMessage, exceptionThrown);
        //req.setAttribute("ExceptionThrown", exceptionThrown);
    }

    if (Logger.isDebugEnabled()) {
        req.setAttribute("LogLevel", "debug");
    }

    //forward this to errorpage-proxy.jsp where the HTML error page is generated
    ServletContext context = getServletContext();
    RequestDispatcher dispatcher = context.getRequestDispatcher("/errorpage-proxy.jsp");
    try {
        dispatcher.forward(req, resp);
    } catch (ServletException e) {
        Logger.error(e);
    } catch (IOException e) {
        Logger.error(e);
    }

}

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

/**
 * Execute JDBC query//ww  w  . j a  va 2  s.c o  m
 */
private void executeJdbc(Session session, String qs, ServletContext sc, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, ServletException, IOException {
    WorkerJdbc worker = new WorkerJdbc();
    worker.setQueryString(qs);
    session.doWork(worker);

    sc.setAttribute("showSql", null);
    sc.setAttribute("exception", null);
    sc.setAttribute("globalResults", worker.getGlobalResults());
    sc.getRequestDispatcher("/admin/database_query.jsp").forward(request, response);
}

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

/**
 * Execute JDBC query/*from  ww w.jav  a  2 s.c  o  m*/
 */
private void executeJdbc(Session session, String qs, ServletContext sc, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, ServletException, IOException {
    WorkerJdbc worker = new WorkerJdbc();
    worker.setQueryString(qs);
    session.doWork(worker);

    //sc.setAttribute("sql", null);
    sc.setAttribute("exception", null);
    sc.setAttribute("globalResults", worker.getGlobalResults());
    sc.getRequestDispatcher("/admin/database_query.jsp").forward(request, response);
}

From source file:org.telscenter.sail.webapp.presentation.web.controllers.BridgeController.java

private ModelAndView handlePost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String type = request.getParameter("type");
    ServletContext servletContext2 = this.getServletContext();
    ServletContext vlewrappercontext = servletContext2.getContext("/vlewrapper");
    User user = ControllerUtil.getSignedInUser();
    CredentialManager.setRequestCredentials(request, user);

    if (type == null) {
        // post student data
        RequestDispatcher requestDispatcher = vlewrappercontext.getRequestDispatcher("/postdata.html");
        requestDispatcher.forward(request, response);
    } else if (type.equals("flag") || type.equals("annotation")) {
        RequestDispatcher requestDispatcher = vlewrappercontext.getRequestDispatcher("/annotations.html");
        requestDispatcher.forward(request, response);
    } else if (type.equals("journal")) {
        RequestDispatcher requestDispatcher = vlewrappercontext.getRequestDispatcher("/journaldata.html");
        requestDispatcher.forward(request, response);
    } else if (type.equals("peerreview")) {
        RequestDispatcher requestDispatcher = vlewrappercontext.getRequestDispatcher("/peerreview.html");
        requestDispatcher.forward(request, response);
    } else if (type.equals("ideaBasket")) {
        handleIdeaBasket(request, response);
    } else if (type.equals("studentAssetManager")) {
        handleStudentAssetManager(request, response);
    } else if (type.equals("viewStudentAssets")) {
        handleViewStudentAssets(request, response);
    }//  w ww  . j a  v a  2 s  . com
    return null;
}

From source file:org.focusns.common.web.page.engine.widget.WidgetPageEngine.java

public void doRender(HttpServletRequest request, HttpServletResponse response) throws PageEngineException {
    ////from ww w  .j a  v a  2s. c  om
    try {
        //
        if (request.getSession().getAttribute("redirectAttributes") != null) {
            Map<String, Object> redirectAttributes = (Map<String, Object>) request.getSession()
                    .getAttribute("redirectAttributes");
            for (String name : redirectAttributes.keySet()) {
                Object value = redirectAttributes.get(name);
                request.setAttribute(name, value);
            }
            //request.getSession().removeAttribute("redirectAttributes");
        }
        //
        Map<String, WidgetRequest> widgetRequestMap = new HashMap<String, WidgetRequest>();
        Map<String, WidgetResponse> widgetResponseMap = new HashMap<String, WidgetResponse>();
        //
        PageConfig pageConfig = (PageConfig) request.getAttribute("pageConfig");
        //
        for (PageRenderInterceptor pageRenderInterceptor : pageRenderInterceptors) {
            if (pageRenderInterceptor.beforeRender(request, response) == false) {
                return;
            }
        }
        //
        for (String position : pageConfig.getPositionConfigMap().keySet()) {
            PositionConfig positionConfig = pageConfig.getPositionConfigMap().get(position);
            for (WidgetConfig widgetConfig : positionConfig.getWidgetConfigMap().values()) {
                //
                WidgetRequest widgetRequest = new WidgetRequest(request, widgetConfig, "render");
                WidgetResponse widgetResponse = new WidgetResponse(response);
                //
                ServletContext widgetContext = getServletContext();
                if (StringUtils.hasText(widgetConfig.getContext())) {
                    widgetContext = getServletContext().getContext(widgetConfig.getContext());
                    if (widgetContext == null) {
                        continue;
                    }
                }
                //
                for (WidgetRenderInterceptor widgetRenderInterceptor : widgetRenderInterceptors) {
                    if (widgetRenderInterceptor.beforeRender(request, response) == false) {
                        return;
                    }
                }
                //
                String widgetPath = "/widget" + widgetConfig.getTarget();
                widgetContext.getRequestDispatcher(widgetPath).include(widgetRequest, widgetResponse);
                //
                for (WidgetRenderInterceptor widgetRenderInterceptor : widgetRenderInterceptors) {
                    widgetRenderInterceptor.afterRender(request, response);
                }
                //
                widgetRequestMap.put(widgetConfig.getId(), widgetRequest);
                widgetResponseMap.put(widgetConfig.getId(), widgetResponse);
            }
        }
        //
        request.setAttribute("widgetRequestMap", widgetRequestMap);
        request.setAttribute("widgetResponseMap", widgetResponseMap);
        //
        for (PageRenderInterceptor pageRenderInterceptor : pageRenderInterceptors) {
            pageRenderInterceptor.afterRender(request, response);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new PageEngineException(e.getMessage(), e);
    }
}

From source file:org.orbeon.oxf.servlet.ServletExternalContext.java

public RequestDispatcher getRequestDispatcher(String path, boolean isContextRelative) {

    final ServletContext servletContext = (ServletContext) webAppContext.getNativeContext();

    if (isContextRelative) {
        // Path is relative to the current context root
        final ServletContext slashServletContext = servletContext.getContext("/");
        return new ServletToExternalContextRequestDispatcherWrapper(servletContext.getRequestDispatcher(path),
                slashServletContext == servletContext);
    } else {/*  w  ww .ja  v a  2s.c o m*/
        // Path is relative to the server document root

        final ServletContext otherServletContext = servletContext.getContext(path);
        if (otherServletContext == null)
            return null;

        final ServletContext slashServletContext = servletContext.getContext("/");

        final String modifiedPath;
        final boolean isDefaultContext;
        if (slashServletContext != otherServletContext) {
            // Remove first path element
            modifiedPath = NetUtils.removeFirstPathElement(path);
            if (modifiedPath == null)
                return null;
            isDefaultContext = false;
        } else {
            // No need to remove first path element because the servlet context is ""
            modifiedPath = path;
            isDefaultContext = true;
        }

        return new ServletToExternalContextRequestDispatcherWrapper(
                otherServletContext.getRequestDispatcher(modifiedPath), isDefaultContext);
    }
}