Example usage for javax.servlet RequestDispatcher include

List of usage examples for javax.servlet RequestDispatcher include

Introduction

In this page you can find the example usage for javax.servlet RequestDispatcher include.

Prototype

public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException;

Source Link

Document

Includes the content of a resource (servlet, JSP page, HTML file) in the response.

Usage

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

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("Add " + username);
    System.out.println("Add " + password);
    if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
        String error = "Username or password is missing.";
        request.setAttribute("error", error);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/add.jsp");
        rd.include(request, response);
    } else {/*  w  w  w . j  a va2  s.co  m*/
        try {
            if (mySQLService.findOne(username)) {
                String error = "This Username is already exist.";
                request.setAttribute("error", error);
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/add.jsp");
                rd.include(request, response);
            } else {
                String digest = DigestUtils.md5Hex(password);
                mySQLService.addDataBase(username, digest);
                response.sendRedirect("/");
            }
        } catch (Exception e) {

        }

    }
}

From source file:org.owasp.benchmark.testcode.BenchmarkTest00098.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    javax.servlet.http.Cookie userCookie = new javax.servlet.http.Cookie("BenchmarkTest00098", "my_user_id");
    userCookie.setMaxAge(60 * 3); //Store cookie for 3 minutes
    userCookie.setSecure(true);/*from  w  w w .j a  v a  2  s . c om*/
    userCookie.setPath(request.getRequestURI());
    response.addCookie(userCookie);
    javax.servlet.RequestDispatcher rd = request.getRequestDispatcher("/trustbound-00/BenchmarkTest00098.html");
    rd.include(request, response);
}

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

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String firstname = request.getParameter("firstname");

    if (securityService.isAuthorized(request)) {

        if (!StringUtils.isBlank(username) && !StringUtils.isBlank(password)) {

            if (securityService.allowAdd(username)) {
                PrintWriter out = response.getWriter();
                new DatabaseService().insertDB(username, securityService.hashPassword(password), firstname);
                //                    new DatabaseService().insertDB(username,password,firstname);
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/home.jsp");
                rd.include(request, response);
                out.print("<p style=\"color:green\">Add User Successful</p>");
            } else {
                String error = "Username already exist";
                request.setAttribute("error", error);
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/adduser.jsp");
                rd.include(request, response);
            }// w ww.  ja  v a2s. co  m
        } else {
            String error = "Username or password is missing.";
            request.setAttribute("error", error);
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/adduser.jsp");
            rd.include(request, response);
        }
    } else {
        response.sendRedirect("/");
    }
}

From source file:ModuleServlet.java

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

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head>");
    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/functions.js");
    dispatcher.include(request, response);

    out.println("<title>Client Forms</title></head><body>");

    out.println("<h2>Enter Your Name and Email</h2>");
    out.println(/*from w  w  w  . j a v  a  2s  .  c  om*/
            "<form action=\"/home/displayHeaders.jsp\" name=\"entryForm\" onSubmit=\" return CheckEmail(this.email.value)\">");
    out.println("<table border=\"0\"><tr><td valign=\"top\">");
    out.println(
            "First and last name: </td>  <td valign=\"top\"><input type=\"text\" name=\"name\" size=\"20\"></td></tr>");
    out.println("<tr><td valign=\"top\">");
    out.println("Email: </td>  <td valign=\"top\"><input type=\"text\" name=\"email\" size=\"20\"></td>");
    out.println("<tr><td valign=\"top\"><input type=\"submit\" value=\"Submit\" ></td>");
    out.println("</tr></table></form>");

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

}

From source file:com.berwickheights.spring.svc.GenEmailMsgSvcImpl.java

public String genEmailMsgFromJSP(HttpServletRequest request, HttpServletResponse response, String jspPagePath,
        Map<String, Object> extraPageArgs) throws IOException, ServletException {
    Map<String, Object> args = new HashMap<String, Object>();

    // Add extra arguments if provided
    if (extraPageArgs != null) {
        for (String key : extraPageArgs.keySet()) {
            args.put(key, extraPageArgs.get(key));
        }/*from   ww  w  .  j  a  v  a 2  s. c  o  m*/
    }

    // Set up data for jsp/jstl template
    request.setAttribute("args", args);

    // Create an output stream
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Create the "dummy" response object
    RedirectingServletResponse dummyResponse;
    dummyResponse = new RedirectingServletResponse(response, baos);

    // Get a request dispatcher for the email msg body to load
    RequestDispatcher rd = request.getRequestDispatcher(jspPagePath);

    // Execute JSP
    rd.include(request, dummyResponse);

    //
    // Get email message body
    // NB: JSP page must have "out.flush()" at end of file to flush Tomcat buffers
    // so that ByteArrayOutputStream (baos) will contain the mail message text
    //
    dummyResponse.flushBuffer();
    String emailText = baos.toString();
    baos.close();

    if (logger.isDebugEnabled()) {
        logger.debug("Generated message: " + emailText);
    }

    return emailText;
}

