JSP技术概览

以下是资料介绍,如需要完整的请充值下载. 本资料已审核过,确保内容和网页里介绍一致.  
无需注册登录,支付后按照提示操作即可获取该资料.
资料介绍:

JSP是一种用于Web应用程序开发的最新的JAVA技术,它是建立在servlet 技术基础之上的。尽管servlet 的功能在很多方面来说都很强大,但它通常属于程序员专有的领域。在本章中,我们将介绍JSP 技术可以解决哪些问题,对JSP 页面的剖析,servlet 和JSP 之间的关系,以及服务器处理JSP 页面的方式等。
在任何Web 应用程序中,都有一个服务器上的程序来处理请求并产生应答。对于简单的单页式应用程序,例如一个在线公告牌,你并不需要过多地考虑代码的设计,而且所有的逻辑都可以堆在一个程序中。但当应用程序变得越来越大时(如跨越了多个页面,使用数据库之类的外部资源,并为更多类型的客户提供了更多的选项和支持),情况就完全不一样了。站点的设计方式直接关系到它适应新需求的能力以及继续改进的能力。令人高兴的是,JSP 技术可以作为所有类型Web 应用程序的一个重要部分,从最简单的到最复杂的应用程序都是如此。因此,本章还将介绍推荐使用的Web应用程序设计模型的基本概念,以及JSP和其他JAVA 技术在这个模型中所扮演的不同角色。
servlet 所带来的问题
在许多基于JAVA servlet 的应用程序中,处理请求和产生应答是由同一个servlet 类来执行的。例3-1 显示了servlet 通常的外观:
例3-1:典型的servlet 类
public class OrderServlet extends HttpServlet {
public void doGet((HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if (isOrderInfoValid(request)) {
saveOrderInfo(request);
out.println("<html>");
out.println(" <head>");
out.println(" <title>Order Confirmation</title>");
out.println(" </head>");
out.println(" <body>");
out.println(" <h1>Order Confirmation</h1>");
renderOrderInfo(request);
out.println(" </body>");
out.println("</html>");
}
...
如果你不是程序员,也不用为这些代码中的细节担心。关键的一点是,servlet 包含了请求的处理以及商务逻辑(通过像isOrderInfoValid()和saveOrderInfo()这样的方法来实现),并且产生了应答的HTML代码,这些代码是通过调用println()函数而直接嵌入在servlet代码中的。一个更加结构化的servlet应用程序可以将处理过程分成各个不同的部分,封装在各种可重用的工具类中,也可能使用一个单独的类库来产生应答中的实际的HTML元素。尽管如此,纯servlet 的方案还是存在以下一些问题:
● 开发和维护应用程序的所有部分需要有深厚的JAVA编程知识,因为处理代码和HTML 元素是交织在一起的。
● 改变应用程序的外观和风格,或者加入对某种新类型客户机(如WML客户机)的支持时,都需要更新并重新编译servlet 代码。
● 很难利用网页开发工具的优势来设计应用程序界面。如果使用这些工具来开发网页布局的话,生成的HTML 代码必须被手工嵌入到servlet 代码中,这个过程既耗时又容易出错,而且极度枯燥乏味。
引入JSP 可以将请求处理和商务逻辑与外观呈现分离开来,如图3-1 所示。此时并不是将HTML嵌入到代码中,而是将所有静态HTML放到JSP 页面中,就像一个通常的网页一样,然后加入一些JSP 元素来产生页面的动态部分。对请求的处理工作可以留给servlet 程序员来做,商务逻辑则可以由JAVABeans 和EJB 组件来处理。


JSP is the latest JAVA technology for web application development and is based on the servlet technology introduced in the previous chapter. While servlets are great in many ways, they are generally reserved for programmers. In this chapter, we look at the problems that JSP technology solves, the anatomy of a JSP page, the relationship between servlets and JSP, and how the server processes a JSP page.

In any web application, a program on the server processes requests and generates responses. In a simple one-page application, such as an online bulletin board, you don't need to be overly concerned about the design of this piece of code; all logic can be lumped together in a single program. However, when the application grows into something bigger (spanning multiple pages, using external resources such as databases, with more options and support for more types of clients), it's a different story. The way your site is designed is critical to how well it can be adapted to new requirements and continue to evolve. The good news is that JSP technology can be used as an important part in all kinds of web applications, from the simplest to the most complex.

Therefore, this chapter also introduces the primary concepts in the design model recommended for web applications and the different roles played by JSP and other JAVA technologies in this model.

The Problem with Servlets

In many JAVA servlet-based applications, processing the request and generating the response are both handled by a single servlet class. Example 3-1 shows how a servlet class often looks.

Example 3-1. A typical servlet class

public class OrderServlet extends HttpServlet {

    public void doGet((HttpServletRequest request,

        HttpServletResponse response)

        throws ServletException, IOException  {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter(  );

        if (isOrderInfoValid(request)) {

            saveOrderInfo(request);