• Implicit objects in jsp are the objects that are created by the container automatically and the container makes all these 9 implicit objects to the developer.
  • We do not need to create them explicitly, since these objects are created and automatically by container and are accessed using standard variables(objects) are called implicit objects.
  • The following are of implicit variables available in the JSP.  

9 Jsp implicit Objects

1.out
2.request
3.response
4.config
5.application
6.session
7.pagecontext
8.page
9.exception



1.out:

  •  out is one the implicit object to write the data to the buffer and send output to the client in response.
  • out object allows us to access the servlets output stream and has a pagescope.
  • out.print("");
  • out is the object of javax.servlet.jsp.JspWriter class.
  • While working with servlet we need printWriter object
  • PrintWriter out=response.getWriter();   
  • In servlet we are used PrintWriter to send the output to the client. In JSP we use JSPWriter. 

Difference between JspWriter and PrintWriter:

  • Every JSPWriter is associated with 8KB of internal Buffer. Where as PrintWriter doesn’t associated with any Buffer.
  • The methods of the JspWriter class are designed to throw java.io.IOException where as the methods of PrintWriter class are not throw Exception.
  • JspWriter is an abstract class present in javax.servlet package.
  • PrintWriter is a class defined in java.io package.
  1. public class PrintWriter extends Writer { 
  2. // .....
  3. }
  4.  
  5. public abstract class JspWriter extends Writer {
  6. //....
  7. }

JspWriter out object in jsp:

  1. <html> 
  2. <body> 
  3. <%
  4.     int a = 10;
  5.     int b = 20;
  6.     out.print(“A value is:”+a);
  7.     out.write(“B value is:”+b);
  8. %>
  9. </body> 
  10. </html> 



Printwriter object in servlet:

  1. PrintWriter out=response.getWriter();  

2.request:

  • request implicit object is the object of  type  HttpServletRequest.
  • request object will be created by the container for every request.
  • It will be used to get request information like parameter , header information ,server name server port etc.
  • By using request object we can able to set ,get and remove attributes from request scope.
  • It uses the getPareameter() method to access request parameter.
  • The container passes this object to the _jspService() method.


  1. <form action="welcome.jsp">  
  2. <input type="text" name="username">  
  3. <input type="submit" value="submit"><br/>  
  4. </form> 

  1. <%   
  2. String name=request.getParameter("userid");  
  3. out.print("welcome "+name);  
  4. %> 

  1. out.println(request.getMethod()+”<br>”);
  2. out.println(request.getRequestURI()+”<br>”);
  3. out.println(request.getProtocol()+”<br>”);
  4. out.println(request.getHeader(“User Agent”)+”<br>”);
  5. out.println(request.getHeader(“Accept-language”)+”<br>”);

3.response:

  • response is an instance of class which implements the HttpServletResponse interface.
  • Container generates this object and passes to the _jspService() method as parameter.
  • response object will be created by the container for each request.
  • It represents the response that to be given to the client. 
  • The response implicit object is  used to content type, add cookie and redirect the response to another resource.

1.set content type.

  1. response.setContextType(“text/xml”);
  2. out.println(“<employee>”);
  3. out.println(“<eno>|</eno>”);
  4. out.println(“</employee>”);

2.Redirect response:

  1. <%  
  2.     response.sendRedirect("http://www.instanceofjava.com"); 
  3. %>

3. To send the error message directly to the client:

  1. <%  response.sendError(537,”xyz”); %>

4.config

  • config is one of the implicit object of type javax.servlet.ServletConfig.
  • config object is created by the web container for each jsp page.
  • config object used to get initialization parameters from web.xml file.


  1. <web-app>
  2.  
  3. <servlet>  
  4. <servlet-name>instanceofjava</servlet-name>  
  5. <jsp-file>/welcome.jsp</jsp-file>  
  6. <init-param>  
  7. <param-name>dname</param-name>  
  8. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  9. </init-param>  
  10. </servlet>  
  11.  
  12. <servlet-mapping>  
  13. <servlet-name>instanceofjava</servlet-name>  
  14. <url-pattern>/welcome</url-pattern>  
  15. </servlet-mapping>  
  16.  
  17. </web-app> 

  1. <%   
  2. String driver=config.getInitParameter("dname");  
  3. out.print("driver name="+driver);  
  4. %> 

  • servletConfig is an object used by servlet container to pass the information during initialization.
  • Once if we get the ServletConfig object, we can find the name of the servlet which is configured in web.xml file by using a method getServletName() method.

  1. <%
  2. String name = config.getServletName();
  3. oput.println(name);
  4. %>


5.application:

  • application is one of the implicit object of type javax.servlet.ServletContext.
  • application object is created by the web container one per application when the application gets deployed.
  • application object used to get context parameters from web.xml file.
  • ServletContext object contains set of methods which are used to interact with ServletContainer. By using these methods we can find the information about ServletContainer.
  • When ever sun micro system releases a new version of API. It is the responsibility of server vender to provide the implementation to the latest API. 
  • For example when servlet 2.5 is released Tomcat guys has provided the implementation and integrated it in Tomcat 6.0 the latest version of servlet API is 3.0 this version is integrated in Tomcat 7.0.
     

  1. <web-app>
  2.  
  3. <servlet>  
  4. <servlet-name>instanceofjava</servlet-name>  
  5. <jsp-file>/index.jsp</servlet-class>  
  6. </servlet>  
  7.  
  8. <servlet-mapping>  
  9. <servlet-name>instanceofjava</servlet-name>  
  10. <url-pattern>/index</url-pattern>  
  11. </servlet-mapping> 
  12.  
  13. <context-param>  
  14. <param-name>dname</param-name>  
  15. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  16. </context-param>  
  17.  
  18. </web-app>

  1. <%   
  2. String driver=application.getInitParameter("dname");  
  3. out.print("driver name="+driver);  
  4. %> 

6.session

  • session implicit object is holding HttpSession object.
  • session object used to set, get, and remove attributes to the session scope and also used to get session information.

index.html

  1. <html>  
  2. <body>  
  3. <form action="/welcome">  
  4. <input type="text" name="uname">  
  5. <input type="submit" value="submit"><br/>  
  6. </form>  
  7. </body>  
  8. </html>   

welcome.jsp

  1. <html>  
  2. <body>  
  3. <%   
  4. String name=request.getParameter("uname");  
  5. out.print("Welcome "+name);  
  6. session.setAttribute("user",name);  
  7. <a href="showuserinfo.jsp">showuser details</a>  
  8. %>  
  9. </body>  
  10. </html>   

showuserinfo.jsp

  1. <html>  
  2. <body>  
  3.  
  4. <%   
  5. String name=(String)session.getAttribute("user");  
  6. out.print("Hello "+name);  
  7. %>  
  8.  
  9. </body>  
  10. </html> 
  11. %> 

7.pagecontext:

  • pagecontext implicit object is type of  PageContext.
  • pagecontext object used to set , get and remove attributes in particular scope
  1. page scope
  2. request scope
  3. session scope
  4. application scope

  1. pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);
  2.  String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE); 

8.page

  • Page implicit variable hold the currently executes Servlet object for the corresponding JSP.
  • Acts as this object for current jsp page
  1.  <%=page.getClass().getName() %>
  2.   <%=page.getServlestInfo()%> 

9.exception

  • The implicit variable can be used only in Error pages. When we try to use this variable in a JSP which is not an Error page we get an Error message.

  1.  <%@ page isErrorPage="true" %>  
  2. <html>  
  3. <body>   
  4.  
  5. <%= exception %>  
  6.  
  7. </body>  
  8. </html> 


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu