• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why servlet as controller

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Main reasons for using servlet as a controller in struts. why not jsp
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you explain what is meant by controller then ?
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This article explains the benefits nicely: http://www.javaranch.com/journal/200603/frontman.html
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim's reply has it all.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic