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
Which http method is said to be non-idempotent and idempotent?
What are all the ways for session tracking?
Explain session tracking and its importance?
What are advantages of servlets over cgi?
What is servlet in tomcat?
Explain how to improve Servlet Performance?
What are some advantages of storing session state in cookies?
What are the features added in Servlet 2.5?
Explain get and post.
Why session tracking is needed?
What are the different types of servlets?
How to read request headers from servlets?
What do you mean by the servlet chaining?
What is the use of attribute in servlets?
What is servlet attributes and their scope?