Professional Documents
Culture Documents
Pages (JSP)
U. K. R.
Dept. of IT, JU
u_roy@it.jusl.ac.in
Web Development
Static html
– text file containing html tags created
manually
– may include some client-side scripts (e.g.
JavaScript)
Active html
html contains a program that runs at the client
Java Applets
Dynamic html
– html file produced at time of request
Browser
HTTP / HTML
Application Server
Servlet JSP
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>\n" +
"<HEAD><TITLE>HelloWWW</TITLE></HEAD
>\n" +
"<BODY>\n" + "<H1>Hello
WWW</H1>\n" +
"</BODY></HTML>");
}
}
Java Server Pages
JSPs are equivalent to Servlets
A simplified, fast way to create dynamic web
content
HTML or XML pages with embedded Java Code
or Java Beans
Can be a mix of template data in HTML/XML with
some dynamic content
A JSP is a complied to a Java Servlet
automatically by the Servlet container, it is then
cached
Predefined objects
You can declare your own variables, as
usual
JSP provides several predefined variables
request : The HttpServletRequest parameter
response : The HttpServletResponse parameter
session : The HttpSession associated with the
request, or null if there is none
out : A JspWriter (like a PrintWriter) used to
send output to the client
Example:
Your hostname: <%= request.getRemoteHost() %>
JSP Tags
Four types of tags
Directives
Used to import packages, define error handling
pages or the session information of the JSP page
Declaration
used for defining the functions and variables to be
used in the JSP
Scriplets
Used to insert any amount of valid java code
Expression
Used to output any data on the generated page
Directives
Directives affect the servlet class itself
A directive has the form:
<%@ directive attribute="value" %>
or
<%@ directive attribute1="value1"
attribute2="value2"
...
attributeN="valueN" %>
The most useful directive is page, which lets
you import packages
Example: <%@ page import="java.util.*" %>
page Directives
Example
<%@page language="java" %>
<%@page import="java.sql.* %>
<%@page extends="mypackage.myclass" %>
<%@page session="true" %>
<%@page errorPage="error.jsp" %>
<%@page contentType="text/html;charset=ISO-
8859-1" %>
The include directive
The include directive inserts another file
into the file being parsed
The included file is treated as just more JSP,
hence it can include static HTML, scripting
elements, actions, and directives
Syntax: <%@ include file="URL " %>
The URL is treated as relative to the JSP page
If the URL begins with a slash, it is treated as
relative to the home directory of the Web server
The include directive is especially useful
for inserting things like navigation bars
Directives
Syntax
<%@directive attribute="value" %>
<%@ directive attribute1="value1"
attribute2="value2"
...
attributeN="valueN" %>
Directive may be
page:
Used to provide the information about it
include:
Used to include a file in the JSP page
taglib:
used to use the custom tags in the JSP pages
Directives
include
<%@ include file="/header.jsp" %>
taglib
<%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>
Actions
Actions are XML-syntax tags used to
control the servlet engine
<jsp:include page="URL " flush="true" />
Inserts the indicated relative URL at execution
time (not at compile time, like the include
directive does)
This is great for rapidly changing data
<HTML>
<BODY>
Hello! The time is now <%= new java.util.Date() %>
</BODY>
</HTML>
Session Tracking
Typical scenario – shopping cart in online store
Necessary because HTTP is a "stateless"
protocol
Common solutions: Cookies and URL-rewriting
Session Tracking API allows you to
look up session object associated with current
request
create a new session object when necessary
look up information associated with a session
store information in a session
discard completed or abandoned sessions
Session Tracking API
Looking up a session object
HttpSession session = request.getSession(true);
Pass true to create a new session if one does
not exist
Associating information with session
session.setAttribute(“user”,request.getParamete
r(“name”))
Session attributes can be of any type
Looking up session information
String name = (String)
session.getAttribute(“user”)
JSP and JavaBeans
A JavaBean is a Java Class file that
creates an object
Defines how to create an Object, retrieve
and set properties of the Object