what is the difference between Servlet and JSP?Advantage of
JSP over Servelt?Any concept present in JSP which we cant
implement in Servlet?
Answer Posted / prashant bari
 Difference between JSP & Servlet:-
Before moving to the actual discussion let us have glimpse
on following points.
1. HTML, JSP & Servlet-
a. By using HTML only static part of the webpage can
be designed.
b. So there was a need of some other coding option to
create dynamic content for web page. Both servlet & JSP can
be used for this purpose. The way they can be used to
create dynamic content of the web page constitutes one of
the difference between them.
i. Sevlet contain HTML code in java code.
ii. JSP Contain java code in HTTP code.
This arose a question ”When to use which?”
2. When to use which?
There are two more things we need to understand those are
Presentation logic & Business logic.
Presentation logic deals with how the information is
presented.
Business logic usually written in java code contains coding
for business decisions like “if customer will purchase
goods above rupees 300 allow 10 % discount.”
Ideally business logic & presentation logic should be
separate. Why?
a. Modularity & ease of design.
b. Abstraction (why to explorer business logic to
coder)?
3. How JSP & Servlet implement business logic &
Presentation logic?
a. JSP-
i. Presentation logic is defined in JSP file.
ii. Business logic defined in java Beans.
b. Servlet-
i. Static presentation defined in HTML
ii. Dynamic presentation and business logic in servelet
4. Now the answer…
Consider a scenario of retail industry where business i.e
business logic decisions frequently change. If servlet is
used for such web application. For each change in business
logic we will need to recompile those servlets.
But if we use JSP for this the changes in business logic
can be done in Beans without touching presentation logic.
5. Then why to use Servlet?
Answer is
 When performance is important- as we know JSP is
again converted to servlet it will be two step process.
6. Final confusion- as just stated if JSP is also
converted to servlet internally then if change in business
logic needs to recompile servlet again will it not be the
case in JSP?
Answer is no!!! because presentation logic in JSP is
separate from Buisness logic in bean. The code on JSP only
know how to invoke service from bean. So long as the method
name, parameters & return type remains the same the
business logic developer can easily change rest of the
method.
e.g method in business logic
class BeanDemo
{
int area(int a)
{
Int area;
Area=a*a;
Return()
}
}
The method will appear as follows in presentation logic.
BeanDemo bd=new BeanDemo();
Int a=bd.area();
Now if tomorrow if business logic developer change the
method definition to
class BeanDemo
{
int area(int a)
{
Int area;
Area=a*a;
Return()
}
}
Will it affect the presentation logic code? Surely not. It
wont even need to be recompile what will be needed is re
compiling only business logic part i.e BeanDemo class not
the JSP calling the method..
Bottom line-
1. Use JSP for presentation Logic and ideally include
no business logic.
2. Use Beans for Business Logic and ideally include no
presentation logic.
| Is This Answer Correct ? | 2 Yes | 1 No |
Post New Answer View All Answers
What’s the difference between forward() and sendredirect() methods?
How do I support both get and post from the same servlet?
What do you mean by scope object and what are its types?
How to read request headers from servlets?
Explain the role of dispatcherservlet and contextloaderlistener.
What is a cookie What is the difference between session and cookie
how to link html file to sevlet file.one name is textbox,c&c++ is cheakbox,bc&mca is radio button and one submit buttonis ok why in this programme use in servelt file
What is a deployment descriptor?
What is a servlet context object?
The code in a finally clause will never fail to execute, right?
How can you push data from an Applet to a Servlet?
What is servlet name in web xml?
Should I override the service() method?
What is the capacity the doget can send to the server?
Explain the different ways for servlet authentication?