Home > Java and Web > RequestDispatcher Interface

RequestDispatcher Interface

RequestDispatcher interface merupakan interface yang menyediakan method untuk mengalihkan sebuah request ke resource lain seperti html, jsp dan servlet. RequestDispatcher interface terletak di javax.servlet package. RequestDispatcher interface mempunyai 2 method yaitu forward(ServletRequest request, ServletResponse response) dan include(ServletRequest request, ServletResponse response),Untuk detail dapat dibaca disini.

  • Method forward(ServletRequest request, ServletResponse response) : Mengalihkan sebuah request ke resource lain seperti html, jsp dan servlet.
  • Method include(ServletRequest request, ServletResponse response) : Menambahkan content resource lain seperti html, jsp dan servlet kedalam response.

Berikut ini contoh penggunaan RequestDispatcher interface.

File : index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="Login" method="post">
        <table>
            <tr>

            <td>User Name</td>

            <td><input type="text" name="username"></td>

           </tr>
            <tr>

            <td>Password</td>

           <td><input type="password" name="password"></td>

            </tr>
        </table>
        <input type="submit" value="Input">
        </form>
    </body>
</html>

File : Login.java ( Servlet )



import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
public class Login extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @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 {
        //Do Nothing
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
       
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        if(username.equals("Ren") && password.equals("Ren")){
            RequestDispatcher rd = request.getRequestDispatcher("/Dispatch");
            rd.forward(request, response);
        } else {
            out.println("Username and/or Password wrong");
            RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
            rd.include(request, response);
        }
    }
}

File : Dispatch.java ( Servlet )

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
public class Dispatch extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @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 {
        //Do Nothing
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        String username = request.getParameter("username");
        out.println("Welcome"+username);
    }

}

File : web.xml ( Add config )


 

......
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>ServletPackage.Login</servlet-class>
</servlet>
<servlet>
<servlet-name>Dispatch</servlet-name>
<servlet-class>ServletPackage.Dispatch</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Dispatch</servlet-name>
<url-pattern>/Dispatch</url-pattern>
</servlet-mapping>
......

Contoh merupakan case login, jika usernama= “Ren” dan Passwor=”Ren” maka RequestDispatcher akan memforward request ke servlet dispatch, jika salah maka RequestDispatcher akan men-include response “Username and/or Password wrong” kedalam resource index.jsp.

Halaman login :

Login

Login

Login berhasil :

Login Berhasil

Login Berhasil

Login gagal:

Login Gagal

Login Gagal

Semoga bermanfaat.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: