In this article, we will see JSP Implicit object with Examples,
There are total 9 Implicit Objects in JSP, they are below as follows,
1. Out Object
2. request Object
3. response Object
4. config Object
5. application Object
6. session Object
7. pageContext Object
8. page Object
9. exception Object
Let's see one by one implicit Object in detail with Examples ...
1. out Object:
JSP out implicit object is instance of javax.servlet.jsp.JspWriter implementation and it’s used to output content to be sent in client response. This is one of the most used JSP implicit objects and that's why we have JSP Expression to easily invoke out.print() method. In the case of Servlet we need to write:
Syntax:
PrintWriter out=response.getWriter(); |
But in JSP, you do not need to write this code. Because in JSP for print any result on the browser we use expression tag and this tag is auto-converted to out.print() statement and insert the out.print() into _jspService() of the servlet class by the container.
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% out.print("Welcome"); %>
</body> </html> |


<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="Output.jsp"> <input type="text" name="uname"> <input type="submit" value="go"> <br /> </form> </body> </html> |


<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String name=request.getParameter("uname"); out.print("welcome "+name); %> </body> </html> |


<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% response.sendRedirect("https://javatechseries.blogspot.com"); %> </body> </html> |


<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String
name=config.getInitParameter("name"); out.print("Programming name is = "+name); %> </body> </html> |

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>BasicPrograms</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
<servlet> <servlet-name>Home</servlet-name> <jsp-file>/configObject.jsp</jsp-file> <init-param> <param-name>name</param-name> <param-value>JavaTechSeries</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Home</servlet-name> <url-pattern>/configObject.jsp</url-pattern> </servlet-mapping> </web-app> |


Example of Application implicit object:
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="Application.jsp"> <input type="submit" value="click to check context
value"><br/> </body> </html> |

<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String
info=application.getInitParameter("info"); out.print("Program info = "+info); %> </body> </html> |


<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>BasicPrograms</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet-mapping> <servlet-name>One</servlet-name> <url-pattern>/Application.jsp</url-pattern> </servlet-mapping>
<context-param> <param-name>info</param-name> <param-value>Example of application object</param-value> </context-param> </web-app> |
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="Session1.jsp"> <input type="text" name="uname"> <input type="submit" value="go"><br/> </form> </body> </html> |


<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String
name = request.getParameter("uname"); out.print("Welcome " + name); session.setAttribute("user", name); %> <a href="session2.jsp">second jsp page</a> </body> </html> |


<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String
name = (String) session.getAttribute("user"); out.print("Hello " + name); %>
</body> </html> |

<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String name="JavaTechSeries"; pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE); pageContext.setAttribute("user",name,PageContext.PAGE_SCOPE); pageContext.setAttribute("user",name,PageContext.REQUEST_SCOPE); pageContext.setAttribute("user",name,PageContext.APPLICATION_SCOPE); String
sname=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE); String pname=(String)pageContext.getAttribute("user",PageContext.PAGE_SCOPE); String
rname=(String)pageContext.getAttribute("user",PageContext.REQUEST_SCOPE); String
aname=(String)pageContext.getAttribute("user",PageContext.APPLICATION_SCOPE); %> <b>Value in Session scope <%=sname%></b><br> <b>Value in Request scope <%=rname%></b><br> <b>Value in Page scope <%=pname%></b><br> <b>Value in Application scope <%=aname%></b><br> <a href="PageContext.jsp">One jsp page</a> </body> </html> |

<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String
sname=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE); String
pname=(String)pageContext.getAttribute("user",PageContext.PAGE_SCOPE); String
rname=(String)pageContext.getAttribute("user",PageContext.REQUEST_SCOPE); String
aname=(String)pageContext.getAttribute("user",PageContext.APPLICATION_SCOPE); %> <b>Value in Session scope <%=sname%></b><br> <b>Value in Request scope <%=rname%></b><br> <b>Value in Page scope <%=pname%></b><br> <b>Value in Application scope <%=aname%></b><br> </body> </html> |


<%
(HttpServlet)page.log("message"); %> |
<%
this.log("message"); %> |
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="Operation.jsp"> Input First Integer:<input type="text" name="firstnum" /> Input Second Integer:<input type="text" name="secondnum" /> <input type="submit" value="Get Results"/> </form> </body> </html> |
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page errorPage="exception.jsp" %> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String num1=request.getParameter("firstnum"); String num2=request.getParameter("secondnum"); int v1= Integer.parseInt(num1); int v2= Integer.parseInt(num2); int res= v1/v2; out.print("Output is: "+ res); %> </body> </html> |
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page isErrorPage="true" %> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> Got this Exception: <%= exception %> <b>Please enter correct value</b> </body> </html> |
ConversionConversion EmoticonEmoticon