分享

Tomcat与Java Web开发技术详解连载之二

 bluecrystal 2006-07-19
Tomcat与Java Web开发技术详解连载之二
作者:孙卫琴 发文时间:2005.06.06

2.2.4 部署HTML文件

在helloapp目录下加入index.htm文件,这个文件仅仅用来显示一串带链接的字符"Welcome to HelloApp", 它链接到login.jsp文件。以下是index.htm文件的代码:

<html>
                        <head>
                        <title>helloapp</title>
                        </head>
                        <body >
                        <p><font size="7">Welcome to HelloApp</font></p>
                        <p><a href="login.jsp
                        language=English">English version </a>
                        </body>
                        </html>


访问index.htm的URL为 http://localhost:8080/helloapp/index.htm,该页面的显示结果如图2-3所示。



图2-3 index.htm


2.2.5 部署JSP

接下来,创建两个JSP文件,其中一个是login.jsp(参见例程2-1),它显示登录页面,要求输入用户名和口令,这个页面链接到一个名为DispatcherServlet的Servlet。 还有一个JSP文件是hello.jsp(参见例程2-2),这个JSP被DispatcherServlet调用,显示Hello页面。JSP的语法将在第4章详细讨论,本节侧重于介绍JSP的发布过程。这两个JSP文件都应放在helloapp目录下。

例程2-1 login.jsp

<html>
                        <head>
                        <title>helloapp</title>
                        </head>
                        <body >
                        <br>
                        <form name="loginForm" method="post"
                        action="dispatcher">
                        <table>
                        <tr>
                        <td><div align="right">User Name:</div></td>
                        <td> <input type="text" name="username"></td>
                        </tr>
                        <tr>
                        <td><div align="right">Password:</div></td>
                        <td><input type="password" name="password"></td>
                        </tr>
                        <tr>
                        <td></td>
                        <td><input type="Submit"
                        name="Submit" value="Submit"></td>
                        </tr>
                        </table>
                        </form>
                        </body>
                        </html>


例程2-2 hello.jsp

<html>
                        <head>
                        <title>helloapp</title>
                        </head>
                        <body>
                        <b>Welcome: <%= request.getAttribute("USER") %></b>
                        </body>
                        </html>


login.jsp中生成了一个loginForm表单,它有两个字段:username和passoword。访问login.jsp的URL为http://localhost:8080/helloapp/login.jsp,它生成的页面如图2-4所示。



图2-4 login.jsp网页


2.2.6 部署Servlet

下面,创建一个Servlet文件,名为DispatcherServlet.java(参见例程2-3),它调用HttpServletRequest对象的getParameter方法读取客户提交的loginForm表单数据,获取用户名和口令,然后将用户名和口令保存在HttpServletRequest对象的属性中,再把请求转发给hello.jsp。

例程2-3 DispatcherServlet.java

package mypack;
                        import javax.servlet.*;
                        import javax.servlet.http.*;
                        import java.io.*;
                        import java.util.*;
                        public class DispatcherServlet
                        extends HttpServlet
                        {
                        private String target = "/hello.jsp";
                        public void init(ServletConfig config)
                        throws ServletException
                        {
                        super.init(config);
                        }
                        public void doGet
                        (HttpServletRequest request,
                        HttpServletResponse response)
                        throws ServletException, IOException
                        {
                        // If it is a get request
                        forward to doPost()
                        doPost(request, response);
                        }
                        public void doPost
                        (HttpServletRequest request,
                        HttpServletResponse response)
                        throws ServletException, IOException
                        {
                        // Get the username from the request
                        String username =
                        request.getParameter("username");
                        // Get the password from the request
                        String password =
                        request.getParameter("password");
                        // Add the  user to the request
                        request.setAttribute
                        ("USER", username);
                        request.setAttribute
                        ("PASSWORD", password);
                        // Forward the request
                        to the target named
                        ServletContext context
                        = getServletContext();
                        System.out.println
                        ("Redirecting to " + target);
                        RequestDispatcher dispatcher =
                        context.getRequestDispatcher(target);
                        dispatcher.forward(request, response);
                        }
                        public void destroy()
                        {
                        }
                        }


编译并发布DispatcherServlet的步骤如下。

(1)编译DispatcherServlet.java。编译时,需要将Java Servlet API的JAR文件(servlet-api.jar)设置为classpath,servlet-api.jar文件位于/common/lib目录下。

(2)把编译出来的class文件拷贝到/helloapp/WEB_INF/classes目录下。DispatcherServlet.class的存放位置为/helloapp/WEB_INF/classes/mypack/DispatcherServlet。

在本例中,声明将DispatcherServlet类放在包mypack下,所以应该在/WEB_INF/classes目录下先创建子目录/mypack,然后在子目录下放DispatcherServlet.class文件。

(3)接下来在web.xml中为DispatcherServlet类加上元素。

<web-app>
                        <servlet>
                        <servlet-name>dispatcher</servlet-name>
                        <servlet-class>mypack.DispatcherServlet</servlet-class>
                        </servlet>
                        <servlet-mapping>
                        <servlet-name>dispatcher</servlet-name>
                        <url-pattern>/dispatcher</url-pattern>
                        </servlet-mapping>
                        </web-app>


元素的属性描述参见表2-4。

在本例配置中,没有为DispatcherServlet设置load-on-startup属性,因此当Web应用启动时,Servlet容器不会加载这个Servlet,只有当Web客户首次访问这个Servlet时才加载它。

表2-4 元素的属性