From source file:servlets.EditServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    clientService = new ClientService();
    Client user = (Client) request.getSession().getAttribute("user");
    String mail = user.getMail();
    System.out.println("mailll " + mail);

    String oldpassword = request.getParameter("password");
    newpassword = request.getParameter("newpassword");
    System.out.println("password " + request.getParameter("password"));
    System.out.println("newpassword " + request.getParameter("newpassword"));

    request.setAttribute("PasswordWrong", false);
    request.getSession().setAttribute("PasswordDiv", true);

    if (clientService.CheckPassword(mail, oldpassword)) {
        System.out.println("find user");
        request.getSession().setAttribute("PasswordDiv", false);
        RequestDispatcher dis = request.getRequestDispatcher("EditAccount.jsp");
        dis.include(request, response);
        request.getSession().setAttribute("PasswordDiv", true);

    } else {//from www . j a  va2  s  .c o  m
        System.out.println("not found user");
        RequestDispatcher dis = request.getRequestDispatcher("EditAccount.jsp?password=wrong");
        dis.forward(request, response);
    }

}

From source file:org.jboss.dashboard.ui.config.TreeActionsHandler.java

public CommandResponse actionShowEditPage(CommandRequest request) {
    TreeNode treeNode = treeStatus.getLastEditedNode(getTree());
    String editPage = treeNode.getEditURI();
    RequestDispatcher rd = request.getRequestObject().getRequestDispatcher(editPage);
    try {/*from w w w .j a v a  2s  . c om*/
        rd.include(request.getRequestObject(), request.getResponseObject());
    } catch (Exception e) {
        log.error("Error including page: " + editPage, e);
        try {
            request.getResponseObject().sendError(500);
        } catch (IOException e1) {
            log.error("Error:", e1);
        }
    }
    return new DoNothingResponse();
}

From source file:org.jtwig.functions.SpringFunctions.java

@JtwigFunction(name = "render")
public String render(HttpServletRequest request, @Parameter String url,
        @Parameter Map<String, String> parameters) throws FunctionException {
    RenderHttpServletResponse responseWrapper = new RenderHttpServletResponse();
    RenderHttpServletRequest builder = new RenderHttpServletRequest(request).to(url).withMethod(GET);

    for (Map.Entry<String, String> entry : parameters.entrySet()) {
        builder.withGetParameter(entry.getKey(), entry.getValue());
    }/* ww  w.  j a v  a 2  s  .  c  o m*/

    try {
        RequestDispatcher requestDispatcher = request.getRequestDispatcher(request.getServletPath());
        requestDispatcher.include(builder, responseWrapper);

        return responseWrapper.toString();
    } catch (ServletException | IOException e) {
        throw new FunctionException(e);
    }
}

From source file:org.owasp.benchmark.testcode.BenchmarkTest01860.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    javax.servlet.http.Cookie userCookie = new javax.servlet.http.Cookie("BenchmarkTest01860", "whatever");
    userCookie.setMaxAge(60 * 3); //Store cookie for 3 minutes
    userCookie.setSecure(true);/*w  w w . j a  v  a2s  .  com*/
    userCookie.setPath(request.getRequestURI());
    response.addCookie(userCookie);
    javax.servlet.RequestDispatcher rd = request.getRequestDispatcher("/weakrand-04/BenchmarkTest01860.html");
    rd.include(request, response);
}

From source file:org.openhie.openempi.webapp.filter.StaticFilter.java

/**
 * This method checks to see if the current path matches includes or excludes. If it matches includes and
 * not excludes, it forwards to the static resource and ends the filter chain. Otherwise, it forwards to the
 * next filter in the chain.//from  w w  w  .  j  a va 2  s.  co m
 *
 * @param request the current request
 * @param response the current response
 * @param chain the filter chain
 * @throws ServletException when something goes wrong
 * @throws IOException when something goes terribly wrong
 */
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    UrlPathHelper urlPathHelper = new UrlPathHelper();
    String path = urlPathHelper.getPathWithinApplication(request);
    boolean pathExcluded = PatternMatchUtils.simpleMatch(excludes, path);
    boolean pathIncluded = PatternMatchUtils.simpleMatch(includes, path);

    if (pathIncluded && !pathExcluded) {
        if (logger.isDebugEnabled()) {
            logger.debug("Forwarding to static resource: " + path);
        }

        if (path.contains(".html")) {
            response.setContentType("text/html");
        }

        RequestDispatcher rd = getServletContext().getRequestDispatcher(path);
        rd.include(request, response);
        return;
    }

    if (servletName != null) {
        RequestDispatcher rd = getServletContext().getNamedDispatcher(servletName);
        rd.forward(request, response);
        return;
    }

    chain.doFilter(request, response);
}