File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes why to use servlet as controller not jsp 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 » Engineering » OO, Patterns, UML and Refactoring
Bookmark "why to use servlet as controller not jsp" Watch "why to use servlet as controller not jsp" New topic
Author

why to use servlet as controller not jsp

kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
HI all
why we use servlet as controller in MVC for web, why not JSP.Even JSP can do the same thing what a servlet is capable of.
plz clarify
thanks
candy


SCJP1.4,SCBCD,SCEA,CNA
Failures are practice shoots for success.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Basically, JSP aims to be written by non-Java programmers. Thus, you can put in HTML codes, together with some scriptlets (now include JSTL as well). The invention of JSP is due to the easy implementation for Web designers. They can continue their work, using HTML or even a Web page composer.
If we use Serlvet for the display, then those designers need to understand Java coding, and know how to compile, package the codes, etc.
Moreover, as I said, the design of the JSP is different to Serlvet, even though they are finally the same. The life cycle of JSP is: one jspInit(), several _jspServices() and one jspDestroy(). You can see that there is only 1 week to make a request to a JSP and only 1 service.
However, for Servlet, you can have doGet(), doPost(), etc, to handle different requests. In addition, Servlet can be totally isolated with the presentation layer. It does not need to know how the JSP (view) to display the data. And, its main purpose is NOT to display data.
Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

However, for Servlet, you can have doGet(), doPost(), etc, to handle different requests.

Usually a servlet acting as a controller delegates to some method say doProcess from doPost and doGet. In jspService, the HTTP method type can be detected as well.
It is easier to write the controller code in servlet rather than doing so in JSP using scriptlets.


Groovy
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
HI pradeep
how can u say that writing code is easier in servlet than jsp
plz explain
thanks
candy
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Candy,
It is simply because as a programmer it is easier to write


rather than

<%...%>

[ April 20, 2004: Message edited by: Pradeep Bhat ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by candy varma:
how can u say that writing code is easier in servlet than jsp
Because in a JSP, it's easy to cause accidental "out.println()'s" into the generated Servlet source code, which makes life difficult (impossible) if your intention is to redirect the request somewhere else -- the servlet container will throw exceptions at you saying something about not being able to redirect once something has been written into the response stream.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
I think the discussion should be from the very beginning.
First, we have CGI to perform Server-side request handling. On this extend, we got Servlet. So, becos JSP in fact finally being compiled into Servlet, thus, why we need 2 different things during development but in fact they are the same in runtime?
The focus on Servlets are aim to handle requests on Servlet-side, so that most (if not all) issues and program executions are in the server-side, which is transparent to the clients. Thus, the client is not interesting in what the server did! But what the server returns! So, what does the server return? In HTTP, the server returns HTML.
For a Web designer, he is responsible to provide fancy HTML pages, however, he will be very painful in doing this. Becos he may not be a Java expert, but a HTML expert. Thus, can you imagine what he did is not writing HTML code, but using out.println("..."); in many lines to *print out* the HTML codes?
In this aspect, SUN invents JSP, which accepts plain HTML text, that allows the Web designer to do the HTML work, but also allow the Java programmers to do something that HTML cannot do, such as dynamic content generations.
Back to your question, why not use Servlet to display HTML and use JSP to behave as the controller, you can see, JSP is designed for HTML display! Otherwise, JSP is no need to exist!!! All things can in fact be done by Servlet!
In addition, Servlet is always alive in the server-side (the container), while JSP is aims to generate dynamic content for displaying in the client side, it is a better idea for keeping Servlet as the controller.
For the coding issues, in fact, I think JSP is easier to code. What you did write in <%...%> is Java code! No different with those in the method calls! Thus, whether JSP or Serlvet is easier to code is not a concern to programmer, but a Web designer.
Nick
John Holme
Ranch Hand

Joined: Oct 11, 2002
Posts: 54
For very simple applications, you may not really need a controller at all. Just provide direct links between the various JSPs that comprise your application: .
One nice thing about using a servlet controller is that you can use web.xml to map URLs to servlets, then forward to a particular JSP to display the results. The advantage of this capability becomes apparent when you find that the same user action might actually map to more than one JSP. For example, a URL containing the path "admin.do" might forward to two entirely different JSPs depending on whether the user is logged in as a customer or an administrator. You could also use "admin.jsp" or something like it to forward to another JSP, but then you're limited to using physical filenames instead of the logical mappings provided in web.xml.
Once the application gets a little more complicated, the Java scriptlets that you can write in a JSP quickly become inadequate. JSPs enable a developer to 1) provide procedures within the underlying servlet's service method in <% %> tags or 2) write new methods for the servlet using <%! %> tags. What you cannot do with a JSP is write Java objects.
Object orientation is where the real strength of Java lies. By using objects, you can leverage concepts like inheritance,
polymorphism and encapsulation that just aren't available within the narrow world of the JSP. Object-oriented architectures provide avenues for handling variations in the code that result in more robust and maintainable application. JSPs are not Java objects: they cannot be extended like a Java class, and they cannot implement a Java interface.
For a very readable introduction to object oriented concepts and the design patterns that incorporate them, take a look at Alan Shalloway's "Design Patterns Explained: A New Perspective on Object-Oriented Design" (ISBN 0201715945).
JSPs are a great convenience for web application programmers, but over reliance on them can result in screenfuls of procedural code that is difficult to understand and maintain.
John Holme
Ranch Hand

