Professional Documents
Culture Documents
2
What is jsp?............................................................................................................................................2
Why use JSP?.........................................................................................................................................2
JSP Architecture.........................................................................................................................................3
Steps required for a JSP request:............................................................................................................3
Setting up the JSP environment.............................................................................................................5
Using JSP tags ...........................................................................................................................................5
Declaration tag ( <%! %> ) ..................................................................................................................6
Expression tag ( <%= %>) ..................................................................................................................6
Directive tag ( <%@ directive ... %>)...................................................................................................6
Scriptlet tag ( <% ... %> ) .....................................................................................................................7
Example using scripting elements and Directive...................................................................................8
Implicit objects...........................................................................................................................................9
Actions.......................................................................................................................................................9
include....................................................................................................................................................9
UseBean...............................................................................................................................................10
Forward................................................................................................................................................11
Accessing database from JSP...................................................................................................................11
JSP Cookies Example...............................................................................................................................14
Working with jsp session.........................................................................................................................14
Comments and Character Quoting Conventions......................................................................................15
Jsp Expression Language ........................................................................................................................15
JSP Scopes................................................................................................................................................16
Page......................................................................................................................................................16
Request ................................................................................................................................................16
Application...........................................................................................................................................17
Example...................................................................................................................................................17
Custom error page....................................................................................................................................21
Agenda
Introduction to JSP
JSP Architecture
Using jsp tags
JSP Scriptlets
JSP Directives
The JSP page Directive
The JSP include Directive
JSP Declarations
Example Using Scripting Elements and Directives
Implicit Objects
● request
● response
● out
● application
● session
● config
● pageContext
● page
Actions
● The jsp:include Action
● The jsp:useBean Action
● The jsp:setProperty Action
● The jsp:getProperty Action
● The jsp:forward Action
Accessing database from JSP
JSP Cookies Example
Working with JSP Sessions
Comments and Character Quoting Conventions
CaseStudy-How to Design a OOPS Application
Hands-On Session
Introduction to JSP
What is jsp?
JavaServer Pages (JSP) technology is the Java platform technology provides a way to dynamically
generated web pages and simplifies the process of developing web-based applications.
Jsp files are HTML files with special tag containing java source code that provides the dynamic
content.
JSP become a servlet.A servlet don't create. The container looks at your jsp , translates it into source
code, and compiles it into a java servlet class.
JSP is based on Java,an object-oriented language. JSP offers a robust platform for web development.
Declarations do not generate output so are used with JSP expressions or scriptlets.
For Example,
<%!
private int counter = 0 ;
private String get Account ( int accountNo) ;
%>
A semicolon ( ; ) does not appear at the end of the code inside the tag.
Package foo;
public class Counter{
public static int count;
public static int getCount()
{
count++;
return count;
}
}
Wrong
<% out.println(Counter.getCount()); %>
should be
<% out.println(foo.Counter.getCount()); %>
3. taglib: taglib is used to use the custom tags in the JSP pages (custom tags allows us to defined
our own tags).
Example: <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>
2. extends="mypackage.myclass"
This attribute is used when we want to extend any class. We can use comma(,) to import more
than one packages.
Example: <%@page language="java" import="java.sql.*,mypackage.myclass" %>
3. session="true"
When this value is true session data is available to the JSP page otherwise not. By default this
value is true.
Example: <%@page language="java" session="true" %>
4. errorPage="error.jsp"
errorPage is used to handle the un-handled exceptions in the page.
Example: <%@page language="java" session="true" errorPage="error.jsp" %>
5. contentType="text/html;charset=ISO-8859-1"
Use this attribute to set the mime type and character set of the JSP.
Example: <%@page language="java" session="true" contentType="text/html;charset=ISO-
8859-1" %>
<%
String name=request.getParameter("username");
out.println("name========="+name);
%>
</body>
</html>
Implicit objects
Object Class or Interface Description
page jsp.HttpJspPage Page's servlet instance
config ServletConfig Servlet configuration information
pageContext jsp.pageContext Provides access to all the
namespaces associated with a JSP
page and access to several page
attributes
request http.HttpServletRequest Data included with the HTTP
Request
response http.HttpServletResponse HTTP Response data, e.g. cookies
out jsp.JspWriter Output stream for page context
session http.HttpSession User specific session data
application ServletContext Data shared by all application
pages
Actions
Action tag is used to transfer the control between pages and is also used to enable the use of
server side JavaBeans
• include
• forward
• useBean
include
Static resources should always be included using the JSP include directive.This is the compile
time include. Do note that you should always supply a relative URL for the file
attribute.
Difference between directive include and jsp include
Syntax
<!--#include file="data.inc"--> or
<jsp:include path=”path”/>
UseBean
JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes.
You use tags to declare a bean and use to set value of the bean class and use to get value of the
bean class.
bean.java
package jsplearning;
}
bean.jsp
bean.java
package jsplearning;
this.username = username;
}
bean.jsp
Forward
It forwards the current request to another page.
Syxtax:
<jsp:forward page=”url”>
<jsp:param----/>
</jsp:forward>
You can also use the: <jsp:forward page="/newpage.jsp" /> Also note that you can only use
this before any output has been sent to the client. I beleve this is the case with the
response.sendRedirect() method as well.
If you want to pass any paramateres then you can pass using <jsp:forward
page="/servlet/login"> <jsp:param name="username" value="jsmith" /> </jsp:forward>>
<html>
<head>
</head>
<body>
<form name="form1">
</form>
<table><tr><td>
<%
sampl.setConnection();
}
%>
</td></tr></table>
</body>
</html>
_______________________________
mysql.java
___________
package hello;
import java.sql.*;
import javax.servlet.http.*;
}
public void setConnection()
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/mysql",
"root", "");
}
catch(Exception e)
{
System.out.println("error="+e.getMessage());
}
try{
}
catch(Exception e)
{
System.out.println("error="+e.getMessage());
/* try
{
String s1="insert into samp(emplyid) values('"+employid+"')";
System.out.println("s1="+s1);
st=con.prepareStatement(s1);
st.executeUpdate();
}
catch(Exception e)
{
System.out.println("error="+e.getMessage());
}
*/
}
}
Syntax
${person.name}
Example
</body>
</html>
JSP Scopes
Page
Request
Session
Application
Page
Page Scope is the smallest scope. Accessible only in the page in which the object is created. Released
when the response is returned or the request forwarded. This is like making a “private variable” in
Java.
Request
Request scope is the next smallest scope, and is represented with the JSP's request object. All JSPs and
servlets that share a request share the request scope. For example, if I have a JSP that forwards to
another page, and that second page includes a third JSP page, then all three pages are in the same
request, and can share objects through the request scope. A special note here, is the
response.redirect(), will create a new request, unlike forwards and includes. Also note, a new
request is made every time the user gets a new page, be it by clicking a link, a button, or some
JavaScript call.
Session
Session scope is the next lowest scope, represented by an HttpSession object. All requests from the
same user are in the same session. Each user has his own session.
Application
Application scope objects are global objects and accessible to all jsp pages which lie in the same
application. All users share the same values of the application scope, as there is only one made for the
web application. If you have some static material that all users should be able to access, then put it in
the application. So application scope is usually best used for Read-Only data.
Example
Four files
Firstinclude.jsp
Secondinclude.jsp
Linked.jsp
Forward.jsp
Firstinclude.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="scopeVarPage" value="Page Value" scope="page" />
<c:set var="scopeVarRequest" value="Request Value" scope="request" />
<c:set var="scopeVarSession" value="Session Value" scope="session" />
<c:set var="scopeVarApplication" value="Application Value" scope="application" />
<body>
<h3>Main File: index.jsp</h3>
<table border="1">
<tr>
<th>Scoped Variable</th>
<th>Current Value</th>
</tr>
<tr>
<td>
<b>Page Scope</b>
(scopeVarPage)</td>
<td> 
<c:out value="${scopeVarPage}" />
</td>
</tr>
<tr>
<td>
<b>Request Scope</b>
(scopeVarRequest)</td>
<td> 
<c:out value="${scopeVarRequest}" />
</td>
</tr>
<tr>
<td>
<b>Session Scope</b>
(scopeVarSession)</td>
<td> 
<c:out value="${scopeVarSession}" />
</td>
</tr>
<tr>
<td>
<b>Application Scope</b>
(applicationVarPage)</td>
<td> 
<c:out value="${scopeVarApplication}" />
</td>
</tr>
</table>
<br />
</body>
</html>
Secondinclude.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<table border="1">
<tr>
<th>Scoped Variable</th>
<th>Current Value</th>
</tr>
<tr>
<td>
<b>Page Scope</b>
(scopeVarPage)</td>
<td> 
<c:out value="${scopeVarPage}" />
</td>
</tr>
<tr>
<td>
<b>Request Scope</b>
(scopeVarRequest)</td>
<td> 
<c:out value="${scopeVarRequest}" />
</td>
</tr>
<tr>
<td>
<b>Session Scope</b>
(scopeVarSession)</td>
<td> 
<c:out value="${scopeVarSession}" />
</td>
</tr>
<tr>
<td>
<b>Application Scope</b>
(applicationVarPage)</td>
<td> 
<c:out value="${scopeVarApplication}" />
</td>
</tr>
</table>
linked.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Scope Example</title>
</head>
<body>
<h3>Forwarded File: forward.jsp</h3>
<table border="1">
<tr>
<th>Scoped Variable</th>
<th>Current Value</th>
</tr>
<tr>
<td>
<b>Page Scope</b>
(scopeVarPage)</td>
<td> 
<c:out value="${scopeVarPage}" />
</td>
</tr>
<tr>
<td>
<b>Request Scope</b>
(scopeVarRequest)</td>
<td> 
<c:out value="${scopeVarRequest}" />
</td>
</tr>
<tr>
<td>
<b>Session Scope</b>
(scopeVarSession)</td>
<td> 
<c:out value="${scopeVarSession}" />
</td>
</tr>
<tr>
<td>
<b>Application Scope</b>
(applicationVarPage)</td>
<td> 
<c:out value="${scopeVarApplication}" />
</td>
</tr>
</table>
</body>
</html>
forward.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Scope Example</title>
</head>
<body>
<h3>Forwarded File: forward.jsp</h3>
<table border="1">
<tr>
<th>Scoped Variable</th>
<th>Current Value</th>
</tr>
<tr>
<td>
<b>Page Scope</b>
(scopeVarPage)</td>
<td> 
<c:out value="${scopeVarPage}" />
</td>
</tr>
<tr>
<td>
<b>Request Scope</b>
(scopeVarRequest)</td>
<td> 
<c:out value="${scopeVarRequest}" />
</td>
</tr>
<tr>
<td>
<b>Session Scope</b>
(scopeVarSession)</td>
<td> 
<c:out value="${scopeVarSession}" />
</td>
</tr>
<tr>
<td>
<b>Application Scope</b>
(applicationVarPage)</td>
<td> 
<c:out value="${scopeVarApplication}" />
</td>
</tr>
</table>
</body>
</html>
error.jsp
%@ page language="java" isErrorPage="true" %>
<h1>error=<%=exception%></h1>