Java EE Implicit Objects in JSPs

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(Новая: 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 so...)
(нарушало авторские права)
 
Строка 1: Строка 1:
-
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.
 
-
 
-
<source lang="java">
 
-
<%@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>
 
-
</source>
 
-
 
-
Our code example creates a html table. It uses the implicit out parameter.
 
-
 
-
<source lang="java">
 
-
out.println("<th>");
 
-
out.println("Authors");
 
-
out.println("</th>");
 
-
 
-
</source>
 
-
 
-
This code prints a header for the table.
 
-
 
-
There is another way, how we can print the table. By mixing html and java code.
 
-
 
-
<source lang="java">
 
-
<%@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>
 
-
</source>
 
-
 
-
The outcome is the same for both examples.
 
-
 
-
[[image: java_ee_faq_outobject.png| center]]
 
-
 
-
== 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.
 
-
<source lang="java">
 
-
<%
 
-
  response.sendRedirect("target.jsp");
 
-
%>
 
-
</source>
 
-
 
-
<source lang="java">
 
-
 
-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
-
 
-
<html>
 
-
    <head>
 
-
        <title>Target JSP Page</title>
 
-
 
-
    </head>
 
-
    <body>
 
-
        <h2>Welcome</h2>
 
-
    </body>
 
-
</html>
 
-
</source>
 
-
 
-
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.
 
-
<source lang="java">
 
-
<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>
 
-
</source>
 
-
 
-
<source lang="java">
 
-
 
-
<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>
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
<style>
 
-
* { font-size: 12px; font-family: Verdana }
 
-
input { border: 1px solid #ccc }
 
-
</style>
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
<form method="get" action="book.jsp">
 
-
</source>
 
-
 
-
The action parameter specifies the name of the jsp file, which will receive the data we send.
 
-
 
-
<source lang="java">
 
-
You have entered: <%= request.getParameter("bookname") %>
 
-
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
$ ls
 
-
books.jsp  index.jsp
 
-
</source>
 
-
 
-
We have two jsp files.
 
-
 
-
<source lang="java">
 
-
 
-
$ jar -cvf form.war *
 
-
added manifest
 
-
adding: books.jsp(in = 197) (out= 151)(deflated 23%)
 
-
adding: index.jsp(in = 308) (out= 204)(deflated 33%)
 
-
</source>
 
-
 
-
We create a web archive.
 
-
 
-
<source lang="java">
 
-
 
-
$ ls
 
-
books.jsp  form.war  index.jsp
 
-
 
-
</source>
 
-
 
-
The web archive has been created.
 
-
 
-
<source lang="java">
 
-
 
-
$ asadmin deploy --user=admin ~/programming/jee/form/form.war
 
-
</source>
 
-
 
-
We can use the <b>asadmin</b> 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.
 
-
 
-
<source lang="java">
 
-
 
-
Command deploy executed successfully.
 
-
</source>
 
-
 
-
If everything goes ok, we see this message.
 
-
 
-
== application object ==
 
-
The <b>application</b> 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.
 
-
 
-
<source lang="java">
 
-
 
-
<%@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>
 
-
</source>
 
-
 
-
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.
 
-
 
-
[[image: java_ee_faq_application.png| center]]
 
-
 
-
[[Категория:Java]]
 

Текущая версия на 11:36, 7 апреля 2009