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

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

    String username = request.getParameter("username");
    boolean confirm = StringUtils.equals(request.getParameter("confirm"), "true");

    User user = userService.getUser(username);

    String currentUser = (String) request.getSession().getAttribute("username");

    if (user != null && !StringUtils.equals(currentUser, username)) {
        if (confirm) {
            userService.removeUser(user);

            response.sendRedirect("/");
        } else {//from  w  w  w .  j  a v  a2s.  c o  m
            request.setAttribute("user", username);

            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/remove.jsp");
            rd.include(request, response);
        }
    } else {
        response.sendRedirect("/");
    }
}

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

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String username = request.getParameter("username");

    User user = userService.getUser(username);

    if (user != null) {
        request.setAttribute("user", username);

        StringBuilder userDetails = new StringBuilder("<table class=\"table table-bordered table-striped\">"
                + "<thead class=\"thead-inverse\"><tr><th>UserInfo</th><th></th></thead>");
        userDetails.append("<tr><td>Username</td><td>" + user.getUserName() + "</td></tr>");
        //            userDetails.append("<tr><td>Password</td><td>"+user.getPassword()+"</td></tr>");
        userDetails.append("<tr><td>Email</td><td>" + user.getEmail() + "</td></tr>");
        userDetails.append("<tr><td>FirstName</td><td>" + user.getFirstName() + "</td></tr>");
        userDetails.append("<tr><td>LastName</td><td>" + user.getLastName() + "</td></tr>");
        userDetails.append("</tbody></table>");
        request.setAttribute("userDetails", userDetails);

        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/user.jsp");
        rd.include(request, response);
    } else {/*w  w  w . j a v  a  2  s.c om*/
        response.sendRedirect("/");
    }
}

From source file:com.sun.socialsite.web.rest.core.XrdsFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    if ("application/xrds+xml".equals(request.getHeader("Accepts"))) {
        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/jsps/xrds.jsp");
        rd.include(req, res);
    } else {/*from   w  w  w . j a v a  2 s.c  om*/
        chain.doFilter(req, res);
    }
}

From source file:io.muic.ooc.servlet.LoginServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // do login post logic
    // extract username and password from request
    if (SecurityService.isAuthorized(request)) {
        response.sendRedirect("/home");
    } else {/*from ww w.jav  a  2  s. c  om*/
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (!StringUtils.isBlank(username) && !StringUtils.isBlank(password)) {
            if (SecurityService.authenticate(username, password, request)) {
                response.sendRedirect("home");
            } else {
                String error = "Wrong username or password.";
                request.setAttribute("error", error);
                RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/login.jsp");
                rd.include(request, response);
            }
        } else {
            String error = "Username or password is missing.";
            request.setAttribute("error", error);
            RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/login.jsp");
            rd.include(request, response);
        }
    }

}

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

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    boolean authorized = securityService.isAuthorized(request);
    if (authorized) {
        // do MVC in here
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/adduser.jsp");
        rd.include(request, response);
    } else {//  w ww.  j a  v  a  2 s  . c  o m
        response.sendRedirect("/");
    }
}

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

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // do login post logic
    // extract username and password from request
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if (!StringUtils.isBlank(username) && !StringUtils.isBlank(password)) {
        if (securityService.authenticate(username, password, request)) {
            response.sendRedirect("/");
        } else {/* w w  w  .  j av a2s  .c om*/
            String error = "Wrong username or password.";
            request.setAttribute("error", error);
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/login.jsp");
            rd.include(request, response);
        }
    } else {
        String error = "Username or password is missing.";
        request.setAttribute("error", error);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/login.jsp");
        rd.include(request, response);
    }

    // check username and password against database
    // if valid then set username attribute to session via securityService
    // else put error message to render error on the login form

}

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

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String iusername = request.getParameter("username");
    String ipassword = request.getParameter("password");
    String ifirstname = request.getParameter("fname");
    if (!StringUtils.isBlank(iusername) && !StringUtils.isBlank(ipassword)
            && !StringUtils.isBlank(ifirstname)) {
        if (!db.readData().containsKey(iusername)) {
            db.insertDB(iusername, ipassword, ifirstname);
            response.sendRedirect("/login");
        } else {/*from  w  w w.  j a  v  a2  s .  c  om*/
            String error = "Please choose another username. ";
            request.setAttribute("error", error);
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/signup.jsp");
            rd.include(request, response);
        }
    } else {
        String error = "Please fill in all the requirement form.";
        request.setAttribute("error", error);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/signup.jsp");
        rd.include(request, response);
    }
}

From source file:FormServlet.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/validate.js");

    dispatcher.include(request, response);

    out.println("<title>Help Page</title></head><body>");
    out.println("<h2>Please submit your information</h2>");

    out.println("<form action =\"" + request.getContextPath()
            + "/displayHeaders.jsp\" onSubmit=\" return validate(this)\">");

    out.println("<table border=\"0\"><tr><td valign=\"top\">");
    out.println("Your name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"username\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("Your email: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"email\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");

    out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>");
    out.println("</table></form>");
    out.println("</body></html>");
    out.close();//  w  w w .  j a v a  2  s .c  o  m
}

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

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//  w ww.j a  va2  s  .c  o  m
        boolean authorized = securityService.isAuthorized(request);
        if (authorized) {
            // do MVC in here
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/add.jsp");
            rd.include(request, response);
        } else {
            response.sendRedirect("/login");
        }
    } catch (Exception e) {

    }
}

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

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

    UserService userService = new UserService();

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

    Connection conn = userService.connectSQL();

    System.out.println("SignupServlet");
    boolean userExist = userService.userExist(conn, username);

    if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(name)) {
        String error = "Please don't leave any blank field!";
        request.setAttribute("error", error);

        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/signup.jsp");
        rd.include(request, response);
    } else if (!userExist) {
        /* For posting user information */
        String info1 = "Your id is: " + LoginServlet.currentUserID;
        String info2 = "Your name is: " + LoginServlet.currentUserName;

        request.setAttribute("info1", info1);
        request.setAttribute("info2", info2);
        /* Ends here */

        userService.createUser(conn, username, password, name);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/home.jsp");
        rd.include(request, response);//from   w ww .j  ava  2  s  .  com
    } else {
        String error = "This username already exists, please choose another";
        request.setAttribute("error", error);

        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/signup.jsp");
        rd.include(request, response);
    }
}