Implicit Objects
Implicit objects /Implicit reference variable:-
-->Technically speaking we should call them as implicit reference variables because these are local variables in _jspService(-,-) method pointing to the container created objects like request, response, ServletConfig, ServletContext and etc... objects
Implicit objects:-
(1)request
(2)response
(3)config
(4)application
(5)page
(6)PageContext
(7)session
(8)out
(9)exception
Reference type
(1)javax.servlet.HttpServletRequest(I)
(2)javax.servlet.HttpServletResponse(I)
(3)javax.servlet.ServletConfig(I)
(4)javax.servlet.ServletContext(I)
(5)java.lang.Object(c)
(6)javax.servlet.http.tagext.PageContext(AC)
(7)javax.servlet.http.HttpSession(I)
(8)javax.servlet.jsp.JspWriter(AC)
(9)java.lang.Throwable(c)
Scopes
(1)request
(2)request /response
(3)page
(4)application scope
(5)page scope
(6)page
(7)session
(8)page
(9)page
-->The above given reference types are not class names of implicit object there are ordinary interfaces and abstract class and top most hierarchy classes pointing to the container supplied object
Page scope: Data is specific to each jsp pages
Request scope: Data is visible through out request
Session scope: Data is specific to one browser software
Application scope: Data is visible in the entire web application irrespective of any connections
-->The implicit objects of jsp are the objects of underlying ServletContainer, JspContainer supplied java classes implementing the above given interfaces or extending from the above given class or abstract class
-->"out" is not the object of "JspWriter(AC)" , it is the object of underlying ServletContainer/ JspContainer supplied java class that extends from JspWriter(AC). In Tomcat server this class name is "JspWriterImpl" (look in jasper jar file)
E.g
- out object class name:<%=out.getClass()>
-->We generally feel exception handling in jsp is optional, but it is only for the code that goes to _jspService(-,-) method(scriptlet code and expression tag code). The code that goes outside of _jspService(-,-) method like declaration tag code we must explicitly catch and handle the exception
-->All implicit object /reference variable or local variables in _jspService(-,-). So they can't be used in declaration tag because, the code placed in declaration tag go outside of _jspService(-,-) in JES class, but we can access some implicit object by creating separate reference variable by using servlet-api in declaration tags
-->ServletConfig object is container created object on one per servlet/ JES class object either during server startup or during the deployment of web application "config" is the implicit reference variable in _jspService(-,-) pointing to ServletConfig object but it can't be used in declaration class because it is local variable to _jspService(-,-), so we can write servlet-api code in declaration tag to get another reference variable pointing to ServletConfig object
- <%! public void jspInit()
- {
- ServletConfig cg=getServletConfig();
- }
- %>
-->One object can have multiple references, if the implicit reference variable given by jsp is not visible then we can create some explicit reference variable for the same objects
-->We need ServletConfig object in jspInit() and also in other parts of jsp to read init() parameter values of jsp that are specified in web.xml file
In test.jsp
- public void jspInit()
- {
- ServletConfig cg=getServletConfig();
- System.out.println("DB user init parameter value" + cg.getInitParameter("db user")
- }
In web.xml
- <servlet>
- <servlet-name>test</servlet-name>
- <jsp-file>/test.jsp</jsp-file>
- <init-params>
- <param-name>dbuser</param-name>
- <param-value>scott</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>test</servlet-name>
- <url-pattern>/testurl</url-pattern>
- </servlet-mapping>
URL:
http://localhost:1025/JspApp2/testurl
Note: The configurations done in web.xml for jsp will be applied only when jsp is requested with url pattern
Comments
Post a Comment
Express your feelings