Implicit Objects in JSP with example.

Implicit objects in JSP is created by the web container. There are total 9 implicit objects are available in JSP. objects are available for the complete JSP page.

Following are the Implicit Objects in JSP.

Implicit Objects Type
request HttpServletRequest
response HttpServletResponse
session HttpSession
config ServletConfig
application ServletContext
pageContext PageContext
page Object
exception Throwable
out JspWriter

request Implicit Objects in JSP

  • The request object is an instance of the class which is implements java .servlet.http.HttpServletRequest interface.
  • Created by the container for every request.
  • request Object is used to request the information like header information, parameter, server name.
  • getParameter() method is used to access the request from the request object.
<%   
String name=request.getParameter("name");  
out.print("welcome "+name);  
%>

response Objects in JSP

  • The response is an instance of the class which implements HttpServletResponse interface.
  • It will be created by the container for each request.
  • It will help to send a response to the client.
  • Mostly it is used to redirect to response page and add the cookie.
<%   
response.sendRedirect("http://www.codebun.com");  
%>

session object in JSP.

  • The session is holding “httpsession” object.
  • A session object is used to get, set and remove attributes to session scope and also used to get session information.

For example, A real-time example when a user login to a website a session object is created for that user and complete profile of that user will be managed by the session object.

Set a session.

<%   
  
String name=request.getParameter("uname");  
 
session.setAttribute("session_name",name);  
  

%>

Get the session at any page in the application.

<%   
  
String name=(String)session.getAttribute("session_name");  
out.print("Hello "+name);  
  
%>

config object in JSP.

  • “config” is of the type java.servlet.servletConfig.
  • A ServletConfig object is created by web container for each servlet to pass information to a servlet during initialization.
  • This object can be used to get configuration information from a web.xml file.

web.xml

<init-param>  
<param-name>var_name</param-name>  
<param-value>Welcome to codebun.com</param-value>  
</init-param>

index.jsp

<%
String message=config.getInitParameter("var_name");  
out.print("driver name is="+message);  

%>

application object in JSP.

  • Application object is an instance of javax.servlet.ServletContext.
  • It is used to get the context information and attributes in JSP.
  • Application object is created by the container.
  • Servletcontext object contains a set of methods which are used to interact with the servlet container.
  • We can find information about the servlet container.

web.xml

<context-param>  
<param-name>var_name</param-name>  
<param-value>Welcome to codebun.com</param-value>  
</context-param>  

index.jsp

<%
String message=application.getInitParameter("var_name");  
out.print("driver name is="+message);  

%>

 pageContext object in JSP.

  • This object is of the type of pagecontext.
  • It is used to get, set and remove the attributes from a particular scope.

There is four type of objects.

  • Page
  • Request
  • Session
  • Application
<% pageContext.setAttribute("key_var","Value : hello JSP Welcome to codebun",pageContext.PAGE_SCOPE);
String msg= (String)pageContext.getAttribute("key_var");
out.println("Message : " +msg);
%>

page object in JSP.

  • Page implicit variable holds the currently executed servlet object for the corresponding JSP.
  • Type of page is Object.
  • Acts as this object for current JSP page.
<% String page_name = page.toString();
out.println("Page Name is " +page_name);%>

exception object in JSP.

  • An exception is the implicit object of the Throwable class.
  • It is used for exception handling in JSP.
<%@ page isErrorPage="true" %>  
<html>  
<body>  
  
Exception occured:<%= exception %>  
  
</body>  
</html>

out Object in JSP.

Out is object of javax.servlet.jsp.jspWriter class.

  • Out is one of the implicit objects to send output to the client as a response.
  • Out object allows us to access the servlet’s output stream.
  • In Jsp web don’t need to create an object of PrintWriter but in Servlet we need to define object of printWriter like”PrintWriter out=response.getWriter(); “
<html>  
<body>  
<% out.print("Welcome to codebun.com! Enjoy learning"); %>  
</body>  
</html>

 

 

Add a Comment

Your email address will not be published. Required fields are marked *

4 × five =