首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Servlet API 2.2 的新特征(2)

Servlet API 2.2 的新特征(2)

    • 按照此流程,我们给出一个简单的例子,假使WAS_ROOT的名字为BuildWar,在 BuildWar                                下的内容为
      1
      2
      3
      4
      5
      6
      7
      index.html
      Tournament/Player.html
      Tournament/Player.jsp
      Tournament/PlayerDetails.html
      Tournament/PlayerDetails.jsp
      WEB-INF/classes/com/myCompany/MyServlet.class
      WEB-INF/web.xml




      相应的发布描述文件为web.xml,其内容为:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      ITSO
      <servlet>
      <servlet-name>MyServlet</servlet-name>
      <servlet-class>com.myCompany.MyServlet</servlet-class>
      </servlet>
      <servlet-mapping>
      <servlet-name>MyServlet</servlet-name>
      /runMyServlet
      </servlet-mapping>




      使用JDK提供的JAR工具来创建WAR文件的例子:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      C:\>cd BuildWar
      C:\BuildWar>jar -cvf c:/TargetWar/myapp.war .
      added manifest
      adding: Tournament/(in = 0) (out= 0)(stored 0%)
      adding: Tournament/PlayerDetails.jsp(in = 1230) (out= 540)(deflated 56%)
      adding: Tournament/Player.jsp(in = 211) (out= 153)(deflated 27%)
      adding: Tournament/PlayerDetails.html(in = 994) (out= 439)(deflated 55%)
      adding: Tournament/Player.html(in = 612) (out= 351)(deflated 42%)
      adding: WEB-INF/(in = 0) (out= 0)(stored 0%)
      adding: WEB-INF/web.xml(in = 1092) (out= 306)(deflated 71%)
      adding: WEB-INF/classes/(in = 0) (out= 0)(stored 0%)
      adding: WEB-INF/classes/com/(in = 0) (out= 0)(stored 0%)
      adding: WEB-INF/classes/com/myCompany/(in = 0) (out= 0)(stored 0%)
      adding: WEB-INF/classes/com/myCompany/MyServlet.class(in = 5822) (out= 2638)(de
      flated 54%)
      adding: asimple.jsp(in = 107) (out= 69)(deflated 35%)
      adding: index.html(in = 237) (out= 168)(deflated 29%)
      C:\>dir /s C:\TargetWar
      Volume in drive C is WINDOWS2000
      Volume Serial Number is 0A72-0FE1
      Directory of C:\TargetWar
      10/13/2000 02:31p  .
      10/13/2000 02:31p  ..
      10/13/2000 02:46p 6,916 myapp.war
      1 File(s) 6,916 bytes
      Total Files Listed:
      1 File(s) 6,916 bytes




    • 如何从一个WAR来发布用户的应用程序 在形成我们的WAR文件后,我们可以利用它来发布我们自己的 Web                                应用程序,这个过程和具体实现Servlet 2.2                                API的Servlet运行环境相关,每一个Servlet运行环境都可能提供了相应的工具来帮助用户来完成这项工作,我们可以参照具体的产品的文挡来获得关于这类工具的说明。我们以IBM                                Web Sphere 3.5.2为例来解释这个过程。IBM Web Sphere                                3.5.2提供了一个基于命令行的工具:wartoxmlconfig可以完成从一个WAR来发布用户的应用程序的工作。它命令行的具体参数为:
      参数意义WAR文件所在的目录c:\TargetWar\template.war发布目录c:\DeployedWar\as3\se3管理服务器的节点MyhostServlet引擎的节点Myhost应用程序服务器的名字as3Servlet引擎Se3虚拟主机V2Web 应用程序的 Web                                            路径/webapp/newTemplateWeb 应用程序的名字NewTemplate
      对应上表所示的情况,使用wartoxmlconfig的具体命令是:
      wartoxmlconfig                                        c:\TargetWar\template.war c:\DeployedWar\as3\se3                                        myHost myhost as3 se3 v2 /webapp/newTemplate                                        newTemplate
    • Servlet API V2.2增强了对错误的支持Servlet API                                V2.2增强了对错误的支持,用户可以为一个 Web 应用程序指定:
      • 一个缺省的出错页面
      • 为某个特定的条件发生时指定的出错页面。例如当某个Exception或状态发生时显示的出错页面。                                    假使一个我们有一个名字为DefApp的 Web 应用程序,在 Web                                    应用程序下有一个名为TestServlet的Servlet程序。DefApp 的出错页面配置为:                                        404/error/err404.jspjava.io.IOException/error/errIOException.jspJavax.servlet.ServletException/error/errServletException.jspOther/ErrorReporter
        TestServlet的源码为:
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        import java.io.*;
        import javax.servlet.*;
        import javax.servlet.http.*;
        public class TestServlet extends HttpServlet {
        public void service(ServletRequest req, ServletResponse res) throws
        ServletException,IOException
        {
        String eType = req.getParameter("e");
        if (eType == null) {
        PrintWriter out=res.getWriter();
        res.setContentType("text/html");
        out.println("<html><head></head><body><h2>This the TestServlet
        for Error Pages.<br> Please specify the Exception
        Type.<br></h2></body></html>");
        out.close();return;
        }
        if (eType.equals("ioe")) {
        System.out.println("Exception Type == IOException");
        IOException ioe = new IOException();
        throw ioe;
        } else if (eType.equals("se")) {
        System.out.println("Exception Type == ServletException");
        ServletException se = new ServletException();
        throw se;
        } else {
        System.out.println("Other Exception");
        EOFException e = new EOFException();
        throw e;
        }
        }




        当我们按照特定的格式来调用处于DefApp下的资源,不同的格式会触发不同的出错条件,相应的的页面会显示出来。                                       
        • http:///DefApp/x.jsp去调用一个不存在的资源会触发"404"的错误,"/error/err404.jsp"会显示出来。
        • http:///DefApp/servlet/TestServlet?e=ioe                                            会触发"java.io.IOException"的错误,"/error/errIOException.jsp"                                            会显示出来。
        • http:///DefApp/servlet/TestServlet?e=se                                            会触发javax.servlet.ServletException,"/error/errServletException.jsp"会显示出来。
        • http:///DefApp/servlet/TestServlet?e=e                                            会触发其它没有指定的错误,系统会调用"/ErrorReporter",缺省页面会显示出来。
  • Servlet API V2.2 关于 Web 应用程序的“Welcome”页面的定义 Servlet API V2.2为一个 Web                        应用程序定义了一组"Welcome"页面,这组页面可以是HTML文件、JSP页面。当你按照 Web                        应用程序的URL路径去调用一个处于 Web 应用程序下的资源而没有指定特定的文件名字时, Web                        应用程序会显示一个"Welcome"页面。例如,有一个 Web 应用程序名字为DefApp,此 Web                        应用程序的"Welcome"页面配置了两个文件"index1.html"和"index1.jsp"。当我们按照下面的URL格式去调用时:http://myHost/webapp/myApp,按照顺序"index1.html"页面会显示出来,如果我们把实际的index1.html文件更名或者删除,再次按照相同的格式调用时,"index1.jsp"就会显示出来。当两者都不存在,系统应该去寻找缺省的页面"index.html"。
返回列表