You are here:Home » java » Implicit objects in JSPs

Implicit objects in JSPs

Implicit objects in JSPs

In this part of the JEE tutorials, we will talk about implicit objects in JSP pages. Implicit objects are important objects, that are available within scriptlets. Here we will mention some of them.

out object

Implicit object out is used to write to the output stream. The out object has page scope. A specific out object is available for each JSP page.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.List" %>
<%@page import="java.util.ArrayList" %>


<html>
<head>
<title>Implicit Out Object</title>
<style>
* { font-size: 12px; font-family: Verdana }
td { border: 1px solid #ccc; padding: 3px }
th { border: 1px solid #4B8699; padding: 3px;
background: #4B8699; color: white }
</style>
</head>
<body>
<h2>Out Object</h2>

<%!
List authors = new ArrayList();
%>

<%
if (authors.isEmpty()) {
authors.add("Leo Tolstoy");
authors.add("Fiodor Dostoevsky");
authors.add("Emile Zola");
authors.add("Honore de Balzac");
}

out.println("<table>");
out.println("<th>");
out.println("Authors");
out.println("</th>");

for (int i = 0; i < authors.size(); i++) {
out.println("<tr>");
out.println("<td>");
out.println(authors.get(i));
out.println("</td>");
out.println("</tr>");
}
out.println("</table>");

%>

</body>
</html>
Our code example creates a html table. It uses the implicit out parameter.
 out.println("<th>");
out.println("Authors");
out.println("</th>");
This code prints a header for the table.
There is another way, how we can print the table. By mixing html and java code.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.List" %>
<%@page import="java.util.ArrayList" %>


<html>
<head>
<title>Implicit Out Object</title>
<style>
* { font-size: 12px; font-family: Verdana }
td { border: 1px solid #ccc; padding: 3px }
th { border: 1px solid #4B8699; padding: 3px;
background: #4B8699; color: white }
</style>
</head>
<body>
<h2>Out Object</h2>

<%!
List authors = new ArrayList();
%>

<%
if (authors.isEmpty()) {
authors.add("Leo Tolstoy");
authors.add("Fiodor Dostoevsky");
authors.add("Emile Zola");
authors.add("Honore de Balzac");
}
%>

<table>
<th>Authors</th>

<% for (int i = 0; i < authors.size(); i++) { %>
<tr>
<td>
<%= authors.get(i) %>
</td>
</tr>
<% } %>

</table>

</body>
</html>
The outcome is the same for both examples.
Implicit out object
Figure: Implicit out object

response object

Object response is used as a reply to the request from the client. This object is seldom used by JSP authors. The response object has page scope. There is one situation, where the response object might be useful. Say we want to rename the url of one page. But we realize, that many people might have bookmarked the page. To overcame this issue, we might use the response object to redirect the request to a new page.
index.jsp
<%
response.sendRedirect("target.jsp");
%>
target.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>
<head>
<title>Target JSP Page</title>
</head>
<body>
<h2>Welcome</h2>
</body>
</html>
When we run the example, we end up with the target.jsp page.

request object

We will demonstrate a request object in a simple form. The form is use to send user data to the server for processing.
index.jsp
<html>
<head>
<title>Form</title>
<style>
* { font-size: 12px; font-family: Verdana }
input { border: 1px solid #ccc }
</style>
</head>
<body>

<form method="get" action="book.jsp">
<p>
Enter a book name
</p>
<input type="text" name="bookname">
<input type="submit" value="submit">
</form>

</body>
</html>
book.jsp
<html>
<head>
<title>Book</title>
<style>
* { font-size: 12px; font-family: verdana }
</style>
</head>
<body>

<p>
You have entered: <%= request.getParameter("bookname") %>
</p>


</body>
</html>
In this example, we have two jsp files. One file is used to enter data. We use a form. The data is sent to the second jsp file, which will simply print the data, we have entered.
<style>
* { font-size: 12px; font-family: Verdana }
input { border: 1px solid #ccc }
</style>
We use some css to make our page prettier. We change the font size and family and set a new border for an input html tag.
<form method="get" action="book.jsp">
The action parameter specifies the name of the jsp file, which will receive the data we send.
You have entered: <%= request.getParameter("bookname") %>
This line will output the data we entered into the form. The request object encapsulates the data sent by the client.
Next we use an alternative method to deploy the war file.
$ ls
books.jsp index.jsp
We have two jsp files.
$ jar -cvf form.war *
added manifest
adding: books.jsp(in = 197) (out= 151)(deflated 23%)
adding: index.jsp(in = 308) (out= 204)(deflated 33%)
We create a web archive.
$ ls
books.jsp form.war index.jsp
The web archive has been created.
$ asadmin deploy --user=admin ~/programming/jee/form/form.war
We can use the asadmin tool to deploy the web archive. We provide the user name and the path to the war file. By default, the admin user is called admin and the password is adminadmin.
Command deploy executed successfully.
If everything goes ok, we see this message.

application object

The application object is available to all clients. It shares data on all application pages. The application object can be used to share data, log messages, track user hits or access server information.
book.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>
<head>
<title>application object</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<body>
<h2>application object</h2>

<p>
Resource paths:
</p>
<%= application.getResourcePaths("/") %> <br>
<%= application.getResourcePaths("/WEB-INF") %><br>
<%= application.getResourcePaths("/META-INF") %>

<p>
Server:
</p>
<%= application.getServerInfo() %>

<p>
Context path:
</p>
<%= application.getContextPath() %>

</body>
</html>
In our example, we print resource paths. These are all paths to resources within the web application. Further we print server information and the context path. Our server is Resin 3.1.3. The context path uniquely identifies the web application in server.
application object
Figure: application object
In this chapter, we mentioned JSP implicit objects.

0 comments:

Post a Comment