Implicit Objects
Implicit Objects
Expressions
1. Implicit Objects
2. Character Quoting Conventions
3. Unified Expression Language [Unified El]
4. Expression Language
Implicit Objects
1. out
• Out uses various methods such as:
a. void print(): It prints the data to the output. One can specify
the data type too.
out.print(datatype d);
b. void println(): It prints the data type and terminates the
line.
out.println(datatype d);
c. void flush(): The contents in the buffer(if present) gets
written to the output and the stream is flushed.
out.flush();
Implicit Objects
1. out
• Out uses various methods such as:
d. void clear():
It clears the buffer. Even if something is present in the buffer,
it doesn’t get written to output.
It will throw an exception, if this method is called on a buffer
that is already flushed.
out.clear();
Implicit Objects
1. out
• Out uses various methods such as:
e. void clear buffer():
It is similar to clear but it doesn’t give an exception if called
on a flushed buffer.
out.clear buffer();
f. boolean isAutoFlush():
This method checks that the buffer is set to flush automatically
or not.
out.isAutoFlush();
Implicit Objects
1. out
• Out uses various methods such as:
g. int getBufferSize():
This method returns the size of the buffer in bytes.
out.getBufferSize();
h. int getRemaining():
This method returns the remaining bytes of buffer that are
empty before the buffer overflows.
out.getRemaining();
Implicit Objects
1. out - Out uses various methods Example: Out.jsp
<html>
<head><title>IMPLICIT OBJECT</title></head>
<body>
<%
out.println("first statement");
out.println("second");
out.println("third");
%>
</body> </html>
Explanation: Using the method out.println(), this example shows the use of out implicit
object. Output gets shown on the screen.
Implicit Objects
2. request -
• This is an in-built implicit object of
javax.servlet.http.HttpServletRequest .
3. response
• This is an object from HttpServletResponse.
• This object rather gets passed as a parameter to
jspService() like request.
• It is the response given to the user.
• This object can be used for redirect, get header
information, add cookies, send error messages using this
object.
Syntax: response.nethodName();
Implicit Objects
• Output
Implicit Objects
• 4. config
• It is an object of javax.servlet.ServletConfig.
• It is used to get the information regarding configuration of JSP
pages, such as servlet context, servlet name, configuration
parameters, etc..
• For example get Servlet Name etc.
• It gives out the parameters related to servlet mapping
initialization.
Syntax: config.methodName();
Implicit Objects
• 4. config
• Methods used with config object are:
• String getInitParameter(String name) – This method gets the
parameter name.
• Enumeration getInitParameterNames( ) – This method will
return an enumeration of initialization params.
• getServletContext( ) – It will return reference to Servlet context.
• String getServletName() – It returns the name of the servlet which
was defined in the web.xml file used for mapping.
String s = config.getServletName();
Implicit Objects
• 5. session
• session object is the implicit object under
javax.servlet.http.HttpSession .
• It is used to get session information as it tracks sessions between
client requests.
• It can be used to get, set as well as remove under scope of session.
• As session creation and management is a cumbersome project so if
user don’t want a session to be created user can set the session
in page directive as false.
Syntax: session.getAttribute()
Implicit Objects
• 5. session
Methods used with session object are:
• setAttribute(String, object) – This method will assign a string to the
object and save it in session.
• getAttribute(String name) – This method gets the object that is set by
setAttribute.
• removeAttribute(String name) – Objects that are in the session can be
removed using this method.
• getAttributeNames – It returns enumeration of the objects in session.
• getCreationTime – This method returns time when the session was in
active mode
Implicit Objects
• 5. session
Methods used with session object are:
• getId –This method gives out the unique id of the session.
• isNew – This method checks if a session is new or not. It would
return true if cookies are not there.
• getMaxInactiveInterval – Returns time span, the session was
inactive.
• getLastAccessedTime – This method gives the last accessed time
of a session.
• Example: Add Lab Program
Implicit Objects
6. application
• It is an implicit object of ServletContext from javax.servlet.ServletContext
implementation.
• An application object is used to initialize application-wide parameters and
maintain functional data throughout the JSP application.
• This object is created by the web container only when an application is deployed.
• It is generated one per application. As the name suggests, it interacts with servlet.
• It can get set or even remove the attributes under application scope.
• It can exclusively be used to get the object Request Dispatcher. Using Request
Dispatcher, it forwards the request to other pages.
Syntax: application.methodName(“value”);
Implicit Objects
6. application
Methods available under application scope are:
• Object getAttribute(String attributeName):
• This method gets the object available in the attribute and returns
it.
String a = (String)application.getAttribute(“Name”);
6. application
Methods available under application scope are:
• void removeAttribute(String objectName):
• This method will remove an attribute from application.
application.removeAttribute(“Name”);
6. application
Methods available under application scope are:
void log(String message):
• This method stores logs to default log file of JSP Engine.
application.log(“ error 404 page not found”);
• String getServerInfo():
• Returns name and version of web container.
application.getServerInfo();
Implicit Objects
6. application
• Explanation: The object used in
Example:
the example is application along
<html>
with the method getContextPath
<head> method. It gives the Context
<title>Application object</title> Path of the related JSP page.
</head>
<body>
<a>This is an example for application
implicit object</a>
<% application.getRealPath(“/form.html”); • Output:
%>
</body>
</html>
Implicit Objects
7. PageContext
• It is the implicit object from javax.servlet.jsp.PageContext
implementation where PageContext is an abstract
implementation.
• This object has the capability to get, set and remove
attributes that fall under 4 scopes–page, request, session and
application scopes.
Implicit Objects
7. PageContext
• This object has references to other implicit objects.
• It contains information of directives, buffer information,
error page URL.
• It holds reference to request and response as well.
• This can support over 40 methods that are inherited from
ContextClass.
• It is even used to get info of a complete JSP page.
Syntax: pageContext.methodName(“parameter”);
Implicit Objects
8. page
• This implicit object comes under java.lang.Object class.
• Its work is to provide reference to the servlet class generated.
• Type casting is thus required to access this object as shown
below.
<% (HttpServlet)page.log(“message”); %>
Syntax:
Object page=this;
<% this.log(“message”); %>
Implicit Objects
9. Exception
• Exception is an implicit built-in object of java.lang.Throwable.
• This is useful for handling exceptions in JSP.
• As these pages handle errors, they can only be used for JSP
error pages.
• This implies that if <%@ page isErrorPage=”true”%>, then
only user can use this object.
Syntax is as follows:
<%=exception%>
Implicit Objects
form2.html divide.jsp
<html> <%@ page errorPage="exception.jsp" %>
<head> <html>
<body>
<title>Enter two Integers to divide</title>
<%
</head>
String
<body> num1=request.getParameter("number1");
<form action="divide.jsp"> String
Enter First Integer:<input type="text" num2=request.getParameter("number2");
name="number1" /><br/> int n1= Integer.parseInt(num1);
int n2= Integer.parseInt(num2);
Enter Second Integer:<input type="text"
int result= n1/n2;
name="mumber2" />
out.print("Output is: "+ result);
<input type="submit" value="Result"/>
%>
</form>
</body>
</body> </html>
</html>
Implicit Objects
exception.jsp Explanation: In this example html
page opens and asks for input.
<%@ page isErrorPage="true" %>
These entered numbers are
<html> forwarded to divide.jsp that does
<head><title>Exception</title></ calculation and if the page creates an
exception, then exception.jsp is
head> called and it throws
<body> java.langNumberFormatException
and Check the data entered.
<a href: Hey,Got this Exception:
</a><%= exception %> <br/>
<a>Check the data entered.</a>
</body>
</html>
Implicit Objects
9. Exception
• Output
Character Quoting Conventions