0% found this document useful (0 votes)
31 views43 pages

Implicit Objects

Uploaded by

kavishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views43 pages

Implicit Objects

Uploaded by

kavishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Implicit Objects Scope And El

Expressions

1. Implicit Objects
2. Character Quoting Conventions
3. Unified Expression Language [Unified El]
4. Expression Language
Implicit Objects

• Web containers create these implicit objects when they translate


JSP pages to servlet.
• They are available to all JSP pages.
• They are written in scriptlet tags except declaration.
• Beacuse everything that is in the scriptlet tag goes to _jspSevice()
method where processing of requests occurs, whereas declaration
goes to source file generated outside _jspService() method.
• They get called directly and need not to be declared.
• They are also called in-built objects / Predefined variables.
Implicit Objects
• There are 9 implicit objects available in the web container.
• Out of these 9, 7 are objects while 2 are passed as parameters to the
_jspService().
1. out
2. request
3. response
4. config
5. session
6. application
7. page
8. pageContext
9. exception
Implicit Objects
1. out
• It is the most used implicit object that comes from
java.servlet.jsp.JspWriter.
• It’s work is to write data into a buffer which would be sent to the
client as output.
• It possesses the access to servlet output stream.
• Servlet uses PrintWriter but JspWriter has some additional features
to it.
• Jsp Writer has options for buffer and I/O Exceptions which are not
available in PrintWriter.
Syntax: out.methodName();
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

1. out - Out uses various methods Example: Out.jsp


Output:
Implicit Objects

2. request -
• This is an in-built implicit object of
javax.servlet.http.HttpServletRequest .

• It is passed as a parameter to jspService method.


• This instance is created each time a request is generated.
• Using this object user can request for a parameter session,
cookies, header information, server name, server port,
contentType, character encoding etc.
• It has following methods:
Syntax: request.methodname(“value”);
Implicit Objects

2. request - Methods under request parameter are:


a. getParameter(): This method is used so that the value of request can be
got.
String obj=request.getParameter(“name”);
b. getParameterNames(): It gives the Enumeration of requested values.
c. getParameterValues(String name): It returns a whole array of
parameters.
d. getAttribute(String name): This method gets the value of the attributes
present.
request.getAttribute(“value”);
e. getAttributeNames(): This method is used to generate all the attributes
that are present in that session.
Implicit Objects

2. request - Methods under request parameter are:


f. setAttribute(String, Object):
This method sets the value of an attribute according to user requirements.
For example, If user want to set password value as admin then,
str=admin
String str=request.setattribute(“Password”, str)

This will set admin as password.

g. removeAttribute(str): This method will remove the mentioned attribute


from that JSP page.
request.removeAttribute(“value”);
Implicit Objects

2. request - Methods under request parameter are:


h. getCookies(): This method of request will give an array of cookie
objects used under cookie handling.
i. getHeader(String name): This method will get the information
regarding the header of the request.
j. getHeaderNames(): This method will return an enumeration of all the
header names available.
k. getRequestURI(): This method will return the Current JSP page’s URL
to the user.
l. getMethod(): This method will return the method used for the request.
For example, GET for getMethod(), POST for postMethod().
m. getQueryString(): This method will return the string in the url after the
question mark for the associated page.
Implicit Objects

2. request - Methods under request parameter are: Example


form.html
<html>
<head><title>My login</title>
</head>
<body>
<form action="user.jsp">
<a>Username:</a><input type="text" name="username"> <br/>
<a>Password:</a><input type="text" name="password">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
Implicit Objects

2. request - Methods under request parameter are: Example


user.jsp
<html>
<head><title>login</title></head><body>
<%
String name=request.getParameter("username");
String pass=request.getParameter("password");
out.print("Welcome user, you entered your name as "+name);
%>
</body>
</html>
Implicit Objects

• Explanation: This example gets the Parameter


value from the user in the form of username and
password using getParameter() and shows a
welcome message to the user.
• Output:
Implicit Objects

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

3. response - Methods used with response object are:


a. void setContentType(String type):
• This method sets the contempt type of browser and browser
interprets according to it.
response.setContentType(“text/html”);
response.setContentType(“image/gif”);

b. void sendRedirect(String address):


• This method will redirect users to other destinations.
response.sendRedirect(“http://www.google.com”);
Implicit Objects

3. response - Methods used with response object are:


c. void addHeader(String name, String value):
• This method adds header to the response
response.addHeader(“Address”,
“Google.com”);
d. void setHeader(String name, String value):
• This method will set the header value
response.setHeader(“Address”, “G.com”);
Implicit Objects

3. response - Methods used with response object are:


e. boolean containsHeader(String name):
• It checks that if header is present or not. It can have 2 values either
true/false
response.containsHeader(“Address”);
f. void addCookie(Cookie value):
• Response will contain cookie using this method.
response.addCookie(Cookie kk);
g. void sendError(int status_code, String message):
• Error response is sent using this method.
response.sendError(404, “error: Page was not found “);
Implicit Objects

3. response - Methods used with response object


user2.jsp
are:
Example: <html>
redirect.html <head><title>login</title></head>
<html>
<head><title>My login</title> </head> <body>
<body> <%
<form action="user2.jsp">
<a>Username:</a><input type="text"
response.sendRedirect("http://
name="username"> <br/> www.google.com");
<a>Password:</a><input type="text"
name="password">
%>
<input type="submit" value="go"><br/> </body>
</form>
</html>
</body></html>
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”);

• void setAttribute(String attributeName, Object object): This


method will set the value of attribute.
application.setAttribute(“Address”, “Value of Address”)
Implicit Objects

6. application
Methods available under application scope are:
• void removeAttribute(String objectName):
• This method will remove an attribute from application.
application.removeAttribute(“Name”);

• String getRealPath(String value):


• This method will return the absolute path of the file.
String abspath = application.getRealPath(“/form.html”);
Implicit Objects

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

• 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%>
Character Quoting Conventions

• 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%>
Character Quoting Conventions

• 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%>

You might also like