2. DYNAMIC WEB PAGES
• dynamic Web pages can display different content from the same source code.
• Dynamic Web pages are capable of producing different content for different
visitors from the same source code file.The website can display different
content based on what operating system or browser the visitor is using,
whether she is using a PC or a mobile device, or even the source that referred
the visitor.
• A dynamicWeb page is not necessarily better than a static Web page.The two
simply serve different purposes.
4. INTRODUCTION TO JSP
• Java Server Pages allow special tags and Java code to be embedded in HTML files. These
tags and code are processed by the Web server to dynamically produce a standard HTML
page for the browser.
• Allows fast development and testing.
• JSPs combine static markup (HTML, XML) with special dynamic scripting tags. May also
produce XML documents, instead of HTML.
• A JSP page has the extension .jsp; this signals to the web server that the JSP engine will
process elements on this page.
•
4
5. JAVA SERVER PAGES
• JSP technology is an extension/wrapper over the Java servlet technology.
(Servlets are pure Java programs.They introduce dynamism into web pages by using
programmatic content.)
• JSP pages are easier to maintain then a Servlet. JSP pages are opposite of Servlets. Servlet
adds HTML code inside Java code while JSP adds Java code inside HTML. Everything a Servlet
can do, a JSP page can also do it.
• Two major components of JSP:
– Static content: provided by HTML or XML
– Dynamic content: generated by JSP tags and scriplets written in Java language to
encapsulate the application logic.
6. ADVANTAGE OF JSP
• Easy to maintain
• High Performance and Scalability.
• JSP is built on Java technology, so it is platform independent.
7. DEVELOPMENT OF JSP
• Java Server Pages were developed as a response to Microsoft’s Active Server Pages (ASP). The
main differences are that ASP only runs on Microsoft IIS and PersonalWeb Servers, and JSP has
user-defined tags.
• Development dates: (Note that JSP is built on top of Servlets)
– Servlet 2.1 Jan. 99
– JSP 1.0 June 99
– Source code released to Apache to developTomcat server November 99
– Servlet 2.2 and JSP 1.1 (J2EE1.2) December 99
7
8. HOW TO PREPARE AND RUN THE
EXAMPLES?
• Simple JSPs can be typed into .jsp type files using editor.
• Create a directory called JSPExamples in the public_html directory of J2EE. Store the
example JSPs here.
• Start the J2EE server.
• Run the JSP from your browser using the command:
• http://localhost:8000/JSPExamples/xyz.jsp
• For complex examples with actions and beans you will have to create web component
(WAR).
9. JAVA SERVER PAGES (JSP)
Client’s
Computer
Server
1.Browser requests HTML
7. Server sends HTML
back to browser
servlet
servlet
class 5.The servlet
runs and
generates
HTML
Java Engine
6. Java Engine sends HTML to server
2. Server sends requests to Java Engine
3. If needed, the Java Engine
reads the .jsp file
4.The JSP is turned into a
servlet, compiled, and loaded
Bean
Servlets are pure Java programs.They introduce dynamism into web pages by using programmatic content.
10. JSP TAGS
• Declaration: variable declaration
<%! int age = 56 %>
• Directive: ex: import classes
<%@ page import = “java.util.*” %>
• Scriplet: Java code
<% if password(“xyz”) {
%>
<H1>Welcome <H1>
• Expression: regular expression using variables and
constants
– <%= param[3]+4 %>
• Action: <jsp:usebean name =“cart”
class=“com.sun.java.Scart”
11. SCRIPTING
• JSP Scripting element are written inside <% %> tags.
• These code inside <% %> tags are processed by the JSP engine during translation of the JSP
page.
• Any other text in the JSP page is considered as HTML code or plain text.
13. STANDARD TAG (ACTION ELEMENT)
• Standard actions are well known tags that affect the run time behavior of the JSP and the response
sent back to the client.
• JSP specification provides Standard(Action) tags for use within JSP pages.These tags are used to
remove or eliminate scriptlet code from JSP page because scriplet code are technically not
recommended nowadays.
• It's considered to be bad practice to put java code directly inside your JSP page.
• Standard tags begin with the jsp: prefix.There are many JSP Standard Action tag which are used to
perform some specific task.
15. JSP DIRECTIVE TAG
DirectiveTag gives special instruction toWeb Container at the time of page translation.
The jsp directives are messages that tells the web container how to translate a JSP page into the
corresponding servlet.
Directive tags are of three types: page, include and taglib.
16. PAGE DIRECTIVE
• The Page directive defines a number of page dependent properties which communicates with the Web Container at the time
of translation.
• The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code page
directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page.
• import attribute- defines the set of classes and packages that must be inported
• language attribute- defines scripting language to be used
• extends attribute- defines the class name of the superclass of the servlet class
• session attribute- whether the JSP page is participating in an HTTP session.
• isThreadSafe attribute- declares whether the JSP is thread-safe
• isErrorPage attribute-whether the current JSP Page represents another JSP's error page.
• errorPage attribute- indicates another JSP page that will handle all the run time exceptions thrown by current JSP page.
• contentType attribute- defines the MIME type for the JSP response.
• autoFlush attribute- defines whether the buffered output is flushed automatically
• buffer attribute- defines how buffering is handled by the implicit out object.
17. INCLUDE DIRECTIVE
• The include directive tells the Web Container to copy everything in the
included file and paste it into current JSP file. Syntax of include directive.
<%@ include file="filename.jsp" %>
The included file can be:
1. JSP file,
2. HTML file,
3. text file.
18. EXAMPLE OF INCLUDE DIRECTIVE
welcome.jsp
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<%@ include file="header.jsp" %>
Welcome, User
</body>
</html>
header.jsp
<html>
<body>
<img src="header.jpg" alt="This is Header
image" / >
</body>
</html>