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:cn.itcast.bbs.controller.BbsServlet.java

private void addTopic(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//from ww  w . j  av a  2  s  . co m
        int id = Integer.parseInt(request.getParameter("id"));
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        User user = (User) request.getSession().getAttribute("user");
        Topic t = new Topic();
        t.setTitle(title);
        t.setName(user.getUsername());
        t.setContent(content);
        t.setReplyCnt(0);
        service.addTopic(t, id);
        response.sendRedirect(request.getContextPath() + "/BbsServlet?method=showAllTopic&typeId=" + id);
    } catch (Exception e) {
        e.printStackTrace();
        request.setAttribute("message", "?");
        request.getRequestDispatcher("/WEB-INF/bbs/message.jsp").forward(request, response);
    }
}

From source file:com.github.mike10004.stormpathacctmgr.NewPasswordFormServlet.java

/**
 * Handles the HTTP {@code GET} method. Gets the password reset token 
 * parameter value from the request query string and forwards to 
 * a JSP page for rendering./* w  w w .jav  a  2 s  .  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
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, ResourceException {
    boolean tokenVerified = false;
    String token = request.getParameter(PasswordReset.PARAM_RESET_TOKEN);
    if (token != null && !token.isEmpty()) {
        checkNotNullAndNotEmpty(token, "token");
        Stormpaths stormpaths = createStormpaths();
        Application application = stormpaths.buildApplication();
        Account account;
        try {
            account = application.verifyPasswordResetToken(token);
            tokenVerified = true;
            log.log(Level.INFO, "verified token {0} and got account {1}",
                    new Object[] { StringUtils.abbreviate(token, 32), account.getEmail() });
            request.setAttribute(ATTR_TARGET_EMAIL, account.getEmail());
        } catch (ResourceException e) {
            log.log(Level.INFO, "verifyPasswordResetToken failed: {0}", (Object) e);
        }
    } else {
        log.info("'sptoken' query parameter is empty or not present");
    }
    request.setAttribute(ATTR_TOKEN_VERIFIED, tokenVerified);
    RequestDispatcher dispatcher = request.getRequestDispatcher(RESET_ENTER_NEW_PASSWORD_JSP_PATH);
    dispatcher.forward(request, response);
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ??JSP_PATH?//from w  ww  .ja v a2 s.c o  m
 * ?
 *
 * @throws ServletException
 * @throws IOException
 */
