Oracle® Application Server Application Developer's Guide
10g Release 2 (10.1.2) Part No. B14000-01 |
|
![]() Previous |
![]() Next |
You can create the presentation pages, which display data from business logic plus presentation elements, using different methods:
Contents of this chapter:
This option is valid for static pages only. If your pages have dynamic data, you have to generate the pages programmatically.
Servlets enable you to generate pages programmatically. Using servlets, you can call business logic objects to obtain data, then assemble the page by adding in presentation elements. You can then send the completed page to the client.
Servlets can call methods in themselves and methods in other objects. Servlets can retrieve or update data in databases using JDBC or SQLJ.
Disadvantages of using servlets:
Presentation elements are embedded with the business logic. This means that when you want to change the presentation code, you have to be careful not to change the business logic as well. In addition, the person editing the presentation code should have some knowledge of Java and not just HTML.
Because presentation elements are embedded with the business logic, OracleAS has to recompile the servlet when you change the presentation elements in the servlet.
Another issue when using servlets to generate presentation elements is that you have to use the println
method frequently. This makes the code look less tidy.
Servlets are a good choice for implementing state machines or controllers. State machines or controllers receive requests, make decisions based on parameters in the requests, and redirect the requests to the appropriate JSP for assembling the final display page to return to the clients. In the sample application, the controller is a servlet.
One advantage to updating servlets is that OracleAS has an auto-compile feature for servlets. You can place the uncompiled .java files for the servlets in the $J2EE_HOME/default-web-apps/WEB-INF/classes
directory, and OracleAS will compile the files for you. To enable the auto-compile feature, set the development
attribute of the orion-web-app
tag to "true
". This tag is found in $J2EE_HOME/home/config/global-web-application.xml
.
<orion-web-app jsp-cache-directory="./persistence" servlet-webdir="/servlet" development="true" >
For example, the following doGet
method in a servlet sends HTML data to the client:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Set the content type of the response res.setContentType("text/plain"); // Get a print writer stream to write output to the response PrintWriter out = res.getWriter(); // Send HTML to the output stream out.println("<HTML><HEAD>"); out.println("<TITLE>Employee Benefit Application</TITLE></HEAD>"); out.println("<BODY>"); out.println("<p>... more data here ..."); // Close the HTML tags out.println("</BODY></HTML>"); }
Here is an example of a servlet that calls an EJB object. Note how the servlet simply invokes methods on the EJB instance to get data. In this case, the servlet calls getName
and getPrice
methods on the EJB instance and embeds the return values within the presentation code.
import java.util.*; import java.io.IOException; import java.rmi.RemoteException; import javax.servlet.*; import javax.servlet.http.*; import javax.ejb.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; public class ProductServlet extends HttpServlet { ProductHome home; public void init() throws ServletException { try { Context context = new InitialContext(); home = (ProductHome)PortableRemoteObject. narrow(context.lookup("MyProduct"), ProductHome.class); } catch(NamingException e) { throw new ServletException("Error looking up home", e); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); ServletOutputStream out = response.getOutputStream(); try { Collection products = home.findAll(); out.println("<html>"); out.println("<head><title>My products</title></head>"); out.println("<body>"); out.println("<table border=\"2\">"); out.println("<tr><td><b>Name</b></td><td><b>Price</b></td></tr>"); Iterator iterator = products.iterator(); while (iterator.hasNext()) { Product product = (Product)PortableRemoteObject. narrow(iterator.next(), Product.class); out.println("<tr><td>" + product.getName() + "</td><td>" + product.getPrice() + "</td></tr>"); } out.println("</table>"); out.println("</body>"); out.println("</html>"); } catch(RemoteException e) { out.println("Error communicating with EJB-server: " + e.getMessage()); } catch(FinderException e) { out.println("Error finding products: " + e.getMessage()); } finally { out.close(); } // finally } // doGet method }
Like servlets, JSP files enable you to combine HTML tags with Java commands. You do not have the println
statements in JSP files like you do in servlets. Instead, you write your HTML tags as usual, but you add in special tags for JSP commands.
JSPs can do everything that servlets can do. For example, JSPs can invoke other classes and connect to the database to retrieve data or update data in the database.
See Also: Oracle Application Server Containers for J2EE Support for JavaServer Pages Developer's Guidefor details on JSPs. |
In addition, JSPs enable you to define custom tags in tag libraries. Tag libraries enable you to define the behavior of your custom tags. Your JSPs can then access the tag libraries and use the custom tags. This enables you to standardize presentation and behavior across all your JSP files.
Here are few examples of how you can use custom tags and tag libraries. You can use them to:
Send email. Tag libraries can hide the details of JavaMail API.
Access Web services.
Access UltraSearch tags.
Upload or download content from a file or database.
See Also: Oracle Application Server Containers for J2EE JSP Tag Libraries and Utilities Reference for details on tag libraries |
Although you can use as much Java in your JSPs as you like, the file can be difficult to read and debug if it is interleaved with JSP scriptlets and HTML. You will get a cleaner design for your application if you place all the business logic code outside the JSP files. The JSP scriptlets in your files can call out to Enterprise JavaBeans and other Java classes to run business logic. These objects then return the data or status to your JSP file, where you can extract the data and display the data using HTML or XML.
Another benefit of excluding business logic code from your JSPs is that you can have Web page designers who might not be familiar with Java work on the JSP page. They can design the look of the page, using placeholders for the real data. Your developers, who might not want to bother with HTML, can be working on the business logic in other files simultaneously.
If you are supporting different client types (browsers and wireless clients), you can have two versions of JSP files: one that returns HTML and one that returns XML. One important note is that both files make the same calls to the same objects to perform business logic. This is what the sample application does.
In the sample application, all the presentation code, even the pages for error conditions, are written in JSP files, and the JSP files do not contain any business logic code. The application uses one file for browsers (for example, addBenefitToEmployee.jsp
) and a similar file for wireless clients (for example, addBenefitToEmployeeWireless.jsp
). The wireless version of the file contains XML instead of HTML.