Updated April 11, 2023
Introduction to JSP Implicit Objects
JSP provides us various implicit objects. All these implicit objects created by the web container. These objects are by default available to all JSP pages. As we all know Java Server Pages i.e. our JSP and it is basically a server-side language. The main advantage of JSP is that we can place our java code inside the HTML page by using the JSP tag. JSP pages are used to create dynamic pages and web applications.
Explain all JSP Implicit Objects
JSP provides us 9 implicit objects which are as follows let’s have a look:
- Application
- Response
- Request
- Session
- Exception
- Out
- Config
- Page Object
- Page Context
1. Application
This implicit object is the type of ServletContext. Which means it provides a reference to the ServletContext interface. We have various action elements that interact with the ServletContext interface.
2. Response
This implicit object is the type of HttpServletResponse. It is directly linked with the response to the client. We can add so many things with this response object like status codes, cookies, and date stamps, etc.
so basically it is the instance of the HttpServletResponse interface. One advantage is we do not need to create a response object on JSP because it is implicitly available. So it is created by the web container as all the implicit objects are created by the web container only for each request they receive from the client. The response object is basically used to navigate to other resources. Hence it is used to manipulate the response object and we redirect it to other resources we mentioned. we can also specify the error, http status code, etc.
Below is the example to show the use of response object :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo for response object</title>
</head>
<body>
<%
//below line will navigate us to other resource let’s say google for now
response.sendRedirect("google.com");
%>
</body>
</html>
In the above example, it will redirect the link to the google page or any URL we mentioned there as input.
3. Request
This object is also created by the web container, this implicit object is the type of HttpServletRequest and it gets created by each JSP request. It basically gives us the user information which can be used further like username, password, remote address, header information, server name, character encoding, server port, content type, etc. basically it provides us the information about the request or we can say input parameter. The request object is also created by the web container on the JSP page so we do not require to create any implicit request object.
Example
Here is the following example mention below
demo.Html
<form action="demo.jsp">
<input type="text" name="username">
<input type="submit" value="click"><br/>
</form>
demo.jsp
<%
String username=request.getParameter("username");
out.print("welcome "+username);
%>
so in the above example, we are retrieving the user input by using the getParameter() method so whatever user writes into the input filed we can retrieve the input by using the getParameter() method.
4. Session
This implicit object is also created by the web container and can be used to get the session information. we can also use this object to remove, set and get attribute. This object is of type HttpSession.
Below is one simple example of how to use this.
Example
Here is the following example mention below
demo.html
<html>
<body>
<form action="demo.jsp">
<input type="text" name="username">
<input type="submit" value="click"><br/>
</form>
</body>
</html>
demo.jsp
<html>
<body>
<%
String username=request.getParameter("username");
out.print("displaying the username here : "+username);
session.setAttribute("username",username);
<a href="demo2.jsp">another JSP page to get value</a>
%>
</body>
</html>
demo2.jsp
<html>
<body>
<%
String name=(String)session.getAttribute("username");
out.print("getting username here "+name);
%>
</body>
</html>
In the above example, we need to mention the same name while getting and setting the parameter otherwise value would be null.
5. Exception
This implicit object is also created by the web container and this is the type of Throwable class. But this implicit object we can only use in error pages and it is used to print the exception.
Example
Here is the following example mention below
<%@ page isErrorPage="true" %>
<html>
<body>
This will print out the exception that has been occurred:<%= exception %>
</body>
</html>
This example will only print the exception that will occur and can only be used on the error pages.
6. Out
This implicit object is also created by the web container. If we want to write any data to buffer then we have this out the implicit object. This object is a type of JspWriter.
syntax :
PrintWriter out=response.getWriter();
Example
<html>
<body>
<% out.print("Just print today's date :"+java.util.Calendar.getInstance().getTime()); %>
</body>
</html>
In the above example, this will only print the current date this object used to print out the data.
7. Config
This object is of type ServletConfig. This object is also created by the web container. It is basically used for initialization purpose, it gets the parameters from web.xml.
Example
Here is the following example mention below
demo.html
<form action="demo">
<input type="text" name="username">
<input type="submit" value="Click"><br/>
</form>
web.xml
<web-app>
<servlet>
<servlet-name>demoservlet</servlet-name>
<jsp-file>/demo.jsp</jsp-file>
<init-param>
<param-name>drivername</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
demo.jsp
<%
out.print("user name is"+request.getParameter("username"));
String driverName=config.getInitParameter("drivername");
out.print("driverName name is="+driverName);
%>
8. Page Context
This object is of type PageContext. It removes, get and set attribute from below mentioned scope :
- application
- request
- session
- page
Example
Here is the following example mention below
demo.html
<html>
<body>
<form action="demo.jsp">
<input type="text" name="username">
<input type="submit" value="click"><br/>
</form>
</body>
</html>
demo.jsp
<html>
<body>
<%
String username=request.getParameter("username");
out.print("display user name "+username);
pageContext.setAttribute("username",username,PageContext.SESSION_SCOPE);
<a href="demo2.jsp">This is second jsp page.</a>
%>
</body>
</html>
demo2.jsp
<html>
<body>
<%
String username=(String)pageContext.getAttribute("username",PageContext.SESSION_SCOPE);
out.print("Getting value from the previous page "+username);
%>
</body>
</html>
In the above example, we are setting value into one JSP and retrieving it on another JSP. By using the corresponding methods.
9. Page Object
This page is the type of Object class. Also created by the web container.
Conclusion
JSP give us implicit object which means we do not need to create their object explicitly. This object creation task is done by the web container and the objects are available on the JSP page. So we can simply set, get and remove attribute values according to our needs and also navigate them to the destination with all required parameters mentioned.
Recommended Articles
This is a guide to JSP Implicit Objects. Here we discuss that JSP provides us 9 implicit objects along with their examples. You may also have a look at the following articles to learn more –