Menggunakan RequestDispatcher pada Servlet dan JSP

RequestDispatcher dapat digunakan sebagai penerus data dari servlet menuju jsp. Apabila sebelumnya kita menggunakan session untuk menyimpan data dan memforward data tersebut menuju jsp. Kali ini kita akan menggunakan RequestDispatcher untuk meneruskan request dari suatu page menuju servlet kemudian langsung diproses oleh page receiver. Berikut contoh penggunaan RequestDispatcher

package com.zisal.loginapp;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Login
 */
@WebServlet(urlPatterns = "/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		
		LoginService loginService = new LoginService();
		boolean result = loginService.authenticate(userId, password);
		if(result){
			request.setAttribute("user", loginService.getUserName(userId));
			RequestDispatcher dispatcher = request.getRequestDispatcher("Success.jsp");
			dispatcher.forward(request, response);			
		}else{
			response.sendRedirect("index.jsp");
		}
	}
}

Request dari page sebelumnya akan disimpan dalam attribut “user” kemudian akan diteruskan ke page Success.jsp melalui RequestDispatcher.

LoginDetail.jsp

<%@page import="com.zisal.loginapp.User"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
	<%
		User user =  (User) request.getAttribute("user");		
		if(user != null){
			out.println("<h3>You Logged in as ".concat(user.getUserName())+"</h3>");
		}
	%>
</body>
</html>

Pada LoginDetail.jsp, page tersebut akan menangkap request dari page lain dengan attribu “user”.

output :

Tagged: , ,

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: