0% found this document useful (0 votes)
163 views

Advantage of JSP Over Servlet: Unit-VI

The document discusses JSP (JavaServer Pages) technology. Some key points: 1) JSP pages make it easier to create dynamic web applications than servlets by separating programming logic from presentation. JSP pages contain HTML tags as well as special JSP tags. 2) Advantages of JSP over servlets include being an extension of servlets with additional features, easier maintenance by separating logic from presentation, and not needing to recompile on every change. 3) A JSP page is translated into a servlet, which is then compiled and executed to generate the response each time the page is requested. This translation and execution process implements the JSP lifecycle.

Uploaded by

Sathya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Advantage of JSP Over Servlet: Unit-VI

The document discusses JSP (JavaServer Pages) technology. Some key points: 1) JSP pages make it easier to create dynamic web applications than servlets by separating programming logic from presentation. JSP pages contain HTML tags as well as special JSP tags. 2) Advantages of JSP over servlets include being an extension of servlets with additional features, easier maintenance by separating logic from presentation, and not needing to recompile on every change. 3) A JSP page is translated into a servlet, which is then compiled and executed to generate the response each time the page is requested. This translation and execution process implements the JSP lifecycle.

Uploaded by

Sathya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Advanced Java Programming

Unit-VI

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 Tag etc.

Advantage of JSP over Servlet


There are many advantages of JSP over servlet. They are as follows:

1) Extension to Servlet

JSP technology is the extension to servlet technology. We can use all the features of 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 a lot of tags such as action tags, jstl, custom tags etc. that reduces the
code. Moreover, we can use EL, implicit objects etc.

1
Advanced Java Programming

The Anatomy of a JSP Page

A JSP page is simply a regular web page with JSP elements for generating the parts of the page
that differ for each request, as shown in Figure 3.2.

Everything in the page that is not a JSP element is called template text . Template text can really
be any text: HTML, WML, XML, or even plain text. Since HTML is by far the most common
web page language in use today, most of the descriptions and examples in this book are HTML-
based, but keep in mind that JSP has no dependency on HTML; it can be used with any markup
language. Template text is always passed straight through to the browser.

Figure 3-2. Template text and JSP elements

When a JSP page request is processed, the template text and the dynamic content generated by
the JSP elements are merged, and the result is sent as the response to the browser.

JSP Elements

There are three types of elements with JavaServer Pages: directive, action,
and scripting elements.

The directive elements, shown in Table 3.1, are used to specify information about the page itself
that remains the same between page requests, for example, the scripting language used in the
page, whether session tracking is required, and the name of a page that should be used to report
errors, if any.

2
Advanced Java Programming

Table 3-1. Directive Elements

Element Description

<%@ page ... %> Defines page-dependent attributes, such as scripting language, error page, and buffer

JSP Processing

A JSP page cannot be sent as-is to the browser; all JSP elements must first be processed by the
server. This is done by turning the JSP page into a servlet, and then executing the servlet.

Just as a web server needs a servlet container to provide an interface to servlets, the server needs
a JSP container to process JSP pages. The JSP container is often implemented as a servlet
configured to handle all requests for JSP pages. In fact, these two containers—a servlet container
and a JSP container—are often combined into one package under the name web container (as it
is referred to in the J2EE documentation).

A JSP container is responsible for converting the JSP page into a servlet (known as the JSP page
implementation class ) and compiling the servlet. These two steps form the translation phase .
The JSP container automatically initiates the translation phase for a page when the first request
for the page is received. The translation phase takes a bit of time, of course, so a user notices a
slight delay the first time a JSP page is requested. The translation phase can also be initiated
explicitly; this is referred to as precompilationof a JSP page. Precompiling a JSP page avoids
hitting the user with this delay, and is discussed in more detail in Chapter 12.

The JSP container is also responsible for invoking the JSP page implementation class to process
each request and generate the response. This is called the request processing phase. The two
phases are illustrated ...

JSP Application Design with MVC

JSP technology can play a part in everything from the simplest web application, such as an
online phone list or an employee vacation planner, to full-fledged enterprise applications, such as
a human resource application or a sophisticated online shopping site. How large a part JSP plays
differs in each case, of course. In this section, we introduce a design model suitable for both
simple and complex applications called Model-View-Controller (MVC).

3
Advanced Java Programming

MVC was first described by Xerox in a number of papers published in the late 1980s. The key
point of using MVC is to separate components into three distinct units: the Model, the View, and
the Controller. In a server application, we commonly classify the parts of the application as:
business logic, presentation, and request processing. Business logic is the term used for the
manipulation of an application’s data, i.e., customer, product, and order
information. Presentation refers to how the application is displayed to the user, i.e., the position,
font, and size. And finally, request processing is what ties the business logic and presentation
parts together. In MVC terms, the Model corresponds to business logic and data, the View to the
presentation logic, and the Controller to the request processing.

Why use this design with JSP? The answer lies primarily in the first two elements. Remember
that an application data structure and logic (the Model) is typically the most stable part of an
application, ...

Life cycle of a JSP Page

The JSP pages follow these phases:

• Translation of JSP Page


• Compilation of JSP Page
• Class loading (class file is loaded by the class loader)
• Instantiation (Object of the Generated Servlet is created).
• Initialization ( jspInit() method is invoked by the container).
• Request processing ( _jspService() method is invoked by the container).
• Destroy ( jspDestroy() method is invoked by the container).

Note: jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.

4
Advanced Java Programming

