Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSP and the fly likes Servlet versus JSP - Urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlet versus JSP - Urgent" Watch "Servlet versus JSP - Urgent" New topic
Author

Servlet versus JSP - Urgent

Mauricio Andrade JR
Greenhorn

Joined: Apr 15, 2003
Posts: 2
I'm newbee to Java and need a urgent answer. At my company we are starting java development and there is a consultor helping us. We asked him to teach us how to hard code a MVC controller, as we had used struts before. The problem is that he did the controller as a JSP page! I always thought it would not be something that one put in a JSP page. I think the controller should be a servlet but I don�t have enough knowledge to argue with him. And he�s leaving. So, technicaly speaking, how could I prove to him that he�s wrong in doing such a thing ???

Thanxs,
Mauricio.
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Hi..
Controller is meant for getting the user input from the view and communicate with the model to reply back the view with the required information.
JSP can only serve as a view.View is a presenattion layer, so JSP should be used to build the view.Whereas controller is a sort of mediator which does some important processing between view and model.So it is advisable to use servlets for the same as servlet is more of builing application logic rather than bulding presentation logic.
U can visit the sun site for more Details..
Cheers
Geeta
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
For one thing, it seems to me that having a JSP as controller makes it more difficult to serve binary data. As Geeta says, Sun's standard view of the roles of servlets and JSP always puts servlets in the controller position.
Bill
Hugh Jamieson
Greenhorn

Joined: Dec 03, 2002
Posts: 8
Here's my 10 cents...
One pricipal design goal for using Struts or any MVC architecture is to separate logic from presentation. JSP=presentation, servlet=logic. While it is technically possible to use a JSP as the controller, it really kind of silly. After all, the JSP will be compiled into a servlet anyway. What advantage is there in having the ability to short-hand markup from this controller? The only benefit I could see is that you can change the controller my simply changing the source; the server will automatically recompile it. But this could also open you up to inadvertent changes that could crash your app.
The idea behind MVC is to separate form + function. The same person writing the business logic of your application may not be the same person doing your markup. And do you really want to scan through thousands of lines of JSP code to figure out why the user just bought 1000.00 units instead of 10000.0 units?
There are certain other advantages to servlets that make them better candidates for the controller mechanism, but I would not get too hung up on it. If the JSP is a pure scriptlet, maybe it's not so bad. Still, I would not use a JSP as my controller. I agree with you on this.
Hugh
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
some random thoughts that may bolster your case...
i've never seen a jsp as controller, except in books, where it is used to demonstrate both that it can be done and that there is no reason to do so.
as stated above jsp's should be concerned with taking business data and displaying it.
i imagine that a controller jsp is hard to read, therefore hard to maintain. having the controller exist as a java class (e.g. servlet) will surely aid in maintenance and ability to easily extend.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
One pragmatic reason... A Java method cannot exceed 64k. If you are using any scriptlets in your code (and even if you aren't depending on the compiler), your entire JSP will be placed in one method of a servlet when it is compiled. If you build some huge monster of a JSP controller, which is probably more likely than if you were using good OO principles in a well designed controller servlet, you may run up against the 64k wall.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Would you mind revealing the company who sent this "consultant"? I for one would think twice about employing their services again after this JSP-controller thingy.
It might be OK to use JSP in order to simplify an example BUT he would've at least had to mention that this is not a recommended approach for this and that reason.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Servlet versus JSP - Urgent
 
Similar Threads
Urgent!
Servlet versus JSP - Urgent
Urgent
URGENT!!!!
MVC (urgent)