如何打包JSP工程为war文件[转自sun: Packaging Web Components]

Packaging Web Components

You add web components to a J2EE application in a package called a web application archive (WAR), which is a JAR similar to the package used for Java class libraries. A WAR usually contains other resources besides web components, including:

  • Server-side utility classes (database beans, shopping carts, and so on).
  • Static web resources (HTML, image, and sound files, and so on)
  • Client-side classes (applets and utility classes)

A WAR has a specific hierarchical directory structure. The top-level directory of a WAR is the document root of the application. The document root is where JSP pages, client-side classes and archives, and static web resources are stored.

The document root contains a subdirectory called WEB-INF, which contains the following files and directories:

  • web.xml - the web application deployment descriptor
  • Tag library descriptor files (see Tag Library Descriptors).
  • classes - a directory that contains server-side classes: servlet, utility classes, and JavaBeans components.
  • lib - a directory that contains JAR archives of libraries (tag libraries and any utility libraries called by server-side classes).

You can also create application-specific subdirectories (that is , package directories) in either the document root or the WEB-INF/classes directory.


Note: When you add classes and archives to a WAR, deploytool automatically packages them in the WEB-INF subdirectory. This is correct for web components and server-side utility classes, but incorrect for client-side classes such as applets and any archives accessed by applets. To put client-side classes and archives in the correct location you must "drag" them to the document root after you have added them to the archive.

Creating a WAR

When you add the first web component to a J2EE application, deploytool automatically creates a new WAR to contain the component. A later section describes how to add a web component.

You can also manually create a WAR in three ways:

  • With the packager tool distributed with the J2EE SDK. This tool is described in Packager.
  • With the war task of the ant portable build tool. Ant is used to build the J2EE Tutorial examples. The example application described in The Example JSP Pages uses ant to create the WAR.
  • With the JAR tool distributed with the J2SE. If you arrange your application development directory in the structure required by the WAR format, it is straightforward to create a web application archive file in the required format. You simply execute the following command in the top-level directory of the application:
    jar cvf archiveName.war .

                           [注:重要的就是在这里,jar命令也可以这样用!并不需要配置文件]

Note that in order to use any of these methods, you must also manually create a deployment descriptor in the correct format.

Adding a WAR to a J2EE Application

If you manually create a WAR or you obtain a WAR from another party, you can add it to an existing J2EE application as follows:

  1. Select a J2EE application.
  2. Select File->Add->Web WAR.
  3. Navigate to the directory containing the WAR, select the WAR, and click Add Web WAR.

See The Example JSP Pages for an example.

You can also add a WAR to a J2EE application using the packager tool. The Duke's Bank application described in Building, Deploying, and Running the Application uses packager.

Adding a Web Component to a WAR

The following procedure describes how to create and add the web component in the Hello1App application to a WAR. Although the web component wizard solicits WAR and component-level configuration information when you add the component, this chapter describes how to add the component and provide configuration information at a later time using application, WAR, and web component inspectors:

    • Go to j2eetutorial/examples/src and build the example by running ant hello1. For detailed instructions, see About the Examples).
    • Create a J2EE application called Hello1App.
      1. Select File->New->Application.
      2. Click Browse.
      3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
      4. In the File Name field, enter Hello1App .
      5. Click New Application.
      6. Click OK.
    • Create a J2EE application called Hello1App.
      1. Select File->New->Application.
      2. Click Browse.
      3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
      4. In the File Name field, enter Hello1App .
      5. Click New Application.
      6. Click OK.
      1. Select File->New->Application.
      2. Click Browse.
      3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
      4. In the File Name field, enter Hello1App .
      5. Click New Application.
      6. Click OK.
    • Create a J2EE application called Hello1App.
      1. Select File->New->Application.
      2. Click Browse.
      3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
      4. In the File Name field, enter Hello1App .
      5. Click New Application.
      6. Click OK.
    • Create a J2EE application called Hello1App.
      1. Select File->New->Application.
      2. Click Browse.
      3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
      4. In the File Name field, enter Hello1App .
      5. Click New Application.
      6. Click OK.
      1. Select File->New->Application.
      2. Click Browse.
      3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
      4. In the File Name field, enter Hello1App .
      5. Click New Application.
      6. Click OK.
    • Create a J2EE application called Hello1App.
      1. Select File->New->Application.
      2. Click Browse.
      3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
      4. In the File Name field, enter Hello1App .
      5. Click New Application.
      6. Click OK.
      1. Select File->New->Application.
      2. Click Browse.
      3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
      4. In the File Name field, enter Hello1App .
      5. Click New Application.
      6. Click OK.
    • Create the WAR and add the GreetingServlet web component and all the of the Hello1App application content.
      1. Invoke the web component wizard by selecting File->New->Web Component.
      2. In the combo box labelled Create New WAR File in Application select Hello1App. Enter Hello1WAR in the field labeled WAR Display Name.
      3. Click Edit to add the content files.
      4. In the Edit Contents dialog, navigate to j2eetutorial/examples/build/web/hello1. Select GreetingServlet.class, ResponseServlet.class, and duke.waving.gif, and click Add. Click OK.
      5. Click Next.
      6. Select the servlet radio button.
      7. Click Next.
      8. Select GreetingServlet from the Servlet Class combo box.
      9. Click Finish.
    • Add the ResponseServlet web component.
      1. Invoke the web component wizard by selecting File->New->Web Component.
      2. In the combo box labelled Add to Existing WAR File select Hello1WAR.
      3. Click Next.
      4. Select the servlet radio button.
      5. Click Next.
      6. Select ResponseServlet from the Servlet Class combo box.
      7. Click Finish.
    1. Select File->New->Application.
    2. Click Browse.
    3. In the file chooser, navigate to j2eetutorial/examples/src/web/hello1.
    4. In the File Name field, enter Hello1App .
    5. Click New Application.
    6. Click OK.

Note: You can add JSP pages to a WAR without creating a new web component for each page. You simply select the WAR, click Edit to edit the contents of the WAR, and add the pages. The JSP version of the Hello, World application, described in Updating Web Components , shows how to do this. If you choose this method, you will not be able to specify alias paths (described in Specifying an Alias Path ) for the pages.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值