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 do you mean by session tracking?
What do you mean by httpservlet and how it is different from the genericservlet?
Why session tracking is needed?
Can we use threads in Servlets?
How the servlet is loaded?
When a servlet accepts a call from a client, it receives two objects. What are they?
What is webservlet?
What's the advantages using servlets than using cgi?
Which exception is thrown if the servlet is not initialized properly?
Is dispatcher servlet a singleton?
What do you understand by mime type?
What is servletconfig?
What is the use of servlet wrapper classes?
What is Servlets and explain the advantages of Servlet life cycle?
Explain their methods? Tell me their parameter names also have you used threads in servlet?