This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes why servlet as controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "why servlet as controller" Watch "why servlet as controller" New topic
Author

why servlet as controller

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Main reasons for using servlet as a controller in struts. why not jsp
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Can you explain what is meant by controller then ?
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
What i know about controller is : to handle request from client and forward it to model(for accessing database) after processing business logic send response back to client. I Am new to this concepts . please explain clearly. Thanks Prakash
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

It's Model/View/Controller. You're dismissing the View part.

The reason why servlet's make better controllers than JSP's is that JSP's were designed to present Views and Controllers are logic, not display. JSPs get compiled to produce servlets, but their source format is more like the presentation display, so they're easier to maintain.

Conversely, loading a JSP with scriptlets (Java code) can get really ugly really fast. And they're a nightmare to maintain.

Hence, servlets are good Controllers but (generally) lousy Views and JSPs are good Views, but (almost always) lousy controllers.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
This article explains the benefits nicely: http://www.javaranch.com/journal/200603/frontman.html


Ping & DNS - my free Android networking tools app
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Hi Friend Still am not clear about servlet as controller. See , we can use jsp also as a controller . As per mvc-2 we are using servlet as controller . Please give this clearly Advantages of servlet as controller ,Disadvantages of using jsp as controller
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
My interviewer asked this in depth. so am asking you again. I understand it may irritate you. But please clarify me.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Tim's reply has it all.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
we can use jsp also as a controller.

That's like saying "we can open a window with a hammer" - it achieves the primary objective, but fails in all other regards. JSPs are a templating technology, not something to put code into (much less code that redirects or forwards elsewhere). Have you read the article I linked to?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Take a look at the servlet code which any JSP is translated to. You will see that JSP makes many many assumptions about the desired output.

JSP expects that you want to write an HTML page but any real-world web application may have to generate any of a huge variety of outputs from images to PDF. Only servlets - which only assume a Http request and response - are suitable for a controller.

Bill
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

William Brogden wrote:
JSP expects that you want to write an HTML page


Actually, that's not really true. Both XML (including the cell-phone WAP form) and PDF's are frequently generated from JSPs - among other formats. But those are Views.

I'll accept the term "suitable" here to mean "If you try and ram Controller functionality into a JSP I'll hunt you down and do you serious bodily harm", however. I'm still healing from some really awful JSPs that someone did exactly that in.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
and PDF's are frequently generated from JSPs


Oh really? The JSP writes a PDF formatted output directly to output? Can you point to an example?

Bill
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

William Brogden wrote:
and PDF's are frequently generated from JSPs


Oh really? The JSP writes a PDF formatted output directly to output? Can you point to an example?

Bill


Well, I'm sufficiently twisted to do just that. A PDF is, after all just PostScript with some extra wrappings.

But most people are either emitting the actual PDF content via a JavaBean or a controller servlet. What they're actually doing is just using the JSP as a way to poke out a content-type of "application/pdf". If you've never run across one of these puppies, you've been more fortunate than I have. <howl/>

Remember, your assertion was a blanket one that JSP's were only used for HTML, without reference to whether there was a separate controller object.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

My use of JSPs is for when there needs to be a text template. 98.5% of the time, that's for HTML. But they are also useful for other types of text responses. Anything that doesn't need a text template doesn't go through a JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
G.Sathish kumar
Ranch Hand

Joined: Jul 27, 2009
Posts: 84
Rajendra Prakash wrote:Main reasons for using servlet as a controller in struts. why not jsp


Hi Prakash

Jsp we have to use this for only presentation because embed the java code with html, main advantage of doing that ease of developing the presentation layer

Jsp we can use this for controller but not recommended because when you see the translated servlet of the jsp you can see the lot of out.print

MVC design pattern requirement not full filled if you use jsp as controller logic

finally servelet is just java class so the communication with models easy which is not that much ease in jsp

first servlet used with html then only jsp has beed used in the web application development, have a look what is the needy of jsp technology?

i am wron anywhere please let me know.



Servelet we have to use this for controller because

Thanks
Sathish kumar
SCJP, SCWCD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

G.Sathish kumar wrote:... because embed the java code with html, main advantage of doing that ease of developing the presentation layer

It is not longer considered acceptable to embed Java scriptlet code in JSPs. Rather, the JSTL and EL should be employed to provide the dynamic action of JSPs.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

Bear Bibeault wrote:
G.Sathish kumar wrote:... because embed the java code with html, main advantage of doing that ease of developing the presentation layer

It is not longer considered acceptable to embed Java scriptlet code in JSPs. Rather, the JSTL and EL should be employed to provide the dynamic action of JSPs.


Actually, I'd already deduced that it wasn't acceptable long before JSTL and EL were ever designed. Java and JSP have two radically different syntaxes, and when you combine the 2 in a single editable file, it drives all but the most sophisticated editors insane. And there were no editors that sophisticated back then. At least if you absolutely insist on coding logic into the view, JSTL keeps it all in one syntax framework.

I can only repeat, writing controller logic into a JSP should be a punishable offence with severe penalties. Quite recently I helped someone out with a system that had been coded with controller logic in JSPs. It was, in itself, a punishment. It was abominable. 20-40 page JSPs with presentation and logic splattered all over the landscape.

I'm warning you, children: if you don't lead good and virtuous lives, you'll spend eternity doing maintenance on projects like that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why servlet as controller
 
Similar Threads
why controller is servlet
servlet is used as controller
Parsing xml from a URL
ActionServlet
In MVC