What exactly is a RequestHelper
Hi all,
I've been gathering information from various sources regarding various presentation tier design patterns. In common, most of these patterns states the use of Command and Request Helper.
The most common code snippet I found on the Main Servlet controller was:
RequestHelper helper =new RequestHelper(req);
Command cmd = helper.getCommand();
String page = cmd.execute( helper);
RequestDispatcher dispatcher =getServletContext().getRequestDispatcher(page);
dispatcher.forward(req, resp);
I've been trying to source out concrete code examples of RequestHelper but my effort was futile.
All I can see from the code snippet was that the RequestHelper was taking in the request, and from the request, it created some Command ( I assume it was using the GoF Command Pattern ) which is responsible for handling the request. This Command was also responsible to decide on the next page . However, I'm just unable to visualize how's the implementation like in the RequestHelper.
In addition, how is this implementation supposed to be useful in the following scenario:
- A servlet will recieves a request.
- Based on the request , it will do some data retrieval depending on some parameter in the request.
- After the data retrieval, it will forward the request to corresponding pages.
Thank you very much.
-chiew boon
# 2
Hi,
Thank you. For now, I'm more familiar with the RequestHelper.
Given a scenario where I've implementation both my navigation logic and business logic using a single servlet : Example:
class myServlet {
doPost(..) {
String action = request.getParameter("action");
if ( aciton.equals("Case1") {
//do case 1
}
if ( aciton.equals("Case2") {
//do case 2
}
//.....etc
}
}
Now, if I am to do some design improvement using the controller pattern. That is, for each case, I will be using a Command object.
However, this will result is objects like, Case1Command, Case2Command etc.
On one hand, I've managed to make my controller simpler by segregating the business logic from the navigation logic, on the other, I've introduce tremendous number of classes (Command Objects).
Is this an overkill?
-chiew boon
# 3
> On one hand, I've managed to make my controller
> simpler by segregating the business logic from the
> navigation logic, on the other, I've introduce
> tremendous number of classes (Command Objects).
>
> Is this an overkill?
No, it is not an overkill (if you use cache). As a meter of fact doing this you get closer from the Struts controller. The commands you intend to do, and that are supposed to encapsulate the business of the application, are the equivalent of the Struts Actions classes. You can cache these commands so that not to build new ones each time a request is made (you must use synchronization here).
It is a good idea to separate the navigation logic from the business logic. But .. by the way, why do not you use Struts framework ?
# 4
Hi,
Thanks for your valuable opinion. Yes, struts was also in my mind right now, however, what I am try to to is to incorporate some framework into a existing system which does not has any.
If I were to implement struct into the system in full scale, the effort will not be justifiable. Thus, on the other side, while enhancements to the system are made, I gradually tries to incorporate some simple and light weight framework, which in my case, it's simply applying some design pattern.
By the way, when you mentioned about synchronising, which areas should be syn. Currently, I've only had the execute method in the command objects sync.
Message was edited by:
Chiew_Boon
# 5
> By the way, when you mentioned about synchronising,
> which areas should be syn.
You must synchronize the cache (if you want to use it). The cache will hold the commands (action classes - the model in the MVC pattern) that will be used concurently by all the server threads. The controller is a servlet that works in a server environment (google for servlet model).
>Currently, I've only had
> the execute method in the command objects sync.
Which is the purpose for synchronizing the execute ? Does the command objects have some members ? If yes, you should change this if you want to cache them.