Joined: Oct 11, 2002
Posts: 54
Originally posted by John Holme:
[QB]For very simple applications, you may not really need a controller at all. Just provide direct links between the various JSPs that comprise your application: <a href="myPage.jsp"></a>.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
In old days (in fact maybe 2 years ago), before the MVC model 2 take place, many people use MVC model 1, that is, all workflow logic are coded inside the JSPs. (Ko Ko told me this somedays ago, before, I think MVC must be JSP as view, data as model and Servlet as controller )
While the system grows, people starts feel that, if the system is complex, there are more and more logic coded inside the JSP, while increase the effort for code maintainence and view reuse. View becomes not reusable, as more and more *specific* logic are coded.
So, people tried to think about model 2. using JSP most for views, while all logic moves to the Servlet.
You can see the trent. Nowadays, people start feeling that, using scriptlets in JSP as well as HTML may increase the effort for view maintanence, and thus, they come think about JSTL and EL, which tries to minimize the use of scriptlets and HTML code. They are tried to replace by JSTL and EL as much as possible.
Nick
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Originally posted by Nicholas Cheung:
Basically, JSP aims to be written by non-Java programmers. Thus, you can put in HTML codes, together with some scriptlets (now include JSTL as well). The invention of JSP is due to the easy implementation for Web designers. They can continue their work, using HTML or even a Web page composer.

At least, that was the theory. In the old days, as you point out, what actually happened was that JSP pages started collecting more and more scripting interspersed with the HTML. And to actually write a good JSP page, you ended up needing either someone who was good at both HTML and Java, or you needed two separate people working in close collaboration. That at least that was my experience; I'm interested in others with different experience.
I'm curious as to any first hand accounts about the newer versions of JSP, using JSTL or JSF, which are intended to better separate the model and the controller. Can HTML people now really handle the JSP pages on their own, or do they still need programming knowledge?
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
In JSP 2.0, HTML people still cannot handle JSP developement without having programming knowledge, but EL did help a lot.
In the past, if they wanna get something from the session, they need to know Java APIs:

With EL, they can now do this:

Thus, even they dont know the Servlet APIs, if they know the magic, they can still get what they wanna. I think learning EL is a bit easier than learning the Servlet APIs, as EL is somewhat like object properties. And, this is a bit similar to JavaScript as well.
Nick
[EJFH: Edited to use escapes for angle brackets so they show up!]
[ April 20, 2004: Message edited by: Ernest Friedman-Hill ]
C Kutler
Ranch Hand

Joined: Apr 15, 2004
Posts: 62
One good reason I can think of to use a servlet as a front controller is so that it is easier to maintain the web site after it is built. Struts is a good example, where it supplies the front controller and you use an XML file to specify where to direct the request. So, if you later want link A in all your pages to go to page C instead of page B, you don't have to re-edit all of your JSP files. You simply change the XML file.
If you have a lot of duplication in your JSPs, such as checking whether the person is logged in, you can put that in the servlet and don't have to duplicate it all over the place.


We learn by doing, there is no other way.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

If you have a lot of duplication in your JSPs, such as checking whether the person is logged in, you can put that in the servlet and don't have to duplicate it all over the place.

You dont need to duplicate the codes, you can have 1 JSP, and import the page into other JSPs.
Struct is a good example, as well as JSF, both of them are based on Servlets. You may argue that JSP can also read XML config file, however, as the target of 2 technolgoies is different, newer server-side controller technologies will be built on Servlets, since JSP is primarily focus on presentation.
Nick
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: why to use servlet as controller not jsp
 
Similar Threads
servlet is used as controller
Controller in MVC
MVC architecture
MVC - Controller
In MVC