Enterprise Application Development With Java EE
Enterprise Application Development With Java EE
3/23/22 1
JSP Implicit Objects
• These objects are created by JSP Engine during
translation phase (while translating JSP to
Servlet). They are being created inside service
method so we can directly use them within
Scriptlet without initializing and declaring
them. There are total 9 implicit objects
available in JSP.
3/23/22 2
Implicit Objects and their corresponding
classes:
out javax.servlet.jsp.JspWriter
request javax.servlet.http.HttpServletRe
quest
response javax.servlet.http.HttpServletRes
ponse
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
exception javax.servlet.jsp.JspException
page java.lang.Object
pageContext javax.servlet.jsp.PageContext
config javax.servlet.ServletConfig
3/23/22 3
Implicit Object Out
• This is used for writing content to the client (browser). It has
several methods which can be used for properly formatting
output message to the browser and for dealing with the buffer.
• Methods of OUT Implicit Object
– void print()
void println()
void newLine()
void clear()
void clearBuffer()
void flush()
boolean isAutoFlush()
int getBufferSize()
int getRemaining()
3/23/22 4
Implicit Object Out
• void newLine(): This method adds a new line to the output.
Example – out.print(“This will write content without a new
line”); out.newLine(); out.print(“I’m just an another print
statement”);
• void clear(): It clears the output buffer without even letting
it write the buffer content to the client. . This is how it can
be called: out.clear();
• void clearBuffer(): This method is similar to the clear()
method. The only difference between them is that when
we invoke out.clear() on an already flushed buffer it throws
an exception, however out.clearBuffer() doesn’t.
3/23/22 5
Implicit Object Out
• void flush() : This method also clears the buffer just
like clear() method but it forces it to write the content
to the output before flushing it.
• boolean isAutoFlush() : It returns a Boolean value
true/false. It is used to check whether the buffer is
automatically flushed or not.
• int getBufferSize(): This method returns the size of
output buffer in bytes. int getRemaining(): It returns
the number of bytes remaining before hitting the
buffer overflow condition.
3/23/22 6
Implicit Object Request
The main purpose of request implicit object is to
get the data on a JSP page which has been
entered by user on the previous JSP page. While
dealing with login and signup forms in JSP we
often prompts user to fill in those details, this
object is then used to get those entered details
on an another JSP page (action page) for
validation and other purposes.
3/23/22 7
Implicit Object Request
1. getParameter(String name) – This method is used to get the value of a
request’s parameter. For example at login page user enters user-id and
password and the login page gets redirected to user information page,
then using request.getParameter we can get the value of user-id and
password which user has input at the login page.
– String Uid= request.getParameter("user-id");
– String Pass= request.getParameter("password");
2. getParameterNames() – It returns enumeration of all the parameter
names associated to the request.
– Enumeration e= request.getParameterNames();
3. getParameterValues(String name) – It returns the array of parameter
values.
– String[] allpasswords = request.getParameterValues("password");
3/23/22 8
Implicit Object Request
1. getAttribute(String name) – Used to get the attribute value.
– request.getAttribute(“admin”) would give you the value of attribute admin.
2. getAttributeNames() – It is generally used to get the attribute names associated to the
current session. It returns the enumeration of attribute names present in session.
– Enumerator e = request.getAttributeNames();
3. setAttribute(String,Object) – It assigns an object’s value to the attribute. For example I
have an attribute password and a String object str which has a value “admin” then
calling request.setAttribute(“password”, str) would assign a value admin to the
attribute password.
4. removeAttribute(String) – By using this method a attribute can be removed and
cannot be used further. For example If you have a statement
request.removeAttribute(“userid”) on a JSP page then the userid attribute would be
completely removed and request.getAttribute(“userid”) would return NULL if used
after the removeAttribute method.
5. getCookies() – It returns an array of cookie objects received from the client. This
method is mainly used when dealing with cookies in JSP.
3/23/22 9
Implicit Object Request
1. getHeader(String name) – This method is used to get the header
information of the request.
2. getHeaderNames() – Returns enumerator of all header names. Below code
snippet would display all the header names associated with the request.
– Enumeration e = request.getHeaderNames();
– while (enumeration.hasMoreElements())
– { String str = (String)e.nextElement();
– out.println(str); }
3. getRequestURI() – This method (request.getRequestURI()) returns the URL
of current JSP page.
4. getMethod() – It returns HTTP request method. request.getMethod(). For
example it will return GET for a Get request and POST for a Post Request.
5. getQueryString() – Used for getting the query string associated to the JSP
page URL. It is the string associated to the URL after question mark sign (?).
3/23/22 10
Practice Example
1. Index.html
1. <html> <head> <title>Enter UserName and Password</title>
</head> <body> <form action="userinfo.jsp"> Enter User
Name: <input type="text" name="uname" /> <br><br> Enter
Password: <input type="text" name="pass" /> <br><br>
<input type="submit" value="Submit Details"/> </form>
</body> </html>
2. userinfo.jsp
1. <%@ page import = " java.util.* " %> <html> <body> <%
String username=request.getParameter("uname"); String
password=request.getParameter("pass"); out.print("Name:
"+username+" Password: "+password); %> </body> </html>
3/23/22 11
Implicit Object Response
• It is an instance of javax.servlet.http.HttpServletRequest
and mainly used for modifying the response which is being
sent to the browser after processing the client’s request.
• void setContentType(String type) – This method tells
browser, the type of response data by setting up the MIME
type and character encoding. The information sets by this
method helps browser to interpret the response. Example
– response.setContentType("text/html");
– response.setContentType("image/gif");
– response.setContentType("image/png");
– response.setContentType("application/pdf");
3/23/22 12
Implicit Object Response
• void sendRedirect(String address) – It redirects the control to a new JSP
page. For example, When the browser would detect the below statement,
it would be redirected to the beginnersbook.com from the current JSP
page.
– response.sendRedirect("http://beginnersbook.com");
• void addHeader(String name, String value) – addHeader method adds a
header to the response, basically it includes a header name and it’s value.
For example – The below statement will include a header “Site” in the
response with value “BeginnersBook.com”.
– response.addHeader("Site", "BeginnersBook.com");
• void setHeader(String name, String value) – It sets the header value. This
method overrides the current value of header with the new value. Let’s
say I’m modifying the value of Header “Site“. The below statement would
modify the current value BeginnersBook.com to a new value BB.com
– response.setHeader("Site", "BB.com");
3/23/22 13
Implicit Object Response
• boolean containsHeader(String name) – It returns a Boolean value
true/false. It basically checks the whether the header is present in the
response or not. For example – Above, in the addHeader method
example we have added a Site Header in response so the below
statement would return true.
– response.containsHeader("Site");
• void addCookie(Cookie cookie) – This method adds a cookie to the
response. The below statements would add 2 Cookies Author and
Siteinfo to the response.
– response.addCookie(Cookie Author);
– response.addCookie(Cookie Siteinfo);
• void sendError(int status_code, String message) – It is used to send
error response with a code and an error message. For example –
– response.sendError(404, "Page not found error");
3/23/22 14
Implicit Object Response
• boolean isCommitted() -It checks whether the Http
Response has been sent to the client, if yes then it
returns true else it gives false.
– <% if(response.isCommited()) {
– <%--do something --%>
– }else { <%--do something else --%> } %>
• void setStatus(int statuscode) – This method is used
to set the HTTP status to a given value. For example,
the below statement would set HTTP response code to
404 (Page not found).
– response.setStatus(404);
3/23/22 15
Practice Example
• Index.html
– <html> <head> <title>Login Page</title> </head> <body> <form
action="checkdetails.jsp"> UserId: <input type="text" name="id" />
<br><br> Password: <input type="text" name="pass" /> <br><br>
<input type="submit" value="Sign In!!"/> </form> </body> </html>
• Checkdetails.jsp
– <html> <head><title>Check Credentials</title> </head> <body> <%
String uid=request.getParameter("id"); String
password=request.getParameter("pass");
session.setAttribute("session-uid", uid); if(uid.equals(”qaiser") &&
password.equals(”jspbook"))
{ response.sendRedirect("success.jsp"); } else
{ response.sendRedirect("failed.jsp"); } %> </body> </html>
3/23/22 16
Practice Example
• Success.jsp
– <html> <head><title>Success Page</title> </head>
<body> <% String
data=(String)session.getAttribute("session-uid");
out.println("Welcome "+ data+"!!"); %> </body> </html>
• Failed.jsp
– <html> <head><title>Sign-in Failed Page</title> </head>
<body> <% String
data2=(String)session.getAttribute("session-uid");
out.println("Hi "+ data2+". Id/Password are wrong. Please
try Again."); %> </body> </html>
3/23/22 17