Choice between JSP and Servlet

M

Matt

I am new to JSP and Servlet. I know JSP container will convert JSP to servlet
eventually. But in application development's standpoint, anything can be
done in servlet can also be done in JSP? Some people told me we can
use the combination of both in one application.

My question is in what situation we use JSP alone, servlet alone, or
combination of both??

Please advise. Thanks!!
 
S

Sudsy

Matt said:
I am new to JSP and Servlet. I know JSP container will convert JSP to servlet
eventually. But in application development's standpoint, anything can be
done in servlet can also be done in JSP? Some people told me we can
use the combination of both in one application.

My question is in what situation we use JSP alone, servlet alone, or
combination of both??

Sure, you can incorporate Java code directly into a JSP. But it breaks
one of the cardinal rules, and the reason for the existence of JSP in
the first place.
What you should be trying to do is cleanly separate Java code from
HTML. Use tags in your JSP to access underlying functionality. Don't
use servlets to generate HTML.
Do yourself a favour and don't fall into the trap of trying to do it
all in a JSP while promising that you'll separate things once you're
ready to go live. You'll run out of time and maintenance will become
an absolute nightmare!
Do it right the first time.
 
W

William Brogden

I am new to JSP and Servlet. I know JSP container will convert JSP to
servlet
eventually. But in application development's standpoint, anything can be
done in servlet can also be done in JSP? Some people told me we can
use the combination of both in one application.

My question is in what situation we use JSP alone, servlet alone, or
combination of both??

Please advise. Thanks!!

The usual response to this question is:

Use JSP where the complexity of the HTML is the most important
thing / or where non-programmer HTML authors have to maintain the
site.

Use servlets where computation is the most important thing or
where binary content such as images must be generated.

Many people happily use a mixture.

Bill
 
L

Liz

Sudsy said:
Sure, you can incorporate Java code directly into a JSP. But it breaks
one of the cardinal rules, and the reason for the existence of JSP in
the first place.
What you should be trying to do is cleanly separate Java code from
HTML. Use tags in your JSP to access underlying functionality. Don't
use servlets to generate HTML.
Do yourself a favour and don't fall into the trap of trying to do it
all in a JSP while promising that you'll separate things once you're
ready to go live. You'll run out of time and maintenance will become
an absolute nightmare!
Do it right the first time.
I'm new to jsp myself and am interested, therefore, as an observer.
Is it true that the technique to do the separation is to make java beans?
Then the jsp pages can interact with the java stuff via the bean properties?
 
S

Sudsy

Liz wrote:
I'm new to jsp myself and am interested, therefore, as an observer.
Is it true that the technique to do the separation is to make java beans?
Then the jsp pages can interact with the java stuff via the bean properties?

That's the proper approach, IMHO. The tags (custom or Struts or JSTL)
create scripting variables which adhere to the semantics of JavaBeans,
i.e. they provide accessors/mutators named getXXX/setXXX (where XXX is
the property name).
Pay particular attention to the javax.servlet.jsp.tagext.TagExtraInfo
class. Note that the norm is a tag similar to this:
<taglibURI:tagName id="..." name="..." property="...">
The bean specified by the name attribute is located and the getXXX
method is invoked using the value of the property attribute to replace
XXX. Finally, the returned Object is placed into page scope under the
name specified by the id attribute.
If you want to read an excellent treatise on the power of JSPs and
custom tags then I highly recommend "Advanced JavaServer Pages" by
David M. Geary, ISBN 0-13-030704-1. His region tag library was the
progenitor of Tiles.
Once you see how straight-forward it can be to "do it right", you'll
steer clear of the quagmire of scriptlets and other nasties.
 
J

juli

hai readers,

I think servlets stuff should be learnt first since writing jsp alone will
not make a web development scalable i mean u should embed custom tag from
jsp in to servlets or servlets will looklike html pages wtih less coding
ie programming logics.

Once u get thru servlets +jsp deploy the same application in ejb it will
be perfect scalable application
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top