0% found this document useful (0 votes)
70 views14 pages

OUT Implicit Object in JSP

The document discusses various implicit objects in JSP including out, request, response, session, application, exception, and pageContext. It provides code examples for each: 1) The out implicit object is used to print output to the response. request is used to retrieve submitted form parameters. response handles redirects. 2) session tracks user sessions across requests. application counts total page visits. exception handles errors. 3) pageContext stores attributes in different scopes like session, and fetches them later to share data across JSP pages.

Uploaded by

Ziesha Arora
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)
70 views14 pages

OUT Implicit Object in JSP

The document discusses various implicit objects in JSP including out, request, response, session, application, exception, and pageContext. It provides code examples for each: 1) The out implicit object is used to print output to the response. request is used to retrieve submitted form parameters. response handles redirects. 2) session tracks user sessions across requests. application counts total page visits. exception handles errors. 3) pageContext stores attributes in different scopes like session, and fetches them later to share data across JSP pages.

Uploaded by

Ziesha Arora
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/ 14

OUT Implicit Object in JSP

index.jsp

<HTML>
<HEAD>
<TITLE> OUT IMPLICIT OBJECT EXAMPLE </TITLE>
</HEAD>
<BODY>
<%
out.print( "print statement " );
out.println( "println" );
out.print("Another print statement");
%>
</BODY>
</HTML>

Output:
print statement println
Another print statement
Request Implicit Object in JSP
index.html

<html>
<head>
<title>Enter UserName and Password</title>
</head>
<body>
<form action="userinfo.jsp">
Enter User Name: <input type="text" name="uname" /> <br><br>
Enter Password: <input type="text" name="pass" /> <br><br>
<input type="submit" value="Submit Details"/>
</form>
</body>
</html>
userinfo.jsp

<%@ page import = " java.util.* " %>


<html>
<body>
<%
String username=request.getParameter("uname");
String password=request.getParameter("pass");
out.print("Name: "+username+" Password: "+password);
%>
</body>
</html>
Snapshots of above example

Once you run the above JSP code. It would show you the below screen with two
text fields of username and password.
This is the output of userinfo.jsp page. Here we have fetched the id and
password which has been entered by the user in login page.
Response Implicit Object in JSP
index.html

<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="checkdetails.jsp">
UserId: <input type="text" name="id" /> <br><br>
Password: <input type="text" name="pass" /> <br><br>
<input type="submit" value="Sign In!!"/>
</form>
</body>
</html>
This JSP page verifies the input id/pass against hard-coded values.

checkdetails.jsp

<html>
<head><title>Check Credentials</title>
</head>
<body>
<%
String uid=request.getParameter("id");
String password=request.getParameter("pass");
session.setAttribute("session-uid", uid);
if(uid.equals("Chaitanya") && password.equals("BeginnersBook"))
{
response.sendRedirect("success.jsp");
}
else
{
response.sendRedirect("failed.jsp");
}
%>
</body>
</html>
This JSP page would execute if id/pass are matched to the hardcoded
userid/password.
success.jsp

<html>
<head><title>Success Page</title>
</head>
<body>
<%
String data=(String)session.getAttribute("session-uid");
out.println("Welcome "+ data+"!!");
%>
</body>
</html>
The control will be redirected to this page if the credentials entered by user are
wrong.

failed.jsp

<html>
<head><title>Sign-in Failed Page</title>
</head>
<body>
<%
String data2=(String)session.getAttribute("session-uid");
out.println("Hi "+ data2+". Id/Password are wrong. Please try Again.");
%>
</body>
</html>
Output Screenshots:
Login page
Success Page: When userId and Password are correct.

Login with incorrect details.


Sign-In fail Page: When Id and Password are wrong.
Application Implicit Object in JSP
A JSP page to capture number of hits using application. In this example we are
counting the number of hits to a JSP page using application implicit object.

counter.jsp

<%@ page import="java.io.*,java.util.*" %>


<html>
<head>
<title>Application Implicit Object Example</title>
</head>
<body>
<%
//Comment: This would return null for the first time
Integer counter= (Integer)application.getAttribute("numberOfVisits");
if( counter ==null || counter == 0 ){
//Comment: For the very first Visitor
counter = 1;
}else{
//Comment: For Others
counter = counter+ 1;
}
application.setAttribute("numberOfVisits", counter);
%>
<h3>Total number of hits to this Page is: <%= counter%></h3>
</body>
</html>
Screenshots of output

Number of hits is 1 for the first time visitor.


Number of hits got increased when I refreshed the page.
Exception Implicit Object in JSP
In this example we are taking two integer inputs from user and then we are
performing division between them. We have used exception implicit object to
handle any kind of exception in the below example.

index.html

<html>
<head>
<title>Enter two Integers for Division</title>
</head>
<body>
<form action="division.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>
Here we have specified exception.jsp as errorPage which means if any exception
occurs in this JSP page, the control will immediately transferred to the
exception.jsp JSP page. Note: We have used errorPage attribute of Page
Directive to specify the exception handling JSP page (<%@ page
errorPage=”exception.jsp” %>).

division.jsp

<%@ page errorPage="exception.jsp" %>


<%
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);
%>
In the below JSP page we have set isErrorPage to true which is also an
attribute of Page directive, used for making a page eligible for exception
handling. Since this page is defined as a exception page in division.jsp, in case
of any exception condition this page will be invoked. Here we are displaying the
error message to the user using exception implicit object.
exception.jsp

<%@ page isErrorPage="true" %>


Got this Exception: <%= exception %>
Please correct the input data.
Output

Screen with two input fields for two integer numbers.

Arithmetic Exception message when we have provided the second integer as


zero.
pageContext Implicit Object in JSP
index.html

Here we are simply asking user to enter login details.

<html>
<head>
<title> User Login Page – Enter details</title>
</head>
<body>
<form action="validation.jsp">
Enter User-Id: <input type="text" name="uid"><br>
Enter Password: <input type="text" name="upass"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
validation.jsp

In this page we are storing user’s credentials using pageContext implicit object
with the session scope, which means we will be able to access the details till
the user’s session is active. We can also store the attribute using other scope
parameters such as page, application and request.

<html>
<head> <title> Validation JSP Page</title>
</head>
<body>
<%
String id=request.getParameter("uid");
String pass=request.getParameter("upass");
out.println("hello "+id);
pageContext.setAttribute("UName", id, PageContext.SESSION_SCOPE);
pageContext.setAttribute("UPassword", pass, PageContext.SESSION_SCOPE);
%>
<a href="display.jsp">Click here to see what you have entered </a>
</body>
</html>
display.jsp

In this JSP page we are fetching the stored attributes using getAttribute method.
The point to note here is that we have stored the attributes with session scope so
we must need to specify scope as session in order to fetch those attribute’s
value.

<html>
<head>
<title>Displaying User Details</title>
</head>
<body>
<%
String username= (String) pageContext.getAttribute("UName",
PageContext.SESSION_SCOPE);
String userpassword= (String) pageContext.getAttribute("UPassword",
PageContext.SESSION_SCOPE);
out.println("Hi "+username);
out.println("Your Password is: "+userpassword);
%>
</body>
</html>
Screenshots of the example’s output:

Login page where we are receiving User-Id and Password from user.
A page with details page link –

User Credentials display page which we have passed from login page to this
page through pageContext instance.

You might also like