Dalam javadoc, RequestDispatcher merupakan :
Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.
Dengan begitu, RequestDispatcher dapat kita gunakan untuk memforward request ke halaman lain, contohnya misal saat ini kita akan memcoba membuat sebuah halaman untuk manipulasi data Person, dimana hanya menggunakan sebuah Servlet. Misal saat pengguna membuka halaman :
Jika untuk melakukan render halaman dilakukan, maka hal ini akan sangat menyulitkan, oleh sebab itu ada baiknya jika kita membuat halaman daftar person dan create person pada file yang berbeda, misal kita membuat halaman list.html untuk menampilkan data seluruh person.
<!doctype html>
<html>
<head>
<title>List of Person</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table border="1">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Birthday</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Eko Kurniawan</td>
<td>Khannedy</td>
<td>29 Desember 1988</td>
</tr>
<tr>
<td>1</td>
<td>Eko Kurniawan</td>
<td>Khannedy</td>
<td>29 Desember 1988</td>
</tr>
<tr>
<td>2</td>
<td>Eko Kurniawan</td>
<td>Khannedy</td>
<td>29 Desember 1988</td>
</tr>
<tr>
<td>3</td>
<td>Eko Kurniawan</td>
<td>Khannedy</td>
<td>29 Desember 1988</td>
</tr>
<tr>
<td>4</td>
<td>Eko Kurniawan</td>
<td>Khannedy</td>
<td>29 Desember 1988</td>
</tr>
<tr>
<td>5</td>
<td>Eko Kurniawan</td>
<td>Khannedy</td>
<td>29 Desember 1988</td>
</tr>
</tbody>
</table>
</body>
</html>
dan membuat halaman create.html untuk menampilkan form halaman input data person.
<!doctype html>
<html>
<head>
<title>Create New Person</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Create New Person</h1>
<form method="POST">
<table>
<tbody>
<tr>
<td>First Name :</td>
<td><input type="text" name="first_name"/></td>
</tr>
<tr>
<td>Last Name :</td>
<td><input type="text" name="last_name"/></td>
</tr>
<tr>
<td>Birthday :</td>
<td><input type="text" name="birthday"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Create"/></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
setelah itu baru buat servlet untuk menangani request browser.
package khannedy.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Person", urlPatterns = {"/Person"})
public class PersonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
action = "list";
}
if (action.equals("create")) {
request.getRequestDispatcher("/WEB-INF/page/create.html").forward(request, response);
} else if (action.equals("list")) {
request.getRequestDispatcher("/WEB-INF/page/list.html").forward(request, response);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
}
hasilnya adalah :
Screenshot-List of Person - Google Chrome
Screenshot-Create New Person - Google Chrome
-6.520104
107.638648