2016-2018
Java Server Pages | Batch
request is determined on-the-fly by a program that executes on the server after the HTTP
request has been received and generates content as a result of the execution
Detailed purpose and major uses of server-side scripting
Insertion of continuously changing content into a web page, for example - weather or
stock quotes. Also, any arbitrary logic can be used to determine certain content will be
shown or not. This purpose and (10) below are the primary purposes of server-side
scripting.
Authentication, authorization and session tracking - although rudimentary authentication
and authorization is supported by most Web servers, anything more than the "BASIC"
http authentication and ACLs (access control lists) over static resources requires server-
side programs. Similarly, handling cookies and keeping information about the session
and/or the user is best handled by server-side scripting,
Template-driven page generation. Including repeated content like header/footers and
navigation menus around the "content area" of a web page.
Personalization and customization of content based on authentication and authorization
defined above in (2). This also includes the serving of content based on the content of the
page (e.g. ads) or the browsing behavior of the user
Dynamic image generation, e.g. page counters, human-readable characters for security,
maps, overlays ete.
Dynamic generation of CSS and Javascript
Generating and reading HTTP headers. Although web servers provide rudimentary
abilities, server-side scripting can best generate cache control and other complex headers.
Handling POST form input - accepting the input of a form and writing it to storage (file
system, database, session etc.). This also includes business transaction commitment
control (ALL or NONE) and input error handling.
Device mapping - generating different types of content (HTML, XML, WML) based on
the user agent that sent the HTTP request.
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 4/342016-2018
Java Server Pages | Batch
10. Retrieval of data in response to query string parameters and insertion into a web page.
This is perhaps the most common purpose of utilizing scripting in generating content as
part of a GET request. e.g. sports statistics, staff list, downloadable files list etc. The data
can be retrieved from a database, file system or other forms of storage.
11. Communication with other programs, libraries and APIs - e.g. sending out e-mail,
handling message queues, LDAP ete.
down of
12. Re-use of persistent business objects. HTTP is stateless, but the setup and tea
business objects has a very high overhead in terms of time and server resources. Server-
side scripting allows us to interact with such re-usable business objects e.g. application
servers, EJBs, .NET services and Web services.
Popular server-side scripting languages - and examples
Before we look at popular server-side scripting languages, we will divide them into three
groups based on how the seripting programs
1. Older, standards-based scripting languages - these include SSI (server-side includes) and
CGI (common gateway interface) and were defined in the original NCSA standards for
web servers.
2, In-process scripting languages like PHP, ASP and Perl (sometimes).
3. Out-of-process scripting languages like JSP and servlets (Java) and XSLT
Another classification is based on whether it is page-centrie or script-centric. A page-
centric language is an HTML page with embedded special tags (SSI and all the *SP
languages) while script-centric are Perl and servlets. Scripts in script-centric languages
can produced multiple "pages" and have to output the entire HTML using program
functions.
Page-centrie scripts are embedded into an HTML page only where dynamic content is
required; but they can also be used to generate the entire content, e.g. images, XML,
headers etc. These usually run in-process and use the filesystem namespace of the web
server.
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 5/342016-2018
Batch
SSI (Server Side Includes)
These are extended comment tags inserted into a static HTML page to include other
pages (templates), variables, and also execute extemal programs and include them in the
input, Any static HTML file defined with a special extension (commonly “ shtml”) forces
properly configured Web server to parse the file before sending and replace the special
tags with the appropriate content. This is perhaps the simplest model of server-side
scripting but surprisingly, itis the essential mechanism of server-side scripting,
CGI (Common Gateway Interface)
This is a mechanism that instructs a properly configured Web server to execute a specific
file and send the output of the execution instead of sending it "as-is" to the client. Any
program (shell scripts, DOS batch files, C programs, Perl) can be executed through this
mechanism, Information about the request, the query string and any form parameters are
sent as environment variables to the executed program. Any output by the executed
program is sent directly back to the browser. It should be noted that the program is
responsible for generating all headers. The most commonly used language for CGT was
Perl, due to its powerful text-handling capabilities.
Perl
This is an interpreted language characterised by its intuitive text handling, loose type-
checking, associative arrays, handy loop constructs and simple file and environment
handling. It was the most popular server-side scripting language for many years and it
supports a modular expansion system 4, A Perl script can be executed through the Perl
Interpreter from the CGI interface (see above) or through a Web server extension that
embeds the Perl Interpreter in the Web Server processes (in-process). For example, see
CGI above. Its main drawback is that it pre-dates the Web and it is difficult to lay out
HTML in the code.
PHP (Hypertext Preprocessor)
This language was developed specifically for Web server-side scripting and its utility has
made it one of the most popular server-side scripting languages. As opposed to Perl, itis
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 6/342016-2018
Java Server Pages | Batch
embedded into a fully laid out HTML page and gives complete control over HTTP
request, response, cookie and session, It contains more robust type-checking (if required)
and can be programmed in an object-oriented way. It is most commonly executed in-
process and its biggest drawback is the lack of memory persistence of business objects.
Pages identified by certain extensions (commonly .phtml, .php, .php3) are parsed by the
Web server and passed on to the PHP plugins that passes the content back to the Web
server. It follows the same directory structure as HTML static pages and images and is
thus very easy to program and maintain. It has an extensive library and API system and
some third-party vendors (Zend etc.) offer accelerators for PHP that show considerable
performance improvement for complex applications.
ASP (Active Server Pages)
This is the Microsoft page-centric solution. It only runs on the IS (Internet Information
Server) although third party implementations on other platforms are available, making it
less proprietary than Cold Fusion below. Like other page-centric languages, it embeds
dynamic constructs into HTML pages:
Cold Fusion
This is a Macromedia page-centric solution, However, instead of having ONE special tag
to embed dynamic content, it defines a number of tags that are parsed by a Web server
plugin in-process, These special tags (in red below) make it very powerful and combined
with Macromedia Web Authoring tools, make it the choice of many corporations.
However, itis proprietary:
ISP/Servlets
- based on Java and J2EE
This is standards-based, popular, hybrid and out-of-proce
standards9 . Although JSPs are page-centric at author-time, they are not parsed by a web
server-plugin. They are compiled into servlets and deployed in a separate Web Container.
The Web server communicates with the web container using sockets. Most web
containers implement a simple web server built into them which are usually not as robust
and scalable as the leading Web servers but are good for testing and debugging
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 7/342016-2018
Java Server Pages | Batch
Servlets are seript-centric and are regular Java programs. The compilation of JSPs into
servlets gives us the best of both worlds (author-time page-centric and compiled out-of
process) and both of these have access to the full suite of Java libraries and APIs. The
‘web container also defines sophisticated authorisation, authentication and URL mapping
techniques that make this an enterprise-level Web development platform, Due to its being
out of process, se
sion objects and business objects can be cached and re-used by
multiple HTTP requests.
5.3 JSP & JAVA 2 ENTERPRISE EDITION
Java Server Pages (JSPs) are Web pages coded with an extended HTML that makes it
possible to embed Java code in a Web page. JSPs can call custom Java classes,
called tagiibs, using HTML-like tags. The WebLogic appe compiler weblogic.appe
generates JSPs and validates descriptors. You can also precompile JSPs into the WEB-
INFiclasses/ directory or as a JAR file under WEB-INF/lib/ and package the servlet
class in the Web archive to avoid compiling in the server. Servlets and ISPs may
require additional helper classes to be deployed with the Web application.
JSPs are a Sun Microsystems specification for combining Java with HTML to provide
dynamic content for Web pages. When you create dynamic content, JSPs are more
convenient to write than HTTP servlets because they allow you to embed Java code
directly into your HTML pages, in contrast with HTTP servlets, in which you embed
HTML inside Java code. JSP is part of the Java 2 Enterprise Edition (J2EF).
ISPs enable you to separate the dynamic content of a Web page from its presentation.
It caters to two different types of developers: HTML developers, who are responsible
for the graphical design of the page, and Java developers, who handle the development
of software to create the dynamic content.
Because JSPs are part of the J2EE standard, you can deploy JSPs on a variety of
platforms, including WebLogic Server. In addition, third-party vendors and application
developers can provide JavaBean components and define custom JSP tags that can be
referenced from a JSP page to provide dynamic content.
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 8/342016-2018
Batch
What You Can Do with JSPs
+ Combine Java with HTML to provide dynamic content for Web pages.
= Call custom Java classes, called tag! ibs, using HTML-like tags
+ Embed Java code directly into your HTML pages, in contrast with HTTP servlets,
in which you embed HTML inside Java code.
= Separate the dynamic content ofa Web page fiom its presentation.
Overview of How JSP Requests Are Handled
WebLogic Server handles JSP requests in the following sequence:
1, A browser requests a page with a . sp file extension from WebLogic Server.
2. WebLogic Server reads the request.
3. Using the JSP compiler, WebLogic Server converts the JSP into a servlet class
that implements the javax. sezvlet.4sp.Tap2age interface, The JSP file is compiled
only when the page is first requested, or when the JSP file has been changed.
Otherwise, the previously compiled JSP servlet class is re-used, making subsequent
responses much quicker.
It is also possible to invoke the JSP compiler directly without making a request from
a browser. Because the ISP compiler creates a Java servlet as its first step, you can
look at the Java files it produces, or even register the generated Jspeace servlet class
as an HTTP servlet.
5.4 JSP FUNDAMENTALS: WRITING YOUR FIRST JSP
ISPs were originally designed around the model of embedded server-side scripting tools
such as Microsoft Corporation's ASP technology; however, JSPs have evolved to focus
on XML elements, including custom-designed elements, or custom tags, as the principal
method of generating dynamic web content.
Prepared by Dr.S. Manju Priya, Department of CS, CA & IT, KAHE Page 9/342016-2018
Java Server Pages | Batch
ISP files typically have a jsp extension, as in mypage.jsp. When a client requests the JSP.
page for the first time, or if the developer precompiles the JSP, the web container
translates the textual document into a servlet,
‘A JSP compiler (such as Tomeat's Jasper component) automatically converts the text-
based document into a servlet. The web container creates an instance of the servlet and
makes the servlet available to handle requests. These tasks are transparent to the
developer, who never has to handle the translated servlet source code (although they can
examine the code to find out what's happening behind the scenes, which is always
instructive),
The sample JSP program below, shows a JSP that displays the current date and time. The
example JSP shows how to import and use a custom tag library,. The code also uses
thejspruseBean standard action, a built-in XML element that you can use to create a new
Java object for use in the JSP page. Here are the basic steps for writing a JSP:
1. Open up a text editor, or a programmer's editor that offers JSP syntax
highlighting,
2. If you are developing a JSP for handling HTTP requests, then input the HTML
code just as you would for an HTML file.
3, Include any necessary JSP directives, such as the teglib directive in example
below , at the top of the file. A directive begins with the <%as.
4, Type in the standard actions or custom tags wherever they are needed.
5. Save the file with a jsp extension in the directory you have designated for JSPs.
A typical location is the top-level directory of a web application that you are
developing in your filesystem.
Example: A JSP file that displays the date
<%-- use the ‘taglib’ directive to make the JSTL 1.0 core tags available; use the uri
“hitp://java.sun.com/sp/jstVcore" for JSTL 1.1 ~%>
Prepared by Dr.S. Manju Priya, Department of CS, CA & IT, KAHE Page 10/342016-2018
Java Server Pages | Batch
<%@taglib uri~"http:/java.sun.comjstV/core" prefix~"e" %>
First ISP
today's date
5.5 Tag Convensions
The JSP tags fall into two basic categories: scripting-oriented tags inspired by ASP, and a
fall set of tags based on the Extensible Markup Language, (XML).
Scripting-oriented tags
‘The ASP-derived tags are easily recognized by their delimiters. They all start with the
characters . An additional character may appear after the initial Note that all these tags
are self-contained. All of the information relevant to the tag, and all of the data it will act,
on, is contained within the individual tags themselves. In contrast, many HTML tags
appear in pairs, For example, the and tags have the effect of italicizing any text they
contain. The contained text is referred to as the body of its containing tags. None of these
scripting-oriented JSP tags have bodies.
Prepared by Dr.S. Manju Priya, Department of CS, CA & IT, KAHE Page 11/342016-2018
Batch
XML-based tags
The second type of JSP tag follows XML syntax and conventions. XML syntax is very
similar to HTML, but adds a few rules which remove some of the vagueness of its sister
language. For example, XML tags are case sensitive, XML requires that all attribute
values appearing within a tag must be quoted, using either single or double quotes. (In
HTML, quotes around attribute values are optional, unless the attribute value contains
white-space characters.) XML also makes a distinction between tags within the document
that contain a body, and those that do not. Specifically, a tag which does not contain a
body uses < as its opening delimiter, and /> as its closing delimiter. For example,
5.6 Running JSP
Although the JSP specification does not mandate any one specific approach for
implementing JavaServer Pages, it is currently the case that all major JSP
implementations are based on servlets. As a first step in understanding how JSPs work,
then, it is helpful to understand how servlets work
As already mentioned, servlets are a Java-based analog to CGI programs, implemented
by means of a servlet container associated with an HTTP server. A set of URLs and/or
URL patterns is specified as being handled by the servlet container, so that whenever a
request for a URL matching this set is received by the HTTP server, that request is
forwarded to the servlet container for processing. For example, the URL
http:/server/account/login might be mapped tothe _servlet__ class
com taglib. wdjsp.fundamentals.LoginServlet. When the HTTP server receives a request
for this URL, the server forwards this request to the servlet container, which in turn
forwards it to an instance of the LoginServlet class
The forwarding of requests is accomplished by packaging all of the request data-URL,
origin of the request, parameters and parameter values, and so forth into a Java object. A
similar Java object is constructed representing the response. This response object has
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 12/342016-2018
Java Server Pages | Batch
methods for setting the status code of the response, and for accessing the output stream
whieh
hold the results of processing the request. The servlet classes are responsible
for defining service methods to handle the various types of HTTP requests, including
adoGet () method for handling HTTP GET requests and acoPost() method for
handling HTTP POST requests. The objects constructed by the servlet container to
represent a single request and its corresponding response are passed as arguments to these
methods, which are then called by the servlet container on a per-request basis.
Given a request object and a response object, the service method accesses the properties
of the request and performs the appropriate computations on this data in order to
construct its reply. The HTML that comprises that reply is written to the output stream
associated with the response object. After the service method has finished running, the
servlet container sends the contents of the response object back to the HTTP server,
which in tum sends the response back to the Web browser which submitted the request in
the first place. Multiple simultaneous requests for a servlet are handled by running each
call to the servlet’s service methods in a separate thread.
JavaServer Pages
From this description, you can begin to imagine how this approach might be extended to
support JavaServer Pages. Affer all, JSP execution starts with a request for a JSP page,
processing is done on the JSP tags present on the page in order to generate content
dynamically, and the output of that processing, combined with the page's static HTML,
must be returned to the Web browser. By adding a few extra steps to the basic servlet
process, however, performance can be improved considerably.
The primary component of a servlet-based implementation of JavaServer Pages is a
special servlet often referred to as the page compiler. The container is configured to call
this servlet for all requests with URLs that match the JSP file extension, and it is the
presence of this servlet and its associated Java classes that turns a servlet container into a
ISP container. As its name suggests, the task of this servlet is not just finding JSP pages
in response to such requests, but actually compiling them: each JSP page is compiled into
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 13/342016-2018
Java Server Pages | Batch
a page-specific servlet whose purpose is to generate the dynamic content specified by the
original JSP document.
‘Thus, whenever the HTTP server receives a request for a URL corresponding to a JSP,
that request is sent to the JSP container, which invokes the page compiler servlet to
handle the request. If this is the first time a request has been received for a particular JSP
file, this servlet compiles the JSP file into a servlet.
To compile @ page, the JSP page compiler parses through its contents, looking for ISP
tags. As it parses the file, it translates its contents into the equivalent Java source code
which, when executed, will generate the output indicated by the contents of the original
file. Static HTML is translated into Java strings, which will be written unmodified and in
their original sequence into an output stream. JSP tags are translated into Java code for
generating dynamic content: Bean tags are translated into the corresponding object and
property calls, while scripting elements are transferred as is. This code will be mixed in
with the output of the original static HTML, so that the dynamic content is inserted into
the output in the correct location. This source code is then used to write the service
methods for a servlet, such that running it for a request has the effect of producing the
content specified by the original JSP file, Once all the servlet code has been constructed,
the page compiler servlet calls the Java compiler to compile this source code and add the
resulting Java class file to the appropriate directory in the JSP container’s class path,
Once the compiled JSP page servlet is
in place, the page compiler servlet then invokes
this new servlet to generate the response for the original request. Of course, this parsing,
code generation, and compiling incurs quite a bit of overhead, Fortunately, these steps are
required only the first time a request for a given JSP page is received. All subsequent
requests can be passed directly to the already-compiled page servlet for immediate
processing
As long as the contents of the original JSP page remain unchanged, there is no need to
generate a new servlet, since the Java code corresponding to those contents remains the
same. For this reason, the very first step taken by the JSP page compiler when it receives
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 14/342016-2018
Java Server Pages | Batch
a request for a JSP is to check the time stamp for the JSP file corresponding to the
requested URL, to determine when that file was modified or created. The page compiler
will also check the time stamp on the compiled servlet for this JSP page. If no compiled
servlet is found, or if the time stamp on the JSP file is more recent than the one on the
compiled page servlet, then a new servlet must be generated. This means that the (new or
modified) JSP file must be parsed and translated into source code, and this new source
code must be compiled. If the compiled servlet is newer than the JSP file, however, no
new compilation is required and control can be transferred directly to the servlet to finish
processing the request, saving considerable time. So while the first request for a new or
recently modified JSP page will be slow, all later requests go straight to the compiled
servlet for response generation.
This process is summarized in flowchart form in Figure 1, where Web browser requests
are received by the HTTP server, and JavaServer Pages requests are routed to the page
compiler servlet running in the JSP container. The JSP container then checks whether or
not the servlet for the requested JSP page is up-to-date: Does a compiled servlet exist for
this page, and, if so, is it newer than the current contents of the ISP page? If not, the JSP
container must go through the process of parsing the page, generating the source code,
and compiling it. The newly compiled servlet is then loaded into the servlet container. If
the JSP page servlet is current, then the JSP container needs to make sure that the servlet
is currently loaded, since it may have been unloaded after its original creation due to lack
of use. In either case, control may then be transferred from the page compiler servlet to
the JSP page servlet, which then handles the request. The response is generated by the
ISP page servlet and routed back to the HTTP server, for return to the Web browser
Prepared by Dr.S. Manju Priya, Department of CS, CA & IT, KAHE Page 15/342016-2018
Batch
‘SP Container
Page Compiler Serviet
. There is
also and Extendable Markup Language (XML) version of JSP tags, which are formatted
as .
In JSP tags can be divided into 5 different types. These are:
1. Comment Tag: A comment tag opens with <%- and closes with -%>, and is
followed by a comment that usually describes the functionality of statements that
follow the comment tag,
2. Directives tag: In the directives we can import packages, define error handling pages
or the session information of the JSP page.
3. Declarations tag:This tag is used for defining the functions and variables to be used
in the JSP.
4. Seriplets: In this tag we can insert any amount of valid java code and these codes are
placed in_jspService method by the JSP engine.
5. Expressions: An expression tag opens with <%= and is used for an expression
statement whose result replaces the expression statement whose result replaces the
expression tag when the JSP virtual engine resolves JSP tags. An expression tags
close with %>
5.9 JSP Directives
Syntax of JSP directives is
<%! ifjava codes %>
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 18/342016-2018
Batch
Java Server Pag
ISP Declaratives begins with <%6! and ends %> with .We can embed any amount of java
code in the JSP Declaratives. Variables and functions defined in the declaratives are class
level and can be used anywhere in the JSP page
Where directive may be:
* page: page is used to provide the information about it
Example: <%@page language="java" %>
* include: inclide is used to include a file in the JSP page.
Example:<%@ include file="/header.jsp" %>
‘+ taglib: taglib is used to use the custom tags in the JSP pages (custom tags allows
us to defined our own tags)
Example: <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>
and attribute may be:
© language="java"
This tells the server that the page is using the java language. Current JSP
specification supports only java language.
Example:
+ extends="mypackage.myclass"
This attribute is used when we want to extend any class. We can use comma(,) to
import more than one packages.
Example:
<%@page language"java"import="java.sql.*,mypackage.myclass" %>
* session="true"
‘When this value is true session data is available to the JSP page otherwise not. By
default this value is true.
Example: <%@page language-"java" session~"true" %>
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 19/342016-2018
Batch
© errorPage="error.jsp"
errorPage is used to handle the un-handled exceptions in the page.
Example: <%@page language~"java" session~"true" errorPage~"error,jsp""4>
© contentType="text/html;charset=ISO-8859-1"
Use this attribute to set the MIME type and character set of the JSP.
Example:<%@page language="java" session="true"_contentType~
charset-ISO-8859-1" %>
‘text/html;
+ errorPage="error.jsp"
errorPage is used to handle the un-handled exceptions in the page
Example: <%@page language="java" session="true" errorPage="error,jsp"%>
+ contentType="text/html;charset=ISO-8859-1"
Use this attribute to set the MIME type and character set of the JSP.
Example:<%@page language="java" session="true" _contentType="text/html;
charset=ISO-8859-1" %>
Example:
<%@page content Type="text/html" %>
<%!
int cnt=0;
private int getCount(){
‘increment ent and return the value
ent;
return ent;
}
Values of Cnt are:
Prepared by Dr.S.Manju Priya, Department of CS, CA & IT, KAHE Page 20/342016-2018
Batch
<%=getCount()%>
<%-getCount()%>
<%=getCount()%>
5.10 SCRIPTING ELEMENTS
JSP scripting element are enclosed within <3 ...... >, similar to other server-side
scripts such as ASP and PHP. To print "<2", use escape sequence "<\s""
JSP Comment <%:
>
commen
JSP comments <$-- 35P coments --~%> are ignored by the JSP engine. For example,
<% ~ anything but a closing tag here will be ignored
Note that HTML comment is InstagramFacebook FacebookPinterest Pinterest