Neal Cabage wrote:
>
> Anyone out there using servlets rather than JSPs?  Why would a person build
> a servlet rather than letting JSP just build one for you.  My initial
> thought is that a pure hand-written servlet might be more efficient if there
> is no actual output.  Perhaps one might also consider it better style for
> the same reason.  This is just speculation however and JSP just seems easier
> and more time efficient...so would there be a good reason for hand-writing
> your own servlets instead?

Depending on who you ask, I'm sure you get different answers. The way I
look at it, though, is that JSP is primarily a tool for generating the
response, that typically consists to a large extent of static markup and
some dynamically rendered markup. For simple web application (and there
are many apps that are simple), you can use JSP also for processing
the request (validating input) and business logic (e.g. database
access).

In all cases, you should avoid putting Java code in the JSP pages. Some
reasons:
* simple syntax errors are hard to detect because your code and
  the code generated by the JSP container get intermixed, so the
  compiler error message is often hard to understand and often
  point at code you didn't write at all,
* debugging a JSP page is harder than debugging a regular Java
  class that you have full control over,

Embed the code is custom actions and JavaBeans instead, and try to use
only JSP tags in the page.

A servlet is definitely a better tool than JSP if you generate a
response with binary data (such as an image); JSP is intended for
text only.

For web applications with more than trivial application logic, the
Model-View-Controller (MVC) model makes a lot of sense, combining
servlets, beans and JSP. Let a servlet act as the Controller (request
processing), beans act as the Model (business logic), and use JSP as
the View (generating the response) using custom actions to render the
result.

If you want to learn more about the MVC model and how to implement
it with servlets, beans and JSP, I suggest you take a look at the
Struts framework, part of the Apache Jakarta project:

  <http://jakarta.apache.org/struts/>

I also talk a lot about different variations of the MVC model in
a web application in my JavaServer Pages book:

  <http://TheJSPBook.com/>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to