Advantage of JSP Over Servlet: Unit-VI
Advantage of JSP Over Servlet: Unit-VI
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.
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.
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 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
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.
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
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 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, ...
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.
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>
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.
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
In JSP, java code can be written inside the jsp page using the scriptlet tag. Let's see what
are the scripting elements first.
o scriptlet tag
o expression tag
o declaration tag
7
Advanced Java Programming
A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:
1. <html>
2. <body>
3. <% out.print("welcome to jsp"); %>
4. </body>
5. </html>
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>
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.
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.
index.jsp
1. <html>
2. <body>
3. Current Time: <%= java.util.Calendar.getInstance().getTime() %>
4. </body>
5. </html>
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>
The code written inside the jsp declaration tag is placed outside the service() method of
auto generated servlet.
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.
index.jsp
1. <html>
2. <body>
3. <%! int data=50; %>
4. <%= "Value of the variable is:"+data %>
5. </body>
6. </html>
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