Tuesday, 5 February 2013

IMPLICIT OBJECTS | J2EE Tutorial pdf

IMPLICIT OBJECTS

We have implicit access to certain objects that are available for use in all JSP documents. These objects are parsed by the JSP engine and inserted into the generated servlet as if we defined them ourself.
out
The implicit out object represents a JspWriter (derived from a java.io.Writer) that provides a stream back to the requesting client. The most common method of this object is out.println(), which prints text that will be displayed in the client’s browser. Next Listing provides an example using the implicit out object.
<%@ page errorPage="errorpage.jsp" %>
<html>
<head>
<title>Use Out</title>
</head>
<body>
<%
// Print a simple message using the implicit out object.
out.println("<center><b>Hello Wiley" + " Reader!</b></center>");
%>
</body>
</html>
request
The implicit request object represents the javax.servlet.http.HttpServletRequest interface, discussed later in this chapter. The request object is associated with every HTTP request. One of the more common uses for the request object is to access request parameters. We can do this by calling the request object’s getParameter() method with the parameter name we are seeking. It will return a string with the value matching the named parameter. An example using the implicit request object appears in Listing
<%@ page errorPage="errorpage.jsp" %>
<html>
<head>
<title>UseRequest</title>
</head>
<body>
<%
out.println("<b>Welcome: " + request.getParameter("user") + "</b>");
%>
</body>
</html>
This JSP calls the request.getParameter() method, passing it the parameter user. This method looks for the key user in the parameter list and returns the value, if it is found.
response
The implicit response object represents the javax.servlet.http.HttpServletResponse object. The response object is used to pass data back to the requesting client. This implicit object provides all the functionality of the HttpServletRequest, just as if we were executing in a servlet. One of the more common uses for the response object is writing HTML output back to the client browser; however, the JSP API already provides access to a stream back to the client using the implicit out object, as described in the previous implicit out discussion.
pageContext
The pageContext object provides access to the namespaces associated with a JSP page. It also provides accessors to several other JSP implicit objects. A common use for the pageContext is setting and retrieving objects using the setAttribute() and getAttribute() methods.
session
The implicit session object represents the javax.servlet.http.HttpSession object. It’s used to store objects between client requests, thus providing an almost stateful HTTP interactivity.
An example of using the session object
<%@ page errorPage="errorpage.jsp" %>
<html>
<head>
<title>Session Example</title>
</head>
<body>
<%
// get a reference to the current count from the session
Integer count = (Integer)session.getAttribute("COUNT");
if ( count == null ) {
// If the count was not found create one
count = new Integer(1);
// and add it to the HttpSession
session.setAttribute("COUNT", count);
}
else {
// Otherwise increment the value
count = new Integer(count.intValue() + 1);
session.setAttribute("COUNT", count);
}
out.println("<b>We have accessed this page: "
+ count + " times.</b>");
%>
</body>
</html>
Output:
It will display the number of time accessed the page.
application
The application object represents the javax.servlet.ServletContext, discussed earlier in this chapter. The application object is most often used to access objects stored in the ServletContext to be shared between Web components in a global scope. It is a great place to share objects between JSPs and servlets. An example using the application object can be found earlier in this chapter, in the section “The ServletContext.”
config
The implicit config object holds a reference to the ServletConfig, which contains configuration information about the JSP/servlet engine containing the Web application where this JSP resides.
page
The page object contains a reference to the current instance of the JSP being accessed. The page object is used just like this object, to reference the current instance of the generated servlet representing this JSP.
exception
The implicit exception object provides access to an uncaught exception thrown by a JSP. It is available only in the attribute isErrorPage set to true.

No comments: