0% found this document useful (0 votes)
99 views6 pages

Advantages of JSP Over Servlet: Unit - 6

JSP (JavaServer Pages) is a technology that helps create dynamic web pages based on HTML, XML, or other document types. It allows embedding Java code in web pages to interact with the underlying application logic, databases, and APIs. Some key points: - JSP is an extension to servlets that provides additional functionality like expression language, JSTL tags, and easier separation of design and logic for easier maintenance. - A JSP page consists of HTML tags and JSP tags. The JSP tags like scriptlets, declarations, expressions allow embedding Java code in the page. - JSP pages get compiled into servlets. The JSP container handles requests, compiles the JSP into

Uploaded by

Shivansh tomar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views6 pages

Advantages of JSP Over Servlet: Unit - 6

JSP (JavaServer Pages) is a technology that helps create dynamic web pages based on HTML, XML, or other document types. It allows embedding Java code in web pages to interact with the underlying application logic, databases, and APIs. Some key points: - JSP is an extension to servlets that provides additional functionality like expression language, JSTL tags, and easier separation of design and logic for easier maintenance. - A JSP page consists of HTML tags and JSP tags. The JSP tags like scriptlets, declarations, expressions allow embedding Java code in the page. - JSP pages get compiled into servlets. The JSP container handles requests, compiles the JSP into

Uploaded by

Shivansh tomar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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.

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.

Disadvantages of JSP

Here are cons/drawbacks for using 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.

The Lifecycle of a JSP Page


The JSP pages follow these phases:
o Translation of JSP Page

o Compilation of JSP Page

o Classloading (the classloader loads class file)

o Instantiation (Object of the Generated Servlet is created).

o Initialization ( the container invokes jspInit() method).

o Request processing ( the container invokes _jspService() method).

o Destroy ( the container invokes jspDestroy() method).

Creating a simple JSP Page


To create the first JSP page, write some HTML code as given below, and save it by .jsp extension. We have saved this file as
index.jsp. Put it in a folder and paste the folder in the web-apps directory in apache tomcat to run the JSP page.

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>  

It will print 10 on the browser.

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.

Servlet is a java code. JSP is a HTML-based code.

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, by default, session management is not In JSP, session management is automatically


enabled, user has to enable it explicitly. enabled.

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.

Main component of JSP

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.

Syntax of declaration tag:

<%! Dec var %>

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.

Syntax of Scriptlet tag:

<% java code %>

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:

<%= expression %>

Here the expression is the arithmetic or logical expression.

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:

<% -- JSP Comments %>

This tags are used to comment in JSP and ignored by the JSP container.

<!—comment -->

This is HTML comment which is ignored by browser

How the dynamic content is is generated in JSP? Explain.


(OR)

Describe the architecture of JSP.

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.

You might also like