OUT Implicit Object in JSP
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
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.
counter.jsp
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
<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.