JSP- Implicit objects

JSP Implicit objects

There are 9 jsp implicit objects. These objects are created by the web container that are available to all the jsp pages.

Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
Index.jsp
--------------------------------
<% 
out.print("1.welcome to jsp"); 

String name=request.getParameter("a");  
out.print("<br> 2.Request :"+name);  

response.sendRedirect("http://www.google.com");  
out.print("3.Responce :  ");

String cfg=config.getInitParameter("config");  
out.print("<br> 4.Config ="+cfg);  

String cxt=application.getInitParameter("context");  
out.print("<br> 5.Application ="+cfg);

session.setAttribute("user","Satya");  
out.print("<br> 6.Session ="+session.getAttribute("user"));
%>


web.xml
------------------------------
<web-app>
	<servlet>
 <servlet-name>jsp</servlet-name>
 <jsp-file>/index.jsp</jsp-file>
 <init-param>
 	<param-name>config</param-name>
 	<param-value>iam Config Value</param-value>
 </init-param>
	</servlet>

	<servlet-mapping>
 <servlet-name>jsp</servlet-name>
 <url-pattern>/jsp</url-pattern>
	</servlet-mapping>

	<context-param>
 <param-name>context</param-name>
 <param-value>iam Context Value</param-value>
	</context-param>
 
</web-app>


7.pageContext implicit object

The pageContext object can be used to set, get or remove attributes from one of the following scopes:

  • Page PageContext.PAGE_SCOPE

  • Request PageContext.REQUEST_SCOPE

  • Session PageContext.SESSION_SCOPE

  • Application PageContext.APPLICATION_SCOPE

pageContext.setAttribute("name"," value",PageContext.SESSION_SCOPE);
first.jsp
------------------------
<html>  
<body>  
<%   
  
String name=request.getParameter("uname");  
out.print("Welcome "+name);  
  
pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);  
  
<a href="second.jsp">second jsp page</a>  
  
%>  
</body>  
</html>  

Second.jsp
-----------------------
<html>  
<body>  
<%    
String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);  
out.print("Hello "+name);    
%>  
</body>  
</html>


8. Page implicit object
Page is an implicit object of type Object class


9. Exception

  • Exception is an implicit object of type java.lang.Throwable class.

  • This object can be used to print the exception.

  • it can only be used in error pages

<%@ page isErrorPage="true" %>  
<html>  
<body>  
  
Sorry following exception occured:<%= exception %>  
  
</body>  
</html>