0% found this document useful (0 votes)
45 views22 pages

Implicit Objects in JSP

Uploaded by

imjyoti1511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views22 pages

Implicit Objects in JSP

Uploaded by

imjyoti1511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Implicit Objects in JSP

• These objects are created by the web container that are available
to all the jsp pages.

• These objects are also called as Pre-Defined Variables in JSP.

• JSP implicit objects are created during the translation phase of JSP
to the servlet.

• There are 9 types of implicit objects available in the JSP :

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.

• getAttribute(String attributeName): It returns the object


stored in a given attribute name.
Ex:String s = (String)application.getAttribute("MyAttr");

• setAttribute(String attributeName, Value): It sets the value


of an attribute or in other words it stores an attribute and its
value in application context, which is available to use across
JSP application.
Ex:application.setAttribute(“MyAttr”, “value”);
applnobj.jsp
<html>
<%
Integer counter= (Integer)application.getAttribute("numOfVisits");
if( counter ==null || counter == 0 ){
counter = 1;
}else{
counter = counter+ 1;
}
application.setAttribute("numOfVisits", counter);
%>
<h3>Total number of hits to this Page is: <%= counter%></h3>
</html>
Output:
pageContext Object:
• In JSP, pageContext is an implicit object of type PageContext
class.
• The pageContext object can be used to set, get or remove
attribute from one of the following scopes:
 page – Scope: PAGE_CONTEXT
 request – Scope: REQUEST_CONTEXT
 session – Scope: SESSION_CONTEXT
 application – Scope: APPLICATION_CONTEXT
• In JSP, page scope is the default scope.
Methods of pageContext Implicit Object
• getAttribute (String AttributeName, int Scope)
• setAttribute(String AttributeName, AttributeValue, int Scope)
pagecontextobj.html
<html>
<head><title> Login Page</title></head>
<body>
<form action="pagecontextobj.jsp">
Enter User-Id: <input type="text" name="uid"><br><br>
Enter Password: <input type="password" name="upass"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
pagecontextobj.jsp
<html>
<body>
<% String id=request.getParameter("uid");
String pass=request.getParameter("upass");
out.println("hello "+id);
pageContext.setAttribute("uname", id, PageContext.SESSION_SCOPE);
pageContext.setAttribute("pwd", pass, PageContext.SESSION_SCOPE);
%>
<a href="display.jsp">Click here to see Password </a>
</body>
</html>
display.jsp
<html>
<body>
<%
String uname= (String) pageContext.getAttribute("uname",
PageContext.SESSION_SCOPE);
String pwd= (String) pageContext.getAttribute("pwd",
PageContext.SESSION_SCOPE);
out.println("Hi "+uname);
out.println("Your Password is: "+pwd);
%>
</body>
</html>
page Object:
• Page implicit variable holds the currently executed servlet
object for the corresponding jsp.
• Acts as this object for current jsp page.
• This object is very rarely used.
Example:
<html>
<body>
<% String pageName = page.toString();
out.println("Page Name is " +pageName);%>
</body>
</html>
config Object:
• In JSP, config is an implicit object of type ServletConfig. This
object can be used to get initialization parameter for a
particular JSP page.
• The config object is created by the web container for each
jsp page.
• Generally, it is used to get initialization parameter from the
web.xml file.
Example: “configobj.jsp”
<html>
<body>
<%
String sname=config.getServletName();
out.print("Servlet Name is: "+sname);
%>
</body>
</html>
web.xml
<web-app>
<servlet>
<servlet-name>SampleServlet</servlet-name>
<jsp-file>/configobj.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/config</url-pattern>
</servlet-mapping>
</web-app>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy