x

JSP Implicit Objects

PREV     NEXT

JSP container makes some Java objects available to the JSP page. No specific declaration or initialization is required within the JSP page. These objects are called implicit objects. List of the JSP implicit objects is included below


Object Name Object type
  request returns the session idjavax.servlet.http.HttpServletRequest
response javax.servlet.http.HttpServletResponse
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
pageContext javax.servlet.jsp.PageContext
exception java.lang.Throwable
  config javax.servlet.ServletConfig
page Object

Request implicit object:

The request implicit object represents the data that is coming as input into the page. The request object is of an instance of class http.HttpServletRequest. The object can be used to access parameter names and values, http method type, URL of the request etc.Example below illustrates the usage of some of the methods of request object.

Parameter names: <%=request.getParameterNames()%><br>

Parameter values: <%=request.getParameter(“paramname”)%><br>

HTTP method name: <%=request.getMethod()%><br>

request URL: <%=request.getRequestURL()%><br>

query string: <%=request.getQueryString()%>

The result below assumes the URL of http://www.javaprepare.com/scwd/notes/implicitobject.html?paramname=paramvalue . The following output gets generated.

Parameter names: java.util.Hashtable$Enumerator@a7dd39
Parameter values: paramvalue
HTTP method name: GET
request URL: http://www.javaprepare.com/scwd/notes/implicitobject.html
query string: paramname=paramvalue

request.getParameterNames returns an Enumeration object with the names of all request parameters. The method request.getParameter(String name) returns the value of the parameter. In this case it returns the string paramvalue.

The function request.getMethod returns the HTTP method (GET, POST etc.) used to access the page. In this case the getMethod function prints GET.

The function request.getRequestURL() prints complete URL without the query string. In this case it returns – http://www.javaprepare.com/scwd/notes/implicitobject.html .

The function request.getQueryString() returns the argument/query string that is passed to the JSP page. In this case it returns paramname=paramvalue.

The complete list of methods of HttpServletRequest is available at the Suns site.

Response implicit object:

The response implicit object refers to the response that gets sent by the JSP page. Some of the key methods of response are –

  1. The method setStatus(code) sets the status code for response. For example setStatus(SC_OK) sets the response to OK. SC_OK is a constant in the interface HttpServletResponse with value zero. For a list of legal constants in HttpServletResponse refer to the Servlet API
  2. In case an error is encountered while processing, sendError method sends the error response to the client. sendError takes an integer representing an error code. The second argument is optional. It represents a String with the error message.
  3. sendRedirect(URL) is used to redirect to a new URL. Browser displays the contents of the URL specified by sendRedirect. As an example
<%sendRedirect(“www.javaprepare.com”);%>

will redirect browser to www.javaprepare.com instead of processing the current page.

Out implicit object:

The out implicit object is used to print contents from a scriptlet.

<%! int n=8, logi=0;%>
<% for(int i=1; i<n; i=i*2) {
logi++;
}
out.println(“log of ” + n + ” is ” + logi);
%>

The example above invokes the out implicit object to print the log to the base two, of a number.


Session implicit object:

The session implicit object stores information about current user’s session. Session object can be used to pass information from one page to another for a user. The information can be stored in session using attributes in a page and retrieved in another page of the same session.session.setAttribute takes two values, attribute name and attribute value. The corresponding getAttribute takes the attribute name as argument and returns the value.

<%
session.setAttribute(“country”,”USA”);
out.println(session.getAttribute(“country”));
%>

The example sets the session attribute country and then displays it. In a typical scenario attribute will be set in a page and then accessed in another page.

Some other important methods of session variable are

Method Description
String getId() returns the session id
int getMaxInactiveInterval() returns the time (in seconds) after receiving a request in seconds when the session becomes inactive.

An example that illustrates the usage of these methods is included below

<%
out.println(session.getId());
out.println(session.getMaxInactiveInterval());
%>

The above code prints the following on my instance of Tomcat server.

3EC9E7825173C32868FC0A8ADACA0EBD
1800

The session timeout of 1800 seconds, which is 60 minutes, is printed by the method getMaxInactiveInterval. If the session attribute of page directive is set to false, then session implicit variable is not available to the page.

Application implicit object:

implicit object application represents the web application to which the JSP page belongs. Two important uses of application implicit object are –

1. It provides access to the writer that can be used to display error and warning messages. The example below illustrates this.

<%
application.log(“This debug message is printed on the server log and not on the browser.”);
%>

2. The application object also provides mechanism to access initialization parameter for the application.

<%
String str = application.getInitParameter(countryName”);
%>

PageContext implicit object:

pageContext implicit object provides methods to access other implicit objects. Specifically pageContext object has methods like getPage, getRequest, getResponse etc. It also provides methods to set and get attributes. The scope of the pages attribute is the processing of that page.

Exception implicit object:

The exception implicit object is used for error handling. The exception object is available only if isErrorPage attribute of page directive is set to true. It has two important methods –

  1. getMessage() to print the error message for the exception.
  2. printStackTrace(output) is used to print the execution stack.

Config implicit object:

The config implicit object represents intialization parameter for the servlet into which JSP page is compiled. The config object is an instance of javax.servlet.ServletConfig . The config object is used to access servlets initialization parameters. It defines two methods –

<%
java.util.Enumeration getInitParameterNames();
String getInitParameter(String name);
%>

Lets say the deployment descriptor of the web application includes the following –

<%
<servlet>
<init-param>
<param-name>country</param-name>
<param-value>usa</param-value>
</init-param>
</servlet> %>

Now to display the country parameter, the following JSP can be used <%= config.getInitParameter(“country”)%>

Page implicit object:

The page implicit object represents the JSP page itself. It is of type object. It is rarely used in JSP pages.

PREV     NEXT



Like it? Please Spread the word!