Implicit Objects in JSP
Implicit Objects in JSP
• These objects are created by the web container that are available
to all the jsp pages.
• JSP implicit objects are created during the translation phase of JSP
to the servlet.
1.out 6.application
2.request 7.pageContext
3.response 8.page
4.session 9.config
5.exception
Out Object:
• Out is one of the implicit objects to write the data to the
buffer and send output to the client(browser) in response.
• It is the object of JspWriter Class.
Example: “outobj.jsp”
<html>
<body>
<% out.print("This is Example <br/> ");
out.print(" Demo for OUT Object");
%>
</body>
</html>
Request Object:
• The request is an implicit object of type HttpServletRequest. It
was created for each jsp request by the web container.
• It can be used to get request information from web page or
document.
• By using this request object ,we can able read values from input
elements in JSP.
Example: “requestobj.html”
<html>
<head>
<title>Request Object</title>
</head>
<body>
<form action="requestobj.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
requestobj.jsp
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
Output:
Response Object:
• In JSP, response is an implicit object of type HttpServletResponse.
It is created by the web container for each jsp request.
• It can be used to add or manipulate response such as redirect
response to another resource, send error etc.
Example:
<html>
<head>
<title>Response Object</title>
</head>
<body>
<form action="responseobj.jsp">
URL:<input type="text" name="url">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
responseobj.jsp
<%
String url=request.getParameter("url");
response.sendRedirect("http://"+url);
%>
Output:
Session Object:
• In JSP, session is an implicit object of type HttpSession.
We can use this object to set, get or remove attribute or
to get session information.
Example:
Session.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go">
</form>
</body>
</html>
session.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
session.setAttribute("user",name);
%>
<br/>
<a href="second.jsp">Next Page</a>
</body>
</html>
second.jsp
<html>
<body>
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
</body>
</html>
Exception Object:
• The exception is normally an object that is thrown at
runtime. Exception Handling is the process to handle the
runtime errors. There may occur exception any time in your
web application.
• 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.
Example:
exception.html
<html>
<body>
<form action="process.jsp">
No1:<input type="text" name="n1" />
No2:<input type="text" name="n2" />
<input type="submit" value="divide"/>
</form>
</body>
</html>
process.jsp
<%@ page errorPage="error.jsp" %>
<%
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
out.print("division of numbers is: "+c);
%>
error.jsp
<html>
<body>
<%@ page isErrorPage="true" %>
<h3>Sorry an exception occured!</h3>
Exception is: <%= exception %>
</body>
</html>
Application Object:
• This is basically used for getting initialization parameters and
for sharing the attributes & their values across the entire JSP
application.
• which means any attribute set by application implicit
object would be available to all the JSP pages.