JSP and Java Bean
Dr. Mareeswari V
School of Computer Science Engineering and Information Systems
(SCORE)
VIT University, Vellore
Cabin No:SJT 210-A30
Java Server Pages
JSP for short is Sun's solution for developing dynamic web sites. JSP
provide excellent server side scripting support for creating database
driven web applications. JSP enable the developers to directly insert
java code into jsp file, this makes the development process very simple
and its maintenance also becomes very easy. JSP pages are efficient, it
loads into the web servers memory on receiving the request very first
time and the subsequent calls are served within a very short period of
time.
Java is known for its characteristic of "write once, run anywhere." JSP
pages are platform independent. Your port your .jsp pages to any
platform. The advantage of JSP is that they are document-centric.
JavaServer Pages (JSP) is a technology that helps software
developers create dynamically generated web pages based on HTML,
XML, or other document types.
2 [Link] V, SCORE, VIT, VELLORE
In today's environment most web sites servers dynamic pages based on
user request. Database is very convenient way to store the data of users
and other things. JDBC provide excellent database connectivity in
heterogeneous database environment. Using JSP and JDBC its very easy
to develop database driven web application.
3 [Link] V, SCORE, VIT, VELLORE
Advantage of JSP over Servlet
1) Extension to Servlet : JSP technology is the extension to servlet
technology. We can use all the features of servlet in JSP. In addition to, we can
use implicit objects, predefined tags, expression language and Custom tags in
JSP, that makes JSP development easy.
2) Easy to maintain : JSP can be easily managed because we can easily
separate our business logic with presentation logic. In servlet technology, we
mix our business logic with the presentation logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project.
The servlet code needs to be updated and recompiled if we have to change the
look and feel of the application.
4) Less code than Servlet : In JSP, we can use a lot of tags such as action
tags, jstl, custom tags etc. that reduces the code. Moreover, we can use EL,
implicit objects etc.
4 [Link] V, SCORE, VIT, VELLORE
Life Cycle of JSP
5 [Link] V, SCORE, VIT, VELLORE
HTML Form Processing Using JSP
6 [Link] V, SCORE, VIT, VELLORE
Cont…
As with a normal page, your browser sends an HTTP request to
the web server.
The web server recognizes that the HTTP request is for a JSP
page and forwards it to a JSP engine. This is done by using the
URL or JSP page which ends with .jsp instead of .html.
The JSP engine loads the JSP page from disk and converts it into
a servlet content. This conversion is very simple in which all
template text is converted to println() statements and all JSP
elements are converted to Java code that implements the
corresponding dynamic behavior of the page.
7 [Link] V, SCORE, VIT, VELLORE
Cont…
The JSP engine compiles the servlet into an executable class and
forwards the original request to a servletengine.
A part of the web server called the servletengine loads the
Servletclass and executes it. During execution, the servlet
produces an output in HTML format, which the servletengine
passes to the web server inside an HTTP response.
The web server forwards the HTTP response to your browser in
terms of static HTML content.
Finally web browser handles the dynamically generated HTML
page inside the HTTP response exactly as if it were a static page.
8 [Link] V, SCORE, VIT, VELLORE
Cont..
This translation and compilation phase occurs only when the JSP file is
requested for the first time, or if it undergoes any changes to the extent
of getting retranslated and recompiled. For each additional request of
the JSP page thereafter, the request directly goes to the servlet byte
code, which is already in memory. Thus when a request comes for a
servlet, an init() method is called when the Servlet is first loaded into
the virtual machine, to perform any global initialization that every
request of the servlet will need. Then the individual requests are sent to
a service() method, where the response is put together. The servlet
creates a new thread to run service() method for each request. The
request from the browser is converted into a Java object of type
HttpServletRequest, which is passed to the Servlet along with an
HttpServletResponse object that is used to send the response back to
the browser. The servlet code performs the operations specified by the
JSP elements
9
in the .jsp file.
[Link] V, SCORE, VIT, VELLORE
JSP Tags
Directives
In the directives we can import packages, define error handling pages
or the session information of the JSP page.
Declarations
This tag is used for defining the functions and variables to be used in
the JSP.
Scriplets
In this tag we can insert any amount of valid java code and these
codes are placed in _jspService method by the JSP engine.
Expressions
We can use this tag to output any data on the generated page. These
data are automatically converted to string and printed on the output
stream.
10 [Link] V, SCORE, VIT, VELLORE
Directives
Syntax of JSP directives is:
<%@directive attribute="value" %>
Where directive may be:
page: page is used to provide the information about it.
Example: <%@page language="java" %>
include: include is used to include a file in the JSP page.
Example: <%@ include file="/[Link]" %>
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/[Link]" prefix="mytag" %>
11 [Link] V, SCORE, VIT, VELLORE
and attribute may be:
language="java“
This tells the server that the page is using the java language. Current
JSP specification supports only java language.
session="true“
When this value is true session data is available to the JSP page
otherwise not. By default this value is true.
errorPage="[Link]"
errorPage is used to handle the un-handled exceptions in the page.
contentType="text/html;charset=ISO-8859-1"
Use this attribute to set the mime type and character set of the JSP.
<%@page language="java" session="true" contentType="text/html;
charset=ISO-8859-1" errorPage="[Link]" %>
12 [Link] V, SCORE, VIT, VELLORE
Declarative
Syntax of JSP Declaratives are:
<%!
//java codes
%>
Variables and functions defined in the declaratives are class
level and can be used anywhere in the JSP page.
13 [Link] V, SCORE, VIT, VELLORE
Expressions
JSP Expressions start with <%= and ends with %>. Between these
this you can put anything and that will converted to the String and that
will be displayed.
Example:
<%="Hello World!" %> display 'Hello World!'
<%=getCount()%> return value of getCount() method
<%= (new [Link]())%> Date
<%= "Welcome "+[Link]("uname") %> Form Value
JSP Comments:
<%-- This is JSP comment --%>
14 [Link] V, SCORE, VIT, VELLORE
Example Program : Count Display
<%@page <p>Values of Cnt are:</p>
contentType="text/html" %> <p><%=getCount()%></p>
<html><body> <p><%=getCount()%></p>
<%! <p><%=getCount()%></p>
int cnt=0; <p><%=getCount()%></p>
private int getCount() <p><%=getCount()%></p>
{ <p><%=getCount()%></p>
cnt++; </body></html>
return cnt;
}
%>
15 [Link] V, SCORE, VIT, VELLORE
Scriptles / Implicit Objects
Syntax of JSP Scriptles are: <% //java codes %>
To simplify code in JSP expressions and scriptlets, you are
supplied with nine automatically defined variables, sometimes
called implicit objects.
16 [Link] V, SCORE, VIT, VELLORE
out
For writing any data to the buffer, JSP provides an implicit object named
out. It is the object of JspWriter ([Link]).
In case of servlet you need to write:
PrintWriter out=[Link]();
But in JSP, you don't need to write this code.
<%@page import ="[Link]"%>
<html> <body>
<% [Link]("Today is:"+new Date()); %> </body></html>
Output: Today is:Tue Mar 10 10:54:04 IST 2015
17 [Link] V, SCORE, VIT, VELLORE
request
request represents the clients request and is a subclass
of HttpServletRequest. Use this variable to retrieve the data
submitted along the request information such as parameter,
header information, remote address, server name, server port,
content type, character encoding etc.
<%
String userName=[Link]("userName"); %>
<%=[Link]("User-Agent") %>
18 [Link] V, SCORE, VIT, VELLORE
response
response is an implicit object of type HttpServletResponse.
The instance of HttpServletResponse is created by the web
container for each jsp request.
It can be used to add or manipulate response such as redirect
response to another resource, send error etc.
<% //Set refresh, autoload time as 5 seconds
[Link]("Refresh", 5); %>
<% [Link]("[Link] %>
<%[Link](new Cookie("Test","Value")); %>
19 [Link] V, SCORE, VIT, VELLORE
config
config is an implicit object of type ServletConfig. This object
can be used to get initialization parameter for a particular JSP
page. The config object is created by the web container for
each jsp page.
Generally, it is used to get initialization parameter from
the [Link] file.
<% [Link](); %>
<%=[Link]("dname") %>
20 [Link] V, SCORE, VIT, VELLORE
application
application is an implicit object of type ServletContext.
The instance of ServletContext is created only once by the web
container when application or project is deployed on the server.
This object can be used to get initialization parameter from
configuration file ([Link]). This initialization parameter can be used
by all jsp pages.
It can also be used to get, set or remove attribute from the
application scope.
<%
[Link]("driver name is="+[Link]("dname"));
%>
<% [Link]("hitCounter", hitsCount); %>
21 [Link] V, SCORE, VIT, VELLORE
session
session is an implicit object of type HttpSession.
The Java developer can use this object to set, get or remove attribute or
to get session information.
<input type="text" name="uname"> 1st page
String name=[Link]("uname"); 2nd page
[Link]("user",name);
[Link]("Hello "+ [Link]("user"); 3rd page
<%=[Link]() %>default information
22 [Link] V, SCORE, VIT, VELLORE
pageContext
pageContext is an implicit object of type PageContext.
The PageContext class defines several fields, including PAGE_SCOPE,
REQUEST_SCOPE, SESSION_SCOPE, and APPLICATION_SCOPE,
which identify the four scopes.
In JSP, page scope is the default scope.
This object is intended as a means to access information about the
page while avoiding most of the implementation details.
[Link]("user",name,PageContext.SESSION_SCOPE);
[Link]("user",PageContext.SESSION_SCOPE);
[Link]("attrName", PAGE_SCOPE);
23 [Link] V, SCORE, VIT, VELLORE
page
JSP page implicit object is instance of [Link] class and
represents the current JSP page. page object provide reference to the
generated servlet class. This object is very rarely used. This is simply a
synonym for this, and is used to call the methods defined by the translated
servlet class.
<%=[Link]().getName() %> or
<%=[Link]().getName() %>
Output: [Link].JspObjects_jsp
24 [Link] V, SCORE, VIT, VELLORE
exception
exception is an implicit object of type [Link]
class. This object can be used to print the exception. But it can
only be used in error pages.
<%@ page isErrorPage="true" %>
<html> <body>
Sorry following exception occured:<%= exception %>
</body> </html>
25 [Link] V, SCORE, VIT, VELLORE