Distinguish between JSP And Servlet?
Ans:
Difference between Servlet and JSP
Servlet JSP
1)Servlet is a java code. JSP is a html based code.
2)Writing code for servlet is harder than JSP as
JSP is easy to code as it is java in html.
it is html in java.
3)Servlet plays a controller role in MVC JSP is the view in MVC approach for showing
approach. output.
JSP is slower than Servlet because the first step
4)Servlet is faster than JSP. in JSP lifecycle is the translation of JSP to java
code and then compile.
5)Servlet can accept all protocol requests. JSP only accept http requests.
6)In Servlet, we can override the service()
In JSP, we cannot override its service() method.
method.
7)In Servlet by default session management is In JSP session management is automatically
not enabled, user have to enable it explicitly. enabled.
8)In Servlet we have to implement everything
In JSP business logic is separated from
like business logic and presentation logic in
presentation logic by using javaBeans.
just one servlet file.
9)Modification in Servlet is a time consuming JSP modification is fast, just need to click the
task because it includes reloading, recompiling refresh button.
and restarting the server.
State the steps are involved in the JSP life cycle and
draw diagram of life cycle of JSP page.
Ans:
Life cycle of JSP
A Java Server Page life cycle is defined as the process that started with its creation which later
translated to a servlet and afterward servlet lifecycle comes into play. This is how the process
goes on until its destruction.
Following steps are involved in the JSP life cycle:
Translation of JSP page to Servlet
Compilation of JSP page(Compilation of JSP into test.java)
Classloading (test.java to test.class)
Instantiation(Object of the generated Servlet is created)
Initialization(jspInit() method is invoked by the container)
Request processing(_jspService()is invoked by the container)
JSP Cleanup (jspDestroy() method is invoked by the container)
We can override jspInit(), jspDestroy() but we can’t override _jspService() method.
Define JSP? State the Advantages of JSP over
Servlet.
Ans:
JSP technology is used to create web application just like Servlet technology. It can be
thought of as an extension to Servlet because it provides more functionality than servlet such
as expression language, JSTL, etc.
Advantages of JSP over Servlet
There are many advantages of JSP over the Servlet. They are as follows:
1) Extension to Servlet
JSP technology is the extension to Servlet technology. We can use all the features of the
Servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression
language and Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with
presentation logic. In Servlet technology, we mix our business logic with the presentation
logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet
code needs to be updated and recompiled if we have to change the look and feel of the
application.
4) Less code than Servlet
In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the
code. Moreover, we can use EL, implicit objects, etc.
Explain JSP MVC Development Model with neat
diagram.
Ans:
MVC in JSP
MVC stands for Model View and Controller. It is a design pattern that separates the
business logic, presentation logic and data.
Controller acts as an interface between View and Model. Controller intercepts all the
incoming requests.
Model represents the state of the application i.e. data. It can also have business logic.
View represents the presentaion i.e. UI(User Interface).
Advantage of MVC (Model 2) Architecture
Navigation Control is centralized
Easy to maintain the large application
Compare Servlet and JSP?(4 Marks)
Difference between Servlet and JSP
Servlet JSP
1) Servlet is a java code. JSP is a html based code.
2) Writing code for servlet is harder than JSP
JSP is easy to code as it is java in html.
as it is html in java.
3) Servlet plays a controller role in MVC JSP is the view in MVC approach for showing
approach. output.
JSP is slower than Servlet because the first step
4) Servlet is faster than JSP. in JSP lifecycle is the translation of JSP to java
code and then compile.
5) Servlet can accept all protocol requests. JSP only accept http requests.
6) In Servlet, we can override the service()
In JSP, we cannot override its service() method.
method.
7) In Servlet by default session management is In JSP session management is automatically
not enabled, user have to enable it explicitly. enabled.
8) In Servlet we have to implement everything In JSP business logic is separated from
like business logic and presentation logic in
just one servlet file. presentation logic by using javaBeans.
9) Modification in Servlet is a time consuming
JSP modification is fast, just need to click the
ask because it includes reloading, recompiling
refresh button.
and restarting the server.
Explain with suitable diagram Life cycle of JSP.
Ans:
Life cycle of JSP 2M
A Java Server Page life cycle is defined as the process that started with its creation which later
translated to a servlet and afterward servlet lifecycle comes into play. This is how the process
goes on until its destruction.
Translation of JSP page to Servlet : 2M
This is the first step of the JSP life cycle. This translation phase deals with the Syntactic
correctness of JSP. Here test.jsp file is translated to test.java.
Compilation of JSP page :
Here the generated java servlet file (test.java) is compiled to a class file (test.class).
Classloading :
Servlet class which has been loaded from the JSP source is now loaded into the container.
Instantiation :
Here an instance of the class is generated. The container manages one or more instances by
providing responses to requests.
Initialization :
jspInit() method is called only once during the life cycle immediately after the generation of
Servlet instance from JSP.
Request processing :
_jspService() method is used to serve the raised requests by JSP. It takes request and response
objects as parameters. This method cannot be overridden.
JSP Cleanup :
In order to remove the JSP from the use by the container or to destroy the method for servlets
jspDestroy()method is used. This method is called once, if you need to perform any cleanup task
like closing open files, releasing database connections jspDestroy() can be overridden.
What is JSP? Advantages of JSP over Servlet.
Ans: 1M
JSP technology is used to create web application just like Servlet technology. It can be thought of
as an extension to Servlet because it provides more functionality than servlet such as expression
language, JSTL, etc.
Advantages of JSP over Servlet 3M
There are many advantages of JSP over the Servlet. They are as follows:
1) Extension to Servlet
JSP technology is the extension to Servlet technology. We can use all the features of the Servlet
in JSP. In addition to, we can use implicit objects, predefined tags, expression language and
Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with presentation
logic. In Servlet technology, we mix our business logic with the presentation logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet code
needs to be updated and recompiled if we have to change the look and feel of the application.
4) Less code than Servlet
In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the code.
Moreover, we can use EL, implicit objects, etc.
Explain Model View Controller in JSP ?With neat
Diagram.
Ans:
MVC in JSP
MVC stands for Model View and Controller. It is a design pattern that separates the business
logic, presentation logic and data.
Controller acts as an interface between View and Model. Controller intercepts all the incoming
requests.
Model represents the state of the application i.e. data. It can also have business logic.
View represents the presentaion i.e. UI(User Interface).
Advantage of MVC (Model 2) Architecture
Navigation Control is centralized
Easy to maintain the large application