Advantages of JSP Over Servlet: Unit - 6
Advantages of JSP Over Servlet: Unit - 6
What is JSP? Explain the advantages of JSP and main component also. How is it differ from
servlet? Give an example to support your answer.
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.
A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet because we can separate
designing and development. It provides some additional features such as Expression Language, Custom Tags, etc.
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.
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.
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.
Disadvantages of JSP
It is hard to trace JSP pages error because JSP pages are translated to servlet.
As JSP output is HTML, it is not rich in features.
It is very hard to debug or trace errors because JSP pages are first translated into servlets before the compilation
process.
Database connectivity is not easy.
JSP pages require more disk space and time to hold JSP pages as they are compiled on the server.
index.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put Java code in the JSP page. We will learn scriptlet
tag later.
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
Servlet JSP
Servlets run faster than JSP. JSP runs slower than servlet as it takes time to
compile the program and convert into servlets.
It is hard to write code in servlet. It's easier to code in JSP compared to servlets.
In MVC architecture, servlet works as a In MVC architecture, JSP works as a view for
controller. displaying output.
It should be use when there is more data JSP is generally used when there is no
processing involved. involvement of much data processing.
There is no custom tag writing facility in servlets. You can easily build custom tags that can directly
call Java beans.
It can accept all protocol requests, including It can only accept HTTP requests.
HTTP.
You can override the service() method. In JSP, you can't override the service() method.
In Servlet, you have to implement both business In JSP, business logic is split from presentation
logic and presentation logic in the single file. logic using JavaBeans.
Modification in Servlet file is a time consuming JSP modification is fast, as you just need to click
due to reloading, recompiling, and restarting the one refresh button.
server.
JSP Declaration
A declaration tag is a piece of Java code for declaring variables, methods and classes. If we declare a variable or method
inside declaration tag it means that the declaration is made inside the servlet class but outside the service method.
We can declare a static member, an instance variable (can declare a number or string) and methods inside the
declaration tag.
JSP Scriptlet
Scriptlet tag allows to write Java code into JSP file.
JSP container moves statements in _jspservice() method while generating servlet from jsp.
For each request of the client, service method of the JSP gets invoked hence the code inside the Scriptlet executes for
every request.
A Scriptlet contains java code that is executed every time JSP is invoked.
Here <%%> tags are scriplets tag and within it, we can place java code.
JSP Expression
Expression tag evaluates the expression placed in it.
It accesses the data stored in stored application.
It allows create expressions like arithmetic and logical.
It produces scriptless JSP page.
Syntax:
JSP Comments
Comments are the one when JSP container wants to ignore certain texts and statements.
When we want to hide certain content, then we can add that to the comments section.
Syntax:
This tags are used to comment in JSP and ignored by the JSP container.
<!—comment -->
The web server needs a JSP engine, i.e, a container to process JSP pages. The JSP container is responsible for intercepting requests
for JSP pages. This tutorial makes use of Apache which has built-in JSP container to support JSP pages development.
A JSP container works with the Web server to provide the runtime environment and other services a JSP needs. It knows how to
understand the special elements that are part of JSPs.
Following diagram shows the position of JSP container and JSP files in a Web application.
JSP Processing
The following steps explain how the web server creates the Webpage using JSP −
As with a normal page, your browser sends an HTTP request to the web server.
The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using
the URL or JSP page which ends with .jsp instead of .html.
The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which
all template text is converted to println( ) statements and all JSP elements are converted to Java code. This code implements
the corresponding dynamic behavior of the page.
The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine.
A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the servlet
produces an output in HTML format. The output is furthur passed on to the web server by the servlet engine inside an HTTP
response.
The web server forwards the HTTP response to your browser in terms of static HTML content.
Finally, the web browser handles the dynamically-generated HTML page inside the HTTP response exactly as if it were a
static page.
All the above mentioned steps can be seen in the following diagram −
Typically, the JSP engine checks to see whether a servlet for a JSP file already exists and whether the modification date on the JSP
is older than the servlet. If the JSP is older than its generated servlet, the JSP container assumes that the JSP hasn't changed and
that the generated servlet still matches the JSP's contents. This makes the process more efficient than with the other scripting
languages (such as PHP) and therefore faster.
So in a way, a JSP page is really just another way to write a servlet without having to be a Java programming wiz. Except for the
translation phase, a JSP page is handled exactly like a regular servlet.