private static void forward() throws RuntimeException {
    try {
        HttpServletRequest request = ActionContext.getReq();
        HttpServletResponse response = ActionContext.getResp();
        if (response.isCommitted()) {
            return;
        }
        String jspPath = null;
        String jspPathParam = request.getParameter(JSP_PATH);
        if (StringUtils.isEmpty(jspPathParam)) {
            Object jspPathObjAttr = request.getAttribute(JSP_PATH);
            if (null == jspPathObjAttr) {
                Object jspPathObjSess = request.getSession().getAttribute(JSP_PATH);
                if (null != jspPathObjSess) {
                    jspPath = (String) jspPathObjSess;
                }
            } else {
                jspPath = (String) jspPathObjAttr;
            }
        } else {
            jspPath = jspPathParam;
        }
        if (null == jspPath) {
            throw new ServletException(
                    "????jspPath?");
        }
        request.getRequestDispatcher(jspPath).forward(request, response);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.deegree.test.gui.StressTestController.java

@SuppressWarnings("unchecked")
private void doStep3(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    threads = Integer.valueOf(request.getParameter("threadNo"));
    requests = Integer.valueOf(request.getParameter("requestNo"));
    String imgornot = request.getParameter("imgornot");
    boolean showImage = false;
    if (imgornot != null && imgornot.equals("displayimg"))
        showImage = true;/*from  w ww .jav  a2  s .c om*/

    // process the request asynchronously
    String capab = (String) request.getSession().getAttribute("capab");
    Map<String, String> paramsSet = (HashMap<String, String>) request.getSession().getAttribute("paramsSet");

    Thread t = new Thread(new Processing(request.getSession(), threads, requests, capab, paramsSet, showImage));
    t.start();

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

    HttpSession session = request.getSession();

    // set session attributes
    session.setAttribute("applicationState", "wait");
    session.setAttribute("operationInProgress", "WPVS");

    // send attributes to jsp page
    request.setAttribute("applicationState", "wait");
    request.setAttribute("operationInProgress", "WPVS");
    dispatcher.forward(request, response);

}

From source file:cn.itcast.bbs.controller.BbsServlet.java

private void addReply(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {/*from w ww .  ja  v a 2s.c  o m*/
        int topicId = Integer.parseInt(request.getParameter("topicId"));
        Reply reply = new Reply();
        reply.setTitle(request.getParameter("title"));
        reply.setContent(request.getParameter("content"));
        User user = (User) request.getSession().getAttribute("user");
        reply.setName(user.getUsername());
        request.setAttribute("topicId", topicId);
        request.setAttribute("reply", reply);
        service.addReply(reply, topicId);
        response.sendRedirect(request.getContextPath() + "/BbsServlet?method=showReply&topicId=" + topicId);
    } catch (Exception e) {
        e.printStackTrace();
        request.setAttribute("message", "?");
        request.getRequestDispatcher("/WEB-INF/bbs/message.jsp").forward(request, response);
    }
}

From source file:ams.fwk.channel.web.AmsRequestView.java

public void render(IRequestContext requestCtx, IResponseContext responseCtx) throws RenderException {

    super.render(requestCtx, responseCtx);

    HttpServletRequest request = (HttpServletRequest) responseCtx.getReadProtocol();

    HttpServletResponse response = (HttpServletResponse) responseCtx.getWriteProtocol();

    /*/*from   www.j  a  va 2  s. c  o  m*/
     * nc_target target ,   ?    url? .
     *  ?  properties? ? path   ? 
     */
    String target = null;
    if (request.getParameter(Constants.TARGET) != null) {
        target = BaseUtils.getConfiguration(request.getParameter(Constants.TARGET));
        if (StringUtils.isEmpty(target)) {
            target = (String) request.getParameter(Constants.TARGET);
        }
    } else {
        target = this.target;
    }

    /*
     * forward/redirect .
     */
    boolean isForwarding = true;
    if (target.startsWith(FORWARD_KEY)) {
        target = target.substring(FORWARD_KEY.length());
    } else if (target.startsWith(REDIRECT_KEY)) {
        isForwarding = false;
        target = target.substring(REDIRECT_KEY.length());
    }

    try {
        if (isForwarding) {
            request.getRequestDispatcher(target).forward(request, response);
        } else {
            String redirectPage = target.startsWith("/") ? request.getContextPath() + target : target;
            response.sendRedirect(response.encodeRedirectURL(redirectPage));
        }
    } catch (Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error("Exception occurred while forward jsp to " + target, e);
        }
        e.printStackTrace();

        // ?  
        // -. ServletException
        // -. IOException
        throw new RenderException("SKFS1010", new String[] { target, e.getLocalizedMessage() }, e);
    }
}

From source file:com.edgenius.wiki.webapp.servlet.InstallServlet.java

/**
 * @param request//from ww  w. java  2s .c  om
 * @param response
 * @throws IOException 
 * @throws ServletException 
 */
private void viewUpgrade(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String root = DataRoot.getDataRoot();
    Installation install = Installation.refreshInstallation();

    request.setAttribute("existVer", install.getVersion());
    request.setAttribute("newVer", Version.VERSION);
    request.setAttribute("root", root);
    request.getRequestDispatcher("/WEB-INF/pages/install/upgrade.jsp").forward(request, response);
}

From source file:RestrictedService.java

/**
 * When the servlet receives a POST request.
 * // w  ww  .j  a v  a  2 s .  co  m
 * SIMILIAR DOCUMENTATION AS SAGAtoNIF PROJECT
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String forward = "";
    String eurosentiment = "";
    HttpEntity entity = null;
    //HttpResponse responseMARL = null;
    HttpSession session = request.getSession();
    RequestDispatcher view;
    // Get a map of the request parameters
    Map parameters = request.getParameterMap();
    if (parameters.containsKey("input")) {
        if (parameters.containsKey("intype") && parameters.containsKey("informat")
                && parameters.containsKey("outformat")) {
            if (!request.getParameter("intype").equalsIgnoreCase("direct")) {
                forward = RESPONSE_JSP;
                eurosentiment = "intype should be direct";
                session.setAttribute("eurosentiment", eurosentiment);
                view = request.getRequestDispatcher(forward);
                view.forward(request, response);
                return;
            }
            if (!request.getParameter("informat").equalsIgnoreCase("text")) {
                forward = RESPONSE_JSP;
                eurosentiment = "informat should be text";
                session.setAttribute("eurosentiment", eurosentiment);
                view = request.getRequestDispatcher(forward);
                view.forward(request, response);
                return;
            }
            if (!request.getParameter("outformat").equalsIgnoreCase("json-ld")) {
                forward = RESPONSE_JSP;
                eurosentiment = "outformat should be json-ld";
                session.setAttribute("eurosentiment", eurosentiment);
                view = request.getRequestDispatcher(forward);
                view.forward(request, response);
                return;
            }
            //Check that in not url or the other type
            forward = RESPONSE_JSP;
            String textToAnalize = request.getParameter("input");
            try {
                if (parameters.containsKey("algo")) {
                    if (request.getParameter("algo").equalsIgnoreCase("enFinancial")) {
                        entity = callSAGA(textToAnalize, "enFinancial");
                    } else if (request.getParameter("algo").equalsIgnoreCase("enFinancialEmoticon")) {
                        entity = callSAGA(textToAnalize, "enFinancialEmoticon");
                    } else if (request.getParameter("algo").equalsIgnoreCase("ANEW2010All")) {
                        entity = callANEW(textToAnalize, "ANEW2010All");
                    } else if (request.getParameter("algo").equalsIgnoreCase("ANEW2010Men")) {
                        entity = callANEW(textToAnalize, "ANEW2010Men");
                    } else if (request.getParameter("algo").equalsIgnoreCase("ANEW2010Women")) {
                        entity = callANEW(textToAnalize, "ANEW2010Women");
                    }
                }

                if (entity != null) {
                    InputStream instream = entity.getContent();
                    try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(instream));
                        String inputLine;
                        StringBuffer marl = new StringBuffer();

                        while ((inputLine = in.readLine()) != null) {
                            marl.append(inputLine);
                            marl.append("\n");
                        }
                        in.close();
                        eurosentiment = marl.toString();
                        session.setAttribute("eurosentiment", eurosentiment);

                    } finally {
                        instream.close();
                    }
                }
            } catch (Exception e) {
                System.err.println(e);
            }
        } else {
            forward = RESPONSE_JSP;
            eurosentiment = "There is no intype, informat or outformat especified";
            session.setAttribute("eurosentiment", eurosentiment);
        }
    } else {
        forward = RESPONSE_JSP;
        eurosentiment = "There is no input";
        session.setAttribute("eurosentiment", eurosentiment);
    }
    view = request.getRequestDispatcher(forward);
    view.forward(request, response);

}