Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

25-Sep-21

Java Server Pages Problem with Servlets


• Written in Java. Therefore cluttered with
numerous print statements while building
html.
• Too much emphasis on the programming
language to create a simple GUI.

JSP JSP…
• Uses regular HTML for most of the page. • A JSP page typically consists of static
• Servlet code is marked with special tags. HTML components, special JSP tags and
• The entire JSP page gets translated into a optional snippets of code written in java
servlet (once) and for each request, it is language, known as “Scriptlets”.
the servlet which actually gets invoked. • Much easier to write and maintain.
• It is a standard extension defined on top of
servlets.

JSPs.. JSPs…
• The logic to generate dynamic content is • Two phases of operation – a translation
kept separate from the static presentation phase and a request processing phase.
by encapsulating it in external • The translation phase is carried out only
components. once (unless the JSP changes), while the
request processing phase will occur on
• Portable – can be moved across platforms every request.
and web-servers without any changes.
• The first request for the JSP results in the
• Completely leverage the servlet API. translation phase where the JSP is
converted into a servlet.

1
25-Sep-21

JSPs JSP Lifecycle


1. The user goes to a JSP page (ending with .jsp). The web
browser makes the request via the Internet.
• At request phase, the servlet is used to 2. The JSP request gets sent to the Web server.
process the request. The actual JSP is 3. The Web server recognises that the file required is
nowhere in picture during the request special (.jsp), therefore passes the request to the JSP
Servlet Engine.
phase. 4. If the JSP file has been called the first time, the JSP file
is parsed, otherwise go to step 7.
5. The next step is to generate a special Servlet from the
JSP file. All the HTML required is converted to println
statements.
6. The Servlet source code is compiled into a class.
7. The Servlet is instantiated, calling the init and service
methods.
8. HTML from the Servlet output is sent via the Internet.
9. HTML results are displayed on the user’s web browser.

JSP Lifecycle… JSP Lifecycle…

JSP Architecture… JSP Syntax


• JSP program consists of a combination of
HTML tags and JSP tags.
• A JSP tag begins with <% and ends with
%>.
• Tags are embedded into the HTML
component of a JSP program and are
processed by the JSP virtual engine such
as Tomcat.

2
25-Sep-21

JSP syntax… JSP tags - comment


• Java code associated with JSP tags are • A comment tag opens with a “<%--” and
executed when encountered by tomcat. ends with a “--%>”
The results are sent to the browser. • The actual comment falls in between these
• Five types of tags – comment tag, two tags.
declaration statement tag, directive tag,
expression tag and the scriptlet tag.

JSP tags - declaration JSP tags – directive


• The declaration tag begins with a “<%!” • A Directive tag opens with a “<%@”.
and ends with a “%>”. • It is a command to the JSP engine to
perform specific tasks such as importing a
• Embedded between these two tags are java package.
declarations of variables, objects, methods • The tag ends with “%>”.
etc that should be available to other • Three common directives – import, include
components of the JSP program. and taglib

Directive… JSP tags - Expression


• Ex: • The expression tag opens with a “<%=“
• <%@ page import=“import java.sql.*”; %> and ends with a “%>”.
• <%@ include file=“file.html” %> • It is used for an expression statement
• <%@ taglib url=“mypage.tld” %> whose result replaces the expression tag
when the JSP virtual engine resolves JSP
tags.
• It closes with a “%>”

3
25-Sep-21

JSP tags – Scriptlets


• Opens with a “<%” and ends with a “%>” • HelloWorld.jsp
and contains java loops and control • JSP2.java (change jsp page and demo
statements too)

Methods Control statements


• Methods are defined within JSP tags. • Content displayed on a browser window
• Similar to variable declarations, method can be made truly dynamic by changing
definitions are also placed between the program flow.
“<%!” and the “%>” tags. • Two control statements can be used within
• Methods can be overloaded. JSP pages – the “if” and the “switch”.
• Methods are called within the expression • Their operation mirrors their operation in
evaluation tags ( <%=) the java language.

Loops
• Controls.jsp • Three kinds of loops normally used – The
for loop, while loop and the do-while loop.

4
25-Sep-21

Predefined variables
• request – HttpServletRequest • Loops.jsp
• response – HttpServletResponse
• out – Writer used to send output to client. • The function calls that are used in a
• session – the HttpSession associated servlet can be used within the jsp as well.
with the request
• application – the ServletContext as
obtained via getServletContext()

Calling methods Calling methods…


• Methods defined within the JSP can be called • FetchSys.jsp
without any scope.
• For methods defined in external classes, the
• FetchParams.java
class has to be imported with the “page”
directive. The method (if static) can then be
called using the “class” scope.
• If the method is non-static, an object of the class
needs to be created in a “declaration” section
and then the method must be called on the
object.

System properties Cookies


• os.name • The Cookie methods can be used in the
• os.version same way as in servlets.
• path.separator • The setMaxAge() may be used to persist
• file.separator the cookie beyond the current session.
• java.version
• java.vendor • Ex: setcookie.jsp and getcookie.jsp
• java.vendor.url etc

5
25-Sep-21

Session objects Session objects


• Attributes can be shared across jsp pages • sessions1.jsp and sessions2.jsp
within the same browser session using the
“session” in-built object.
• This can be very useful in an application
which uses different JSPs for different
functionalities.
• The “session” object is an HttpSession. So
the usual methods (as used in servlets)
can be called on it.

Exception Handling Exception Handling…


• When a JSP encounters an error, it prints • The “errorPage” attribute of the “page”
the error stack on the browser window. directive can be used.
This can sometimes be clumsy. • Ex: <%@ page errorPage=“myerror.jsp
• A clean way of handling errors/exceptions %>
is to use a separate “error” page for • Within the error page, the inbuilt
exceptions. This page can be used “exception” object can be used. To access
whenever an exception occurs. The JSP this object, the isErrorPage attribute is set
can redirect the flow to the error handling to true in the page directive.
page when an exception occurs. • <%@ page isErrorPage=“true” %>

Exception Handling
• FetchSysProp.jsp and myerror.jsp

You might also like