JSP implicit objects
JSP supports nine automatically
defined variables,which are also called implicit objects.
Objects
|
Description
|
Request
|
This is HttpServletRequestobject associated with the request.
Request objects are passed as parameters to jspService() method when a client
request is made.
|
Response
|
This is HttpServletResponseobject associated with the response
to the client. Response objects carry the response of a client request after
the jspService() method is executed.
|
Out
|
This is PrintWriter object used to send output to the client.
|
session
|
This isHttpSessionobject associated with the request. The
session object helps access session data.
|
application
|
The application object refers to the entire environment of a
web application to which a JSP page belongs. The ServletContext(for sharing
data) as obtained via getServletContext().
|
config
|
The configobject specifies the configuration of the parameters
passed to a JSP page. This is theServletConfigobject associated with the
page.
|
pageContext
|
The pageContextobject represents the context of the current
JSP page. This encapsulates use of server-specific features like higher
performanceJspWriters.
|
page
|
This is simply a synonym forthis, and is used to call the
methods defined by the translated servlet class.
|
exception
|
The Exception object allows the exception data to be accessed
by designated JSP.
|
These
objects will be declared by the generated servlet and hence the statements we
write in JSP using these variables will get meaning once they are passed into
the servlet code.
Implicit Objects
Example:
request.jsp
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title
here</title></head>
<body>
Hello,
<b><%=request.getParameter("name")%></b><br/><br/>
Your request details
are <br/><br/>
<table
border="1">
<tr><th>Name</th><th>Value</th></tr>
<tr><td>request
method</td><td><%= request.getMethod()
%></td></tr>
<tr><td>request
URI</td><td><%= request.getRequestURI()
%></td></tr>
<tr><td>request
protocol</td><td><%= request.getProtocol()
%></td></tr>
<tr><td>browser</td><td><%=
request.getHeader("user-agent") %></td></tr>
</table>
<%String orgn=
application.getInitParameter("organization");
out.println("<br>Organization:
"+ orgn); %>
<%if(session.getAttribute(“email")==null)
{
session.setAttribute("email",
"asr@google.com");
}%>
<form action=" other.jsp"
method="post">
<input type="submit"
value="other implicit objects">
</form>
</body></html>
web.xml
<web-app>
<context-param>
<param-name>organization</param-name>
<param-value>Triniti</param-value>
</context-param>
</web-app>
other.jsp
<%@ 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>Insert title
here</title>
</head>
<%!
public void jspInit() {
System.out.println("In jspInit()
method");
}
%>
<body>
<%
String mail=
(String)session.getAttribute("email");
out.println("<h1>"+mail+"</h1>");
%>
</body>
</html>
Comments
Post a Comment