属 性 说 明
servlet-name 定义Servlet的名字
servlet-class 指定实现这个Servlet的类
init-param 定义Servlet的初始化参数 (包括参数名和参数值),一个元素中可以有多个
load-on-startup 指定当Web应用启动时, 装载Servlet的次序。 当这个值为正数或零, Servlet容器先加载数值小的Servlet, 再依次加载其他数值大的Servlet。 如果这个值为负数或者没有设定, 那么Servlet容器将在Web客户首次 访问这个Servlet时加载它


元素用来指定映射。是指访问Servlet的相对URL路径。

根据以上属性,访问DispatcherServlet的URL为http://localhost:8080/ helloapp/dispatcher,DispatcherServlet接受到客户请求后,再把请求转发给hello.jsp,hello.jsp生成的页面如图2-5所示。



图2-5 DispatcherServlet调用hello.jsp生成的网页


2.2.7 部署JSP Tag Library

最后,在Web应用中加入Tag Library(标签库)。Tag Library向用户提供了自定义JSP标签的功能。我们将定义一个名为mytaglib的标签库,它包含了一个简单的hello标签,这个标签能够将JSP页面中所有的解析为字符串"hello"。以下是创建和发布mytaglib标签库的步骤。

(1)编写用于处理hello标签的类HelloTag.java,例程2-4列出了HelloTag.java的源代码。

例程2-4 HelloTag.java

package mypack;
                        import javax.servlet.jsp.JspException;
                        import javax.servlet.jsp.JspTagException;
                        import javax.servlet.jsp.tagext.TagSupport;
                        public class HelloTag extends TagSupport
                        {
                        public void HelloTag()
                        {
                        }
                        // Method called when the
                        closing hello tag is encountered
                        public int doEndTag() throws JspException
                        {
                        try {
                        // We use the pageContext to get a Writer
                        // We then print the text string Hello
                        pageContext.getOut().print("Hello");
                        }
                        catch (Exception e)
                        {
                        throw new JspTagException(e.getMessage());
                        }
                        // We want to return SKIP_BODY because
                        this Tag does not support
                        // a Tag Body
                        return SKIP_BODY;
                        }
                        public void release()
                        {
                        // Call the parent‘s release
                        to release any resources
                        // used by the parent tag.
                        // This is just good practice
                        for when you start creating
                        // hierarchies of tags.
                        super.release();
                        }
                        }


编译HelloTag.java时,需要将jsp-api.jar文件添加到classpath中,这个JAR文件位于/common/lib目录下。编译生成的HelloTag.class存放位置为/WEB-INF/classes/mypack/HelloTag.class。

(2)创建Tag Library的描述文件mytaglib.tld文件,在这个文件中定义mytaglib标签库和hello标签。这个文件存放位置为/WEB-INF/mytaglib.tld。例程2-5列出了mytaglib.tld的源代码。

例程2-5 mytaglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
                        <!DOCTYPE taglib
                        PUBLIC "-//Sun Microsystems,
                        Inc.//DTD JSP Tag Library 1.1
                        //EN"
                        "http://java./j2ee/dtds
                        /web-jsptaglibrary_1_1.dtd">
                        <!-- a tag library descriptor -->
                        <taglib>
                        <tlibversion>1.0</tlibversion>
                        <jspversion>1.1</jspversion>
                        <shortname>mytaglib</shortname>
                        <uri>/mytaglib</uri>
                        <tag>
                        <name>hello</name>
                        <tagclass>mypack.HelloTag</tagclass>
                        <bodycontent>empty</bodycontent>
                        <info>Just Says Hello</info>
                        </tag>
                        </taglib>


(3)在web.xml文件中加入元素,例程2-6列出了修改后的web.xml文件。

例程2-6 加入元素的web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
                        <!DOCTYPE web-app PUBLIC
                        ‘-//Sun Microsystems, Inc.
                        //DTD Web Application 2.3//EN‘
                        ‘http://java./j2ee
                        /dtds/web-app_2_3.dtd‘>
                        <web-app>
                        <servlet>
                        <servlet-name>dispatcher</servlet-name>
                        <servlet-class>mypack.DispatcherServlet
                        </servlet-class>
                        </servlet>
                        <servlet-mapping>
                        <servlet-name>dispatcher</servlet-name>
                        <url-pattern>/dispatcher</url-pattern>
                        </servlet-mapping>
                        <taglib>
                        <taglib-uri>/mytaglib</taglib-uri>
                        <taglib-location>/WEB-INF/mytaglib.tld
                        </taglib-location>
                        </taglib>
                        </web-app>


中包含两个属性。其中指定Tag Library标示符;指定Tag Library的描述文件(TLD)的位置。

(4)在hello.jsp文件中加入hello标签。首先,在hello.jsp中加入引用mytaglib的taglib指令:

<%@ taglib uri="/mytaglib" prefix="mm" %>


以上taglib指令中,prefix用来指定引用mytaglib标签库时的前缀,修改后的hello.jsp文件参见例程2-7。

例程2-7 加入Tag标签的hello.jsp

<%@ taglib uri="/mytaglib" prefix="mm" %>
                        <html>
                        <head>
                        <title>helloapp</title>
                        </head>
                        <b><mm:hello/> :
                        <%= request.getAttribute("USER") %></b>
                        </body>
                        </html>


hello.jsp修改后,再依次访问index.htm→login.jsp→DispatcherServlet→hello.jsp,最后生成的网页如图2-6所示。



图2-6 带hello标签的hello.jsp生成的网页

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多