Implicit Object in JSP

JSP Implicit Object

JSP implicit objects are created by the web container and available to all JSP pages. They are basically Java object which also called pre-defined variables.

There are 9 implicit objects in JSP:

ObjectAvailability and Uses
RequestThe javax.servlet.http.HttpServletRequest object is associated with the request.
ResponseThe javax.servlet.http.HttpServletResponse object is associated with the response.
OutThe javax.servlet.jsp.JspWriter object is used to send output the client.
SessionThe javax.servlet.http.HttpSession object is associated with the session for the given client request.
ApplicationThe javax.servlet.ServletContext object is used for the web application.
ConfigThe javax.servlet.ServletConfig object is associated with the Servlet for current JSP page.
pageContextThe javax.servlet.jsp.PageContext object encapsulates the environment of a single request for the current JSP page.
PageThe page variable is similar to this variable of Java programming. It is available in java.lang.Object class.
ExceptionThe Exception object represents the Throwable object that was thrown by some other JSP page.

Example : Implementing Request Object

//welcome.html

<html>
     <head>
           <title>Username & Password</title>
     </head>
<body>
     <form action="welcome.jsp">
           Enter User Name: <input type="text" name="uname" /> <br>
           Enter Password: <input type="text" name="pass" /> <br>
           <input type="submit" value="Submit"/>
           <input type = "submit" value = "Reset">
     </form>
</body>
</html>

//welcome.jsp

<%@ page import = " java.util.* " %>
<html>
<body>
    <%
         String username = request.getParameter("uname");
         String password = request.getParameter("pass");
         out.print("Welcome: "+username);
    %>
</body>
</html>


Example : Implementing session Object

//index.html

<html>  
<body>  
      <form action="session.jsp">  
            <input type="text" name="uname">  
            <input type="submit" value="Click here!"><br/>  
      </form>  
</body>  
</html>

//session.jsp

<html>
    <head>
        <title>Passing Session Variables</title>
    </head>
<body>  
    <%   
        String name = request.getParameter("uname");  
        out.print("Welcome "+name);  
        session.setAttribute("user",name);  
        <a href="display.jsp">Display Page</a>  
    %>  
</body>  
</html>

//display.jsp

<html>  
<body>  
     <%   
         String name=(String)session.getAttribute("user");  
         out.print("Welcome "+name);  
     %>  
</body>  
</html>


Example : Implementing config Object

//welcome.html

<form action="welcome.jsp">  
      <input type="text" name="uname">  
      <input type="submit" value="Click here!!"><br/>  
</form>  

//web.xml

<web-app>
<servlet>
     <servlet-name>TutorialRide</servlet-name>
     <jsp-file>/welcome.jsp</jsp-file>
</servlet>

<servlet-mapping>
     <servlet-name>TutorialRide</servlet-name>
     <url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>

//welcome.jsp

<html>
    <head>
        <title>Config Implicit Object</title>
    </head>
<body>
    <%
         String sname = config.getServletName();
         out.print("Servlet Name: "+sname);
         String
    %>
</body>
</html>