What is WEB-INF used for in a Java EE web application?

后端 未结 5 664
花落未央
花落未央 2020-11-22 10:39

I\'m working on a Java EE web application with the following source code structure:

src/main/java                 &l         


        
5条回答
  •  Happy的楠姐
    2020-11-22 10:57

    When you deploy a Java EE web application (using frameworks or not),its structure must follow some requirements/specifications. These specifications come from :

    • The servlet container (e.g Tomcat)
    • Java Servlet API
    • Your application domain
    1. The Servlet container requirements
      If you use Apache Tomcat, the root directory of your application must be placed in the webapp folder. That may be different if you use another servlet container or application server.

    2. Java Servlet API requirements
      Java Servlet API states that your root application directory must have the following structure :

      ApplicationName
      |
      |--META-INF
      |--WEB-INF
            |_web.xml       <-- Here is the configuration file of your web app(where you define servlets, filters, listeners...)
            |_classes       <--Here goes all the classes of your webapp, following the package structure you defined. Only 
            |_lib           <--Here goes all the libraries (jars) your application need
      

    These requirements are defined by Java Servlet API.

    3. Your application domain
    Now that you've followed the requirements of the Servlet container(or application server) and the Java Servlet API requirements, you can organize the other parts of your webapp based upon what you need.
    - You can put your resources (JSP files, plain text files, script files) in your application root directory. But then, people can access them directly from their browser, instead of their requests being processed by some logic provided by your application. So, to prevent your resources being directly accessed like that, you can put them in the WEB-INF directory, whose contents is only accessible by the server.
    -If you use some frameworks, they often use configuration files. Most of these frameworks (struts, spring, hibernate) require you to put their configuration files in the classpath (the "classes" directory).

提交回复
热议问题