Thursday 24 March 2016

SESSION IMPLICIT OBJECT

 
In JSP, session is an implicit object of type HttpSession.The Java developer can use this object to set,get or remove attribute or to get session information.

Example of session implicit object

index.html


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

welcome.jsp


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

second.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>  

Output

jsp session implicit object output 1 jsp session implicit object output 2 jsp session implicit object output 3

pageContext implicit object

In JSP, pageContext is an implicit object of type PageContext class.The pageContext object can be used to set,get or remove attribute from one of the following scopes:
  • page
  • request
  • session
  • application
In JSP, page scope is the default scope.

Example of pageContext implicit object

index.html


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

welcome.jsp


  1. <html>  
  2. <body>  
  3. <%   
  4.   
  5. String name=request.getParameter("uname");  
  6. out.print("Welcome "+name);  
  7.   
  8. pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);  
  9.   
  10. <a href="second.jsp">second jsp page</a>  
  11.   
  12. %>  
  13. </body>  
  14. </html>  

second.jsp


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

Output

jsp pageContext implicit object output 1 jsp pageContext implicit object output 2 jsp pageContext implicit object output 3

page implicit object:

In JSP, page is an implicit object of type Object class.This object is assigned to the reference of auto generated servlet class. It is written as:
Object page=this;
For using this object it must be cast to Servlet type.For example:
<% (HttpServlet)page.log("message"); %>
Since, it is of type Object it is less used because you can use this object directly in jsp.For example:
<% this.log("message"); %>

exception implicit object

In JSP, exception is an implicit object of type java.lang.Throwable class. This object can be used to print the exception. But it can only be used in error pages.It is better to learn it after page directive. Let's see a simple example:

Example of exception implicit object:

error.jsp


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

No comments:

Post a Comment