Example: index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<form action="request.jsp">
<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>
request.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% String username = request.getParameter("username");
out.println("Welcome "+ username);
%>
</body>
</html>
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<%response.setContentType("text/html"); %>
</body>
</html>
Example: index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% session.setAttribute("user","Pradeep"); %>
<a href="session.jsp">Click here to get user name</a>
</body>
</html>
session.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% String name = (String)session.getAttribute("user");
out.println("User Name is " +name);
%>
</body>
</html>
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% int num1=10;int num2=20;
out.println("num1 is " +num1);
out.println("num2 is "+num2);
%>
</body>
</html>
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% application.getContextPath(); %>
</body>
</html>
Example: web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>comingsoon</servlet-name>
<servlet-class>mysite.server.ComingSoonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>comingsoon</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% String servletName = config.getServletName();
out.println("Servlet Name is " +servletName);%>
</body>
</html>
pageContext: It is used to get, set and remove the attributes from a particular scope.
page: Page implicit variable holds the currently executed servlet object for the corresponding jsp. Acts as this object for current jsp page.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% String pageName = page.toString();
out.println("Page Name is " +pageName);
%>
</body>
</html>
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% int[] num1={1,2,3,4};
out.println(num1[5]);
%>
<%= exception %>
</body>
</html>
When the user page.jsp
follows the link to the page , he sends an http request to the server GET /page.jsp
. Then, based on this request and the text of the page itself, the server generates a java class, compiles it and executes the resulting servlet, which forms a response to the user in the form of a representation of this page, which the server redirects back to the user.
The JSP life cycle consists of several phases that are managed by the JSP container:
init()
method of the JSP class object and initializing the servlet configuration with the initial parameters that are specified in the deployment descriptor (web.xml
). After this phase, the JSP is able to handle client requests. Typically, these phases occur after the first client request (i.e., lazy loading), but you can configure the loading and initialization of JSP at the start of the application, similar to servlets.ServletRequest
and the ServletResponseservice
methods are executed.A servlet container (for example, Tomcat, GlassFish) creates a servlet class from a JSP page that inherits interface properties javax.servlet.jsp.HttpJspBase
and includes the following methods:
HttpJspPage
, its name begins with an underscore, and it differs from other life cycle methods in that it cannot be redefined;jspInit()
.There is no direct access to the directory /WEB-INF/
from the web application. Therefore, JSP pages can be located inside this folder and thereby restrict access to the page from the browser. However, by analogy with the description of servlets, it will be necessary to configure the deployment descriptor:
<servlet>
<servlet-name> Example </servlet-name>
<jsp-file> /WEB-INF/example.jsp </jsp-file>
<init-param>
<param-name> exampleParameter </param-name>
<param-value> parameterValue </param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name> Example </servlet-name>
<url-pattern> /example.jsp </url-pattern>
</servlet-mapping>
<%= expression %>
- expression that will be processed with redirecting the result to the output;<% code %>
- The code to add to the method service().<%! code %>
- code added to the servlet class body outside the method service().<%@ page att="value" %>
directive: - directives for the servlet container with parameter information.<%@ include file="url" %>
- a file on the local system that is included when translating the JSP into the servlet.<%-- comment --%>
- Comment; ignored when translating a JSP page to a servlet.Action tag and JSP Action Elements provide methods for working with Java Beans, connecting resources, forwarding queries and creating dynamic XML elements. Such elements always begin with recording jsp:and are used directly inside the JSP page without the need for third-party libraries or additional settings.
The most commonly used JSP Action Elements are:
1. jsp:useBean
This action name is used when we want to use beans in the JSP page. With this tag, we can easily invoke a bean.
<jsp:useBean id="" class="" />
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Action JSP1</title>
</head>
<body>
<jsp:useBean id="name" class="demotest.DemoClass">
</body>
</html>
2. jsp:include
It also used to insert a jsp file into another file, just like include directive. It is added during request processing phase
<jsp:include page="page URL" flush="true/false">
3. jsp:setProperty
This property is used to set the property of the bean. We need to define a bean before setting the property
<jsp:setproperty name="" property="">
4. jsp:getProperty
This property is used to get the property of the bean. It converts into a string and finally inserts into the output.
<jsp:getAttribute name="" property="">
5. jsp:forward
It is used to forward the request to another jsp or any static page. Here the request can be forwarded with no parameters or with parameters.
<jsp:forward page="value">
6. jsp:plugin
It is used to introduce Java components into jsp, i.e., the java components can be either an applet or bean. It detects the browser and adds
<jsp:plugin type="applet/bean" code="objectcode" codebase="objectcodebase">
7. jsp:param
This is child object of the plugin object described above. It must contain one or more actions to provide additional parameters.
<jsp:params>
<jsp:param name="val" value="val"/>
</jsp:params>
8. jsp:body
This tag is used to define the XML dynamically i.e., the elements can generate during request time than compilation time. It actually defines the XML, which is generated dynamically element body.
<jsp:body></jsp:body>
9. jsp:attribute
This tag is used to define the XML dynamically i.e. the elements can be generated during request time than compilation time It actually defines the attribute of XML which will be generated dynamically.
<jsp:attribute></jsp:attribute>
10. jsp:text
It is used to template text in JSP pages. Its body does not contain any other elements, and it contains only text and EL expressions.
<jsp:text>template text</jsp:text>
11. jsp:output
It specifies the XML declaration or the DOCTYPE declaration of jsp. The XML declaration and DOCTYPE are declared by the output
<jsp:output doctype-root-element="" doctype-system="">
“JSP - servlet - JSP” architecture for building applications is called MVC (Model / View / Controller) :
Implicit JSP object - an instance of a class javax.servlet.jsp.PageContext
provides access to all namespaces associated with a JSP page, as well as its various attributes. The remaining implicit objects are added to pageContext
automatically.
A class PageContext
is an abstract class, and its instance can be obtained through a method call JspFactory.getPageContext()
, and released through a method call JspFactory.releasePageContext()
.
PageContext has the following set of features and capabilities:
public
objects;JspWriter
output mechanism for output;page
the scripting environment;JSP Expression Language (EL) is a scripted expression language that allows you to access Java components (JavaBeans) from JSP. Starting with JSP 2.0, it is used inside JSP tags to separate Java code from the JSP to provide easy access to Java components, while reducing the amount of Java code in JSP pages, or even completely eliminating it.
The development of EL was aimed at making it easier for designers who have minimal knowledge of the Java programming language. Before the advent of the expression language, JSP had several special tags such as scriptlets (English), expressions, etc. that allowed you to write Java code directly on the page. Using an expression language, a web designer only needs to know how to organize the call of the corresponding java methods.
The JSP 2.0 expression language includes:
An expression language is used inside a construct ${ ... }
. A similar construction can be placed either separately or on the right side of the tag attribute setting expression.
JSTL Core Tags c:catchand
are used to catch and handle exceptions in the service methods of the class c:if
.
The tag c:catchcatches
the exception and wraps it in a variable exception
available for processing in the tag c:if
<c:catch var ="exception">
<% int x = 42/0;%>
</c:catch>
<c:if test = "${exception ne null}">
<p>Exception is : ${exception} <br />
Exception Message: ${exception.message}</p>
</c:if>
To configure various parameters of JSP pages, an element is used jsp-configthat is responsible for: