You are on page 1of 15

JSP Programming

What do you mean by Static & Dynamic


Contents?
Static contents
– Typically static HTML page
– Same display for everyone

Dynamic contents
– Contents is dynamically generated based on
conditions
Conditions could be
- User identity
- Time of the day
- User entered values through forms etc..
What is JSP Page?
A text-based document capable of returning both
static and dynamic content to a client browser
-- Static content and dynamic content can be
intermixed
--Static content
– HTML, XML, Text
-- Dynamic content
– Java code
– Displaying properties of Java Beans
– Invoking business logic defined in Custom tags
A Simple JSP Page
(Blue: static, Red: Dynamic contents)
<html>
<body>
Hello World!
<br>
Current time is <%= new
java.util.Date() %>
</body>
</html>
Comparison Servlets And JSP
JSP Benefits
--Content and display logic are separated
-- Simplify web application development with JSP,
JavaBeans and custom tags

-- Supports software reuse through the use of


components (JavaBeans, Custom tags)
-- Recompile automatically when changes are made to
JSP pages
--Platform-independent
Why JSP over Servlet?
Servlets can do a lot of things, but it is pain to:
– Use those println() statements to generate HTML
page
– Maintain that HTML page, need for compiling,
packaging,CLASSPATH setting
--Easier to create HTML, Can use standard tools
--Uses Separate business logic from the presentation
Do I have to Use JSP over Servlet or
vice-versa?
No, you want to use both leveraging the
strengths of each technology
– Servlet's strength is “controlling and dispatching”
– JSP's strength is “displaying”
In a typically production environment, both
servlet and JSP are used in MVC (Model-View-
Controller) pattern
– Servlet handles controller part
– JSP handles view part
JSP Architecture
JSP Page Lifecycle Phases

--Translation phase
--Compile phase
--Execution phase
JSP COMPONENTS
 Directives : JSP directive affects the overall
structure of the servlet that results from the JSP page.
 Expressions : Of the form <%= expression %>,
which are evaluated and inserted into the servlet’s output
 Scriptlets : Of the form <% code %>, which are
inserted into the servlet’s _jspService method (called by
service)
 Declarations : Of the form <%! code %>, which
are inserted into the body of the servlet class, outside of
any existing methods
JSP Directives

General Syntax :

<%@ directivename attribute=value %>


--page Directive
<%@ page import=“java.sql.*,java.util.*” %>
Attributes for page directive :
--language --session --isThreadSafe --isErrorPage
--extends --buffer --info --contentType

--import --autoFlush --errorPage


JSP Directives

--include Directive :
<%@ include file=“header.jsp” %>

-- This Directive instructs the container to include the


content of the resource in the current JSP by inserting it
inline in the JSP in place of Directive.
--It is important to note that the content of the included file
is parsed by JSP at translation time.
JSP Directives

--taglib Directive :
<%@ taglib uri="..." prefix="..." %>

-- this directive is used to define custom tags I.e user defined


tags
Predefined Variables
--request : This variable is the HttpServletRequest associated with the
request; it gives you access to the request parameters.
--response : This variable is the HttpServletResponse associated
with the response to the client.
--out : This is the PrintWriter used to send output to the client.
However, to make the response object useful, this is a
buffered version of PrintWriter called JspWriter.
--session : This variable is the HttpSession object associated
with the request.
--application : This variable is the ServletContext as
obtained via getServletConfig().getContext().

You might also like