JSP : Predifined variables | implicit objects

There are following implicit objects those become available to programmer to use in JSP page.

 

out

The out implicit object is an instance of a javax.servlet.jsp.JspWriter object and is used to send content in a response. The JspWriter object emulates some of the functionality found in the java.io.PrintWriter and java.io.BufferedWriter objects to provide a convenient method of writing text in a buffered fashion. The out implicit object can be configured on a per JSP basis by the page directive. Example :

<HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<% out.write("Server Date is: "); %>
<% out.write(new java.util.Date().toString()); %>
</BODY>
</HTML>

 

request

The request implicit object is an instance of a javax.servlet.http.HttpServletRequest object. The request implicit object represents a client's request and is a reference to the HttpServletRequest object passed into a HttpServlet's appropriate service method.

 <HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%= request.getParameter("text")%>
</BODY>
</HTML>

Type the following url on address bar: http://localhost:8080/jsp1/requestobject.jsp?text=hello user Output is : hello user

 

response

The response implicit object is an instance of a javax.servlet.http.HttpServletRequest object. The response implicit object represents a response to a client's response and is a reference to the HttpServletResponse object passed into a HttpServlet's appropriate service method.

 <HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%
response.setContentType("text/html");
java.io.PrintWriter pw=response.getWriter();
pw.println("Hello world");
%>
</BODY>
</HTML>

 

session

The session implicit object is an instance of a javax.servlet.http.HttpSession object. By default JSP creates a keep session context with all clients. The session implicit object is a convenience object for use in scripting elements and is the equivalent of calling the HttpServletRequest getSession() object.

<HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%
int hits=0;
String h=""+session.getAttribute("hits");
if(h==null){
hits=0;
}else{
hits=Integer.parseInt(h);
}
hits++;
out.write("Total Hits : "+hits);
session.setAttribute("hits",""+hits);
%>
</BODY>
</HTML>

Output: Total Hits : 4

 

application

The application implicit object is an instance of a javax.servlet.ServletContext object. The application implicit object represents a Servlet's view of a Web Application and is equivalent to calling the ServletConfig getServletContext() method.

<HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%
int hits=0;
String h=""+application.getAttribute("hits");
if(h==null||h.equals("null")){
hits=0;
}else{
hits=Integer.parseInt(h);
}
hits++;
out.write("Total Hits : "+hits);
application.setAttribute("hits",""+hits);
%>
</BODY>
</HTML>

Output: Total Hits : 4

 

config

The normal JSP deployment scheme automatically done by a container works, but nothing stops a JSP developer from declaring and mapping a JSP via the Web Application Deployment Descriptor, web.xml. A JSP can be manually deployed in the same fashion as a Servlet by creating a Servlet declaration in web.xml and replacing the servlet-class element with the jsp-page element. After being declared, the JSP can be mapped to a single or set of URLs same as a Servlet. Example to passing initial parameters to JSP page using config object.

In Web.xml 

  <web-app>
<servlet>
<servlet-name>configobject.jsp</servlet-name>
<jsp-file>/configobject.jsp</jsp-file>
<init-param>
<param-name>author</param-name>
<param-value>Hemraj</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>configobject.jsp</servlet-name>
<url-pattern>/configobject.jsp</url-pattern>
</servlet-mapping>
</web-app> 

 In configobject.jsp 

 <HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<h2>Author of Page:</h2>
<h3><%=""+config.getInitParameter("author")%></h3>
</BODY>
</HTML>

 

page

The page implicit object represents the current class implementation of the page being evaluated. If the scripting language of the page is java, which by default it is, the page object is equivalent to the keyword of a Java class. This object is used to refer the instance of current JSP page created after translation in servlet. We can use it to differentiate local and instance variable as shown in following example.

<HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%out.write(page.toString());%>
</BODY>
</HTML>

 

exception

The exception object is available only within error pages. It is a reference to the java.lang.Throwable object that caused the server to call the error page. You would use it just as you would use any other Exception or Error object in the catch block of a try-catch section of code. The exception object has page scope.
Example :

 <%@page contentType="text/html" pageEncoding="UTF-8"%> <html>
<head>
<title>JSP Page</title>
</head>
<body>
<h2>Error/Exception :</h2>
<%=exeception.getMessage()%>
</body>
</html>

 

pageContext

The pageContext implicit scripting variable is an instance of a javax.servlet.jsp.PageContext object. A PageContext object represents the context of a single JavaServer Page including all the other implicit objects, methods for forwarding to and including Web Application resources, and a scope for binding objects to the page. The PageContext object is not always helpful when used by itself because the other implicit objects are already available for use. A PageContext object is primarily used as a single object that can easily be passed to other objects such as custom actions. This is useful since the page context holds references to the other implicit objects.

 <HTML>
<HEAD>
<TITLE>JSP Demo</TITLE>
</HEAD>
<BODY>
<%
int hits=0;
String h=""+ pageContext.getAttribute("hits");
if(h==null||h.equals("null")){
hits=0;
}else{
hits=Integer.parseInt(h);
}
hits++;
out.write("Total Hits : "+hits);
pageContext.setAttribute("hits",""+hits);
%>
</BODY>
</HTML>

Output: Total Hits : 4

Popular Posts