Slot 6 - JSP
Slot 6 - JSP
JavaServer Pages
Objective
❖ Solution:
❑ Can be accomplished using Servlet technology.
❑ Create a Servlet class named GreetingServlet.
❑ Processes the request and generates a response to be
sent to the client.
• JSP pages use reusable components, such as JavaBeans which can be used by
multiple programs.
• JSP allows a Web developer with limited knowledge in scripting language to design a
page.
• JSP supports the use of JavaBean components with JSP language elements. The user
can easily create and initialize beans and get and set their values.
JSP_Lương Hoàng Hướng 10
Use of Servlet and JSP
❖ Contains tags that are categorized as Standard Tags and Custom Tags.
JSP Scriptlets
❖ Is used to embed Java code within an HTML code.
❖ Is inserted into the _jspService() method of the servlet.
❖ Are executed when the request of the client is being processed.
❖ Are used to add complex data to an HTML form.
❖ Syntax:
<% Java code fragment %>
❖ Figure depicts the use of scriptlet in a JSP page.
JSP Declarations
❖ Allow the user to define variables and methods that are inserted into the
Servlet, outside the service() method.
❖ Can declare variables within the scriptlet tag also, but that variable will be
local to that scriptlet.
❖ Variable declared using the declaration tag will be visible in the whole JSP
page.
❖ Cannot be declared within the scriptlet tag.
❖ Can be used to define multiple variables of same data type.
❖ Syntax:
<%! Java declaration code %>
Comments
❖ JSP comments are used for documenting JSP code which should not be visible
to the client when viewing the source of the Web page.
❖ These comments are called server-side comments and are encoded within
<%-- and --%> symbol.
❖ Syntax:
User can declare these directives anywhere on the page. However, mostly
they are declared at the top of the JSP page.
JSP_Lương Hoàng Hướng 21
Directive Tags
• The include directive includes static contents on the current JSP page
include
• The taglib directive includes the custom library tags on the JSP page
taglib
❖ Used to import more than one package in the JSP page. If more than one
package is imported, then separate the package names by commas.
Language: Used to specify the language of the script. The default value of
language in a JSP page is Java.
errorPage: A path name to this JSP file sends exceptions. If the pathname
begins with a ‘/’, the path is relative to the root directory and is resolved by
the Web server. If not, the path name is relative to the current JSP file.
Session: The client must join an HTTP session in order to use the JSP page. If the
value is true, the session object refers to the current or new session. The default
value for session is true.
where,
❑include indicates the directive and Filename specifies the name of
a file to include along with relative path.
where,
❑ tagLibraryURI specifies Uniform Resource Identifier (URI) that
identifies the tag library descriptor.
❑ prefix specifies tag name that is used to define a custom tag.
❑ tagPrefix defines the prefix string in prefix:tagname.
❖ Implicit objects:
❑ Are a set of Java objects that are available in every JSP page.
❑ Are created and loaded by the Web container.
❑ Are referred to as pre-defined variables.
❑ Are accessible within the scripting elements in the JSP page.
• The scope communication objects provide access to all the objects available in the given
scope.
• Objects in a JSP application are accessible according to the specified scope.
• The scope of an object is the section where that object is accessible
Servlet Objects
Error Objects
• The error object handles error in a JSP page using an implicit object known as Exception.
• User can access this object by declaring JSP page as an error page.
• To do so, use the isErrorPage attribute of the page directive. For example, <%@page
isErrorPage="true" %>.
request Object
❑ When the JSP page is translated into the Servlet class, the request object
is passed as a reference of the
javax.servlet.http.HttpServletRequest interface by the
container.
❑ The scope of the request object is page-level which means that the object
will be accessible only in the current page.
JSP_Lương Hoàng Hướng 34
Input and Output Objects
Table lists some of the methods of the request object available on the JSP page for
the developers.
Method Description
java.lang.String This method returns the value of a request parameter as a
getParameter(java.lang.String name) string, or null if the parameter does not exist.
java.lang.String[] This method returns an array of string objects which
getParameterValues(java.lang.String contains all the values the given request parameter has, or
name) null if the parameter does not exist.
java.util.Enumeration This method returns an enumeration of string objects, which
getParameterNames() contains the names of the parameters. If no parameters,
this method returns an empty enumeration.
void setAttribute(java.lang.String This method stores an attribute in the request. Attributes
name,java.lang.Object o) are reset between requests.
java.lang.Object This method returns the name of the string from the named
getAttribute(java.lang.String name) attribute.
java.lang.String getRemoteHost() This method returns the name of the client or the last proxy
that sends the request. If the hostname is not resolved, this
method returns the dotted-string form of the IP address.
java.lang.String getRemoteAddr() This method returns the Internet protocol address of the
client or last proxy that sent the request.
out Object
❖ Session Object:
❑ Is an instance of javax.servlet.http.HttpSession interface.
❑ Has session scope.
The code snippet demonstrates how to associate a new session object with
the request.
The code snippet shows the pageContext object used to set and retrieve
the object.
The code snippet demonstrates how to retrieve all the objects with request
scope.
The code snippet demonstrates how to remove the objects from the JSP
page.
config
page Object
Object
❖ page Object:
❑ It is an instance of the java.lang.Object.
❑ It is an instance of the servlet processing the current request in a JSP page.
❑ It has page scope.
The code snippet demonstrates how to access the methods of the Servlet
through page object.
<%
out.println(this.getServletInfo());
out.println(((Servlet)page).getServletInfo());
%>
❖ config Object:
❑ Stores the information of the servlet, which is created during the
compilation of a JSP page.
❑ Is an instance of the javax.servlet.ServletConfig interface.
❑ Provides methods to retrieve servlet initialization parameters.
❑ Represents the configuration of the servlet data where a JSP page is
complied.
❑ Has page scope.
❑ This method returns a string which contains the value of the named
initialization parameter, otherwise it returns null.
The isErrorPage attribute is set to true, which indicates that the page is an error
page. The JSP expression (<%= %>) is used to create an instance of the exception
object.
The out object is passed as an argument to the printStackTrace()method which
will display the stack trace information.
JSP_Lương Hoàng Hướng 62
Summary
JSP technology is used for developing dynamic Web sites and provides server-side scripting support for creating
Web applications.
A JSP page is a mixture of standard HTML tags, Web page content, and dynamic
The JSP elements are expressions, scriptlets, comments, declarations, directives, and actions.
Directives are used to specify the structure of the resulting servlet and actions are JSP tags that transfer
control to other server objects or perform operations on other objects.
The page directive is used to set the page attributes, such as the language to be used and encoding format.
The include directive is used to insert a file referenced in the directive into the JSP page.
JSP implicit objects are a set of Java objects that are created and maintained
automatically by the Web container.
The request object represents the request from the client for a Web page.
The response implicit object handles the response generated by JSP and sends
response to the client.
The Web server creates a session object for multiple requests sent by a single user.
The application object represents the application to which the JSP page belongs.
The pageContext object allows user to access all the implicit objects defined
in
The page object provides access to all the objects, which are defined
in a Web page.