JSP
JSP
Pages
Topics
JSP Fundamentals
JSP Scripting Elements
JSP Implicit Objects
JSP Directives
JSP Actions
JSP Example (Loan Calculator)
Servlets & JSPs together
Tag Libraries
Deploying and Running a JSP Application
Introduction
JSP technology is used to create web
application just like Servlet technology.
It can be thought of as an extension to servlet
because it provides more functionality than
servlet such as expression language, jstl etc.
A JSP page consists of HTML tags and JSP tags.
The jsp pages are easier to maintain than
servlet because we can separate designing
and development.
It provides some additional features such as
Expression Language, Custom Tag etc.
Problems with Servlets
Designing in servlet is difficult and slows
down the application.
Writing complex business logic makes the
application difficult to understand.
You need a Java Runtime Environment on
the server to run servlets.
If your application is build on using servlet
technology, it is very difficult for
enhancement and bug fixing.
The Anatomy of a JSP Page
Implementation
class Generate
JSP Page Servlet
<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>
Index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/
>
</form>
</body>
</html>
Welcome.jsp
<html>
<body>
<%
String name=request.getParameter("unam
e");
out.print("welcome "+name);
%>
</form>
</body>
</html>
Expressions
Basics
Used to write dynamic content back to the
browser.
JSP expression tag is written to the output stream
of the response.
So you need not write out.print() to write data. It is
mainly used to print the values of variable or method.
If the output is an object then the result of calling
toString on the object is output to the browser
Embedded in <%= and %> delimiters
Example:
<%=“Fred”+ “ “ + “Flintstone %>
prints “Fred Flintstone” to the browser
<%=Math.sqrt(100)%>
prints 10 to the browser
Expression Tag
<%= … %>
Accepts any Java expression, evaluates
the expression, converts to a String, and
displays.
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
JSP expression tag that prints
current time
<html>
<body>
Current Time: <
%= java.util.Calendar.getInstance().getTim
e() %>
</body>
</html>
Index.jsp
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname"><br/
>
<input type="submit" value="go">
</form>
</body>
</html>
Welcome.jsp
<html>
<body>
<
%= "Welcome "+request.getParameter("un
ame") %>
</body>
</html>
Java Implicit Objects
Scope
Implicit objects provide access to server
side objects
e.g. request, response, session etc.
There are four scopes of the objects
Page: Objects can only be accessed in the
page where they are referenced
Request: Objects can be accessed within all
pages that serve the current request.
(Including the pages that are forwarded to
and included in the original jsp page)
Session: Objects can be accessed within the
JSP pages for which the objects are defined
Application: Objects can be accessed by all
JSP pages in a given context
Java Implicit Objects
List
request: Reference to the current request
response: Response to the request
session: session associated woth current request
application: Servlet context to which a page
belongs
pageContext: Object to access request,
response, session and application associated
with a page
config: Servlet configuration for the page
out: Object that writes to the response output
stream
page: instance of the page implementation class
(this)
exception: Available with JSP pages which are
error pages
Java Implicit Objects
Example
<html> <p>
<head> Storing a string to the application...<br>
<title>Implicit Objects</title> <% application.setAttribute("name", "Meeraj"); %>
</head> Retrieving the string from application...<br>
<body style="font-family:verdana;font-size:10pt"> <b>Name:</b>
<p> <%= application.getAttribute("name") %>
Using Request parameters...<br> </p>
<b>Name:</b> <%= request.getParameter("name") %> <p>
</p> Storing a string to the page context...<br>
<p> <% pageContext.setAttribute("name", "Meeraj"); %>
<% out.println("This is printed using the out implicit Retrieving the string from page context...</br>
variable"); %>
<b>Name:</b>
</p>
<%= pageContext.getAttribute("name") %>
<p>
</p>
Storing a string to the session...<br>
</body>
<% session.setAttribute("name", "Meeraj"); %>
</html>
Retrieving the string from session...<br>
<b>Name:</b> <%= session.getAttribute("name") %>
</p>
Example Implicit Objects
Deploy & Run
Save file:
$TOMCAT_HOME/webapps/jsp/Implicit.jsp
Access file
http://localhost:8080/jsp/Implicit.jsp?name=Sanjay
Results of the execution
<% </body>
} </html>
header.jsp
%>
<h3>Loan Calculator</h3>
footer.jsp
<%= new java.util.Date() %>
Loan Calculator
simple.jsp
<%@ page errorPage="error.jsp" %> <html>
<%! <head>
</tr> </tr>
<tr> </table>
<td><b>Description:</b></td> <%
<td> }
<input name="description" type="text" value="<%= rs.getString(3) %>
%>"/>
</body>
</td>
</html>
</tr>
Inventory
Edit.jsp
<%@page contentType="text/html"%> <tr>
<jsp:useBean id="rs" scope="request" type="javax.sql.RowSet" /> <td><%= rs.getString(2) %></td>
<td><%= rs.getString(3) %></td>
<% %>
</table>
while(rs.next()) {
<a href="New.html">New Item</a>
%>
</body>
</html>
Inventory
New.html
<html> <tr>
<head> <td></td>
<title>Inventory - Add New Item</title> <td><input type="submit" value="Create"/></td>
</head> </tr>
<body style="font-family:verdana;font-size:10pt;">
</table>
<form action="Create">
<table cellpadding="5" style="font-family:verdana;font-size:10pt;"> </body>
<tr> </html>
<td><b>Name:</b></td>
<td><input name="name" type="text"/></td>
</tr>
<tr>
<td><b>Description:</b></td>
<td><input name="description" type="text"/></td>
</tr>
<tr>
<td><b>Price:</b></td>
<td><input name="price" type="text"/></td>
</tr>
<tr>
<td><b>Stock:</b></td>
<td><input name="stock" type="text"/></td>
</tr>