As depicted in the above diagram, JSP page is translated into servlet by the help of JSP
translator. The JSP translator is a part of webserver that is responsible to translate the JSP page
into servlet. Afterthat Servlet page is compiled by the compiler and gets converted into the class
file. Moreover, all the processes that happens in servlet is performed on JSP later like
initialization, committing response to the browser and destroy.

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 save 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

5
Advanced Java Programming

Let's see the simple example of JSP, here we are using the scriptlet tag to put java code in the
JSP page. We will learn scriptlet tag later.

1. <html>
2. <body>
3. <% out.print(2*5); %>
4. </body>
5. </html>

It will print 10 on the browser.

How to run a simple JSP Page ?

Follow the following steps to execute this JSP page:

• Start the server


• put the jsp file in a folder and deploy on the server
• visit the browser by the url http://localhost:portno/contextRoot/jspfile e.g.
http://localhost:8888/myapplication/index.jsp

Do I need to follow directory structure to run a simple JSP ?

No, there is no need of directory structure if you don't have class files or tld files. For example,
put jsp files in a folder directly and deploy that folder.It will be running fine.But if you are using
bean class, Servlet or tld file then directory structure is required.

Directory structure of JSP

The directory structure of JSP page is same as servlet. We contain the jsp page outside the WEB-
INF folder or in any directory.

6
Advanced Java Programming

JSP Scriptlet tag (Scripting elements)


1. Scripting elements
2. JSP scriptlet tag
3. Simple Example of JSP scriptlet tag
4. Example of JSP scriptlet tag that prints the user name

In JSP, java code can be written inside the jsp page using the scriptlet tag. Let's see what
are the scripting elements first.

JSP Scripting elements


The scripting elements provides the ability to insert java code inside the jsp. There are three
types of scripting elements:

o scriptlet tag
o expression tag
o declaration tag

JSP scriptlet tag

7
Advanced Java Programming

A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:

1. <% java source code %>

Example of JSP scriptlet tag


In this example, we are displaying a welcome message.

1. <html>
2. <body>
3. <% out.print("welcome to jsp"); %>
4. </body>
5. </html>

Example of JSP scriptlet tag that prints the user name


In this example, we have created two files index.html and welcome.jsp. The index.html file
gets the username from the user and the welcome.jsp file prints the username with the
welcome message.

File: index.html

1. <html>
2. <body>
3. <form action="welcome.jsp">
4. <input type="text" name="uname">
5. <input type="submit" value="go"><br/>
6. </form>
7. </body>
8. </html>

File: welcome.jsp

1. <html>
2. <body>
3. <%
4. String name=request.getParameter("uname");
5. out.print("welcome "+name);
6. %>
7. </form>
8. </body>
9. </html>

JSP expression tag


8
Advanced Java Programming

The code placed within JSP expression tag is written to the output stream of the
response. So you need not write out.print() to write data. It is mainly used to print the
values of variable or method.

Syntax of JSP expression tag


1. <%= statement %>

Example of JSP expression tag


In this example of jsp expression tag, we are simply displaying a welcome message.

1. <html>
2. <body>
3. <%= "welcome to jsp" %>
4. </body>
5. </html>

Note: Do not end your statement with semicolon in case of expression tag.

Example of JSP expression tag that prints current time


To display the current time, we have used the getTime() method of Calendar class. The
getTime() is an instance method of Calendar class, so we have called it after getting the
instance of Calendar class by the getInstance() method.

index.jsp

1. <html>
2. <body>
3. Current Time: <%= java.util.Calendar.getInstance().getTime() %>
4. </body>
5. </html>

Example of JSP expression tag that prints the user name


In this example, we are printing the username using the expression tag. The index.html file
gets the username and sends the request to the welcome.jsp file, which displays the
username.

File: index.jsp

9
Advanced Java Programming

1. <html>
2. <body>
3. <form action="welcome.jsp">
4. <input type="text" name="uname"><br/>
5. <input type="submit" value="go">
6. </form>
7. </body>
8. </html>

File: welcome.jsp

1. <html>
2. <body>
3. <%= "Welcome "+request.getParameter("uname") %>
4. </body>
5. </html>

JSP Declaration Tag


1. JSP declaration tag
2. Difference between JSP scriptlet tag and JSP declaration tag
3. Example of JSP declaration tag that declares field
4. Example of JSP declaration tag that declares method

The JSP declaration tag is used to declare fields and methods.

The code written inside the jsp declaration tag is placed outside the service() method of
auto generated servlet.

So it doesn't get memory at each request.

Syntax of JSP declaration tag

The syntax of the declaration tag is as follows:

1. <%! field or method declaration %>

Difference between JSP Scriptlet tag and Declaration


tag
Jsp Scriptlet Tag Jsp Declaration Tag

The jsp scriptlet tag can only declare variables not The jsp declaration tag can declare variables as

10
Advanced Java Programming

methods. methods.

The declaration of scriptlet tag is placed inside the The declaration of jsp declaration tag is placed o
_jspService() method. the _jspService() method.

Example of JSP declaration tag that declares field


In this example of JSP declaration tag, we are declaring the field and printing the value of
the declared field using the jsp expression tag.

index.jsp
1. <html>
2. <body>
3. <%! int data=50; %>
4. <%= "Value of the variable is:"+data %>
5. </body>
6. </html>

Example of JSP declaration tag that declares method


In this example of JSP declaration tag, we are defining the method which returns the cube
of given number and calling this method from the jsp expression tag. But we can also use
jsp scriptlet tag to call the declared method.

index.jsp
1. <html>
2. <body>
3. <%!
4. int cube(int n){
5. return n*n*n*;
6. }
7. %>
8. <%= "Cube of 3 is:"+cube(3) %>
9. </body>
10. </html>

11

You might also like