`
wangyanlong0107
  • 浏览: 502157 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

【转】web.xml文件分析

 
阅读更多

<?xml version="1.0" encoding="UTF-8"?>    <!-- web.xml文件的schema信息 -->                                                 
                                                                                                                           
<!-- 其他的元素都被放到了web-app标签之中 -->                                                                               
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"                                                                           
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                                    
  version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee                                                        
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">                                                                        
                                                                                                                           
  <description>                         <!-- description标签是对站点的描述 -->                                                             
  </description>                                                                                                           
                                                                                                                           
  <display-name>                     <!-- display-name标签定义站点的名称 -->                                                            
  </display-name>                                                                                                          
                                                                                                                           
  <distributable>                      <!-- distributable标签,该站点是否支持分布式管理 -->                                                
  </distributable>                                                                                                         
                                                                                                                           
  <content-param>                   <!-- 用来设定web站台的环境参数-->                                                                  
    <param-name>                    <!-- 参数的名字  -->                                                                               
        MyName                                                                                                               
    </param-name>                                                                                                          
    <param-value>                     <!-- 参数的值 -->                                                                                  
        SuperMos                                                                                                             
    </param-value>                                                                                                         
  </content-param>                  <!-- getServletContent().getInitParameter("MyName"); -->                                           
                                                                                                                           
                                                                                                                           
  <filter>                                   <!-- 用来声明filter的相关的设定 -->                                                                
    <filter-name>                       <!-- 这里保存的当然就是filter的名字 -->                                                            
        SetCharacterEncoding                                                                                                 
    </filter-name>                                                                                                         
                                                                                                                           
    <filter-class>                        <!-- 这里来定义的就是filter类 -->                                                                  
        com.yinhai.SetCharacterEncodingFilter                                                                                
    </filter-clss>                                                                                                         
                                                                                                                           
    <init-param>                        <!-- 这里来定义参数 -->                                                                            
      <param-name>                  <!-- 这里来指定参数的名称 -->                                                                      
          encoding                                                                                                           
      </param-name>                                                                                                        
      <param-value>                   <!-- 这里来制定参数的值 -->                                                                        
          utf-8                                                                                                              
      </param-value>                                                                                                       
    </init-param>                                                                                                          
  </filter>                                  <!-- filter必须和filter-mapping配合使用 -->                                                        
                                                                                                                           
  <filter-mapping>                    <!-- 指定filter所对应的URL -->                                                                     
      <filter-name>                     <!-- 这里来指定filter的名字 -->                                                                    
            SetCharacterEncoding                                                                                             
      </filter-name>                                                                                                       
      <url-pattern>                     <!-- 这里来指定filter所作用的URL -->                                                               
            /*                                                                                                               
      </url-pattern>                                                                                                       
  </filter-mapping>                                                                                                        
                                                                                                                           
  <servlet>                              <!-- 用来声明一个servlet数据 -->                                                                   
    <servlet-name>                                                                                                         
          SuperMos                                                                                                           
    </servlet-name>                                                                                                        
    <servlet-class>                                                                                                        
          com.yinhai.SuperMosServlet                                                                                         
    </servlet-class>                                                                                                       
    <jsp-file>                           <!-- 指定web站台中的某个jsp页面的完整的路径 -->                                                     
                                                                                                                           
    </jsp-file>                                                                                                            
    <init-param>                      <!-- 用来定义参数使用的,里面有name和value -->                                                       
                                                                                                                           
    </init-param>                                                                                                          
  </serlet>                                                                                                                
                                                                                                                           
  <servlet-mapping>                <!-- 和servlet配合使用的标签用来定义url -->                                                         
    <servlet-name>                                                                                                         
        SuperMos                                                                                                             
    </servlet-name>                                                                                                        
    <url-pattern>                                                                                                          
        /yinhai/SuperMosquito                                                                                                
    </url-pattern>                                                                                                         
  </servlet-mapping>                                                                                                       
                                                                                                                           
  <session-config>                     <!-- 用来定义web站点的session参数 -->                                                               
    <session-timeout>                <!-- 用来定义该站点的session有效期,时间为分钟 -->                                                   
        30                                                                                                                   
    </session-timeout>                                                                                                     
  </session-config>                                                                                                        
                                                                                                                           
  <mime-mapping>                    <!-- 定义一个扩展名和某一个MIME type做对应 -->                                                      
    <extension>                          <!-- 扩展名的名称 -->                                                                               
        xls                                                                                                                  
    </extension>                                                                                                           
    <mime-type>                        <!-- 对应的MIME的格式 -->                                                                           
         application/vnd.ms-excel                                                                                             
    </mime-type>                                                                                                           
  </mime-mapping>                                                                                                          
                                                                                                                           
  <welcome-file-list>                 <!-- 用来定义首页的列单 -->                                                                         
    <welcome-file>                    <!-- 定义首页的文件名称 -->                                                                         
          index.jsp                                                                                                          
    </welcome-file>                                                                                                        
    <welcome-file>                                                                                                         
          index.html                                                                                                         
    </welcome-file>                                                                                                        
  </welcome-file-list>                                                                                                     
                                                                                                                           
  <error-page>                        <!-- 用来处理错误的代码或者是异常的页面 -->                                                         
    <error-code>                      <!-- 指定错误的代码  -->                                                                            
        404                                                                                                                  
    </error-code>                                                                                                          
    <location>                           <!-- 指定在web站点内的相应的资源的路径 -->                                                          
         /error404.jsp                                                                                                       
    </location>                                                                                                            
  </error-page>                                                                                                            
  <error-page>                        <!-- 用来处理错误的代码或者是异常的页面 -->                                                         
    <error-code>                      <!-- 指定错误的异常信息  -->                                                                        
          java.lang.Exception                                                                                                
    </error-code>                                                                                                          
    <location>                           <!-- 指定在web站点内的相应的资源的路径 -->                                                          
         /exception.jsp                                                                                                    
    </location>                                                                                                            
  </error-page>                                                                                                            
                                                                                                                           
  <taglib>                               <!-- 用来指定jsp页面用到的tag Library路径  -->                                                      
    <taglib-uri>                        <!-- 指定TLD文件的uri,在jsp页面中用taglib指令取出 -->                                               
        TagLib                                                                                                             
    </taglib-uri>                                                                                                          
    <taglib-location>                <!-- 指定TLD文件的存放位置  -->                                                                     
        /WEB-INF/tlds/TagLib.tld                                                                                           
    </taglib-location>                                                                                                     
  </taglib>                                                                                                                
                                                                                                                           
  <resource-ref>                      <!-- 利用jndi可以取得可用资源,比如数据源 -->                                                         
    <description>                      <!-- 资源的说明 -->                                                                                  
        JNDI JDBC DataSource of SuperMos                                                                                   
    </description>                                                                                                         
    <res-ref-name>                 <!-- 资源的名称 -->                                                                                  
        jdbc/SuperMos_db                                                                                                   
    </res-ref-name>                                                                                                        
    <res-type>                        <!-- 资源的种类 -->                                                                                  
        javax.sql.DataSource                                                                                               
    </res-type>                                                                                                            
    <res-auth>                        <!-- 资源的许可,由Container或者Application -->                                                       
        Container                                                                                                          
    </res-auth>                                                                                                            
    <res-sharing-scope>          <!-- 资源是否可以共享Shareable,Unshareable -->                                                     
        Shareable                                                                                                          
    </res-sharing-scope>                                                                                                   
  </resource-ref>                                                                                                          
                                                                                                                           
</web-app>                                                                                                                

 

 

<jsp-config> 包括<taglib> 和<jsp-property-group> 两个子元素。   
    其中<taglib>元素在JSP 1.2时就已经存在;而<jsp-property-group>是JSP 2.0 新增的元素。
    <jsp-property-group>元素主要有八个子元素,它们分别为:   
    1.<description>:设定的说明;
    2.<display-name>:设定名称;
    3.<url-pattern>:设定值所影响的范围,如:/CH2 或 /*.jsp;
    4.<el-ignored>:若为true,表示不支持EL 语法;
    5.<scripting-invalid>:若为true,表示不支持<% scripting %>语法;
    6.<page-encoding>:设定JSP 网页的编码;
    7.<include-prelude>:设置JSP 网页的抬头,扩展名为.jspf;
    8.<include-coda>:设置JSP 网页的结尾,扩展名为.jspf。

 


一个简单的<jsp-config>元素完整配置:

<jsp-config>
    <taglib>
        <taglib-uri>Taglib</taglib-uri>
        <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
    </taglib>
    <jsp-property-group>
        <description>Special property group for JSP Configuration JSP example.</description>
        <display-name>JSPConfiguration</display-name>
        <url-pattern>/jsp/* </url-pattern>
        <el-ignored>true</el-ignored>
        <page-encoding>GB2312</page-encoding>
        <scripting-invalid>true</scripting-invalid>
        <include-prelude>/include/prelude.jspf</include-prelude>
        <include-coda>/include/coda.jspf</include-coda>
    </jsp-property-group>
</jsp-config>


配置web.xml来限制对某些servlet的请求

 

 

     有时我们只希望通过认证的用户才能请求某些servlet的话,就可以在web.xml中来进行相应的配置,来达到此目的。

这就要用到<security-constraint></security-constraint>元素。
对于tomcat,中web.xml使用security-constraint元素需要在位于<Tomcat-installation-directory>/conf/tomcat-users.xml的XML文件中创建用户名和密码。比如下面的这个tomcat-users.xml文件:

 

 

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,manager"/>
<user username="admin" password="admin" roles="admin"/>
</tomcat-users>

 

此XML片段包括一个tomcat-users根元素,它包含一个或多个role和user元素。
然后在Web应用程序的web.xml中创建security-constraint、login-config和security-role元素。

 

<security-constraint>
      <web-resource-collection>
          <web-resource-name>HelloServlet</web-resource-name>
          <url-pattern>/HelloServlet</url-pattern>
          <http-method>GET</http-method>
          <http-method>POST</http-method>
      </web-resource-collection>


      <auth-constraint>
          <description>This applies only to the "tomcat" security role</description>
          <role-name>admin</role-name>
      </auth-constraint>


      <user-data-constraint>
          <transport-guarantee>NONE</transport-guarantee>
      </user-data-constraint>
</security-constraint>

 

<login-config>
      <auth-method>BASIC</auth-method>
</login-config>


<security-role>
      <role-name>admin</role-name>
</security-role>

 

      其中security-constraint元素包含一个或多个web-resource-collection元素,它是描述Web应用程序中的哪些web资源受到指定安全限制的保护。http-method元素指定安全限制覆盖的HTTP方法。上面的例子中,当我们对/HelloServlet的GET或POST请求时将触发配置的安全机制。
    auth-constraint元素用于描述允许访问Web组件的安全角色。此例中安全角色的例子有tomcat、manager、admin。而只有当作为admin角色的用户才可以访问HelloServlet。

 

    Web应用程序通过login-config元素来认证用户,并确认该用户是否为正确的角色。
     longin-config包含的transport-guarantee子元素用来指定认证方法,BASIC是一种常见的Web认证方式,浏览器给用户提示一个对话框,要求输入用户名和密码,随后Tomcat将给出的用户名和密码与tomcat-users.xml中的用户名和密码进行比较,然后使用前面的security-constraint配置来确定用户是否可访问受保护的servlet。

 

除BASIC外,还可以是FORM、CLIENT-CERT、DIGEST等

 

其实这种认证方法实际上有两个步骤:
1、检查提供的用户名和密码是否正确。
2、判断用户是否映射到特定的安全角色。例如,用户可能提供了正确的用户名和密码,但没有映射到特定的安全角色,也将被禁止访问特定的Web资源

分享到:
评论

相关推荐

    web.xml配置文件详解

    #### 二、web.xml文件结构 `web.xml`文件的最外层标签为`&lt;web-app&gt;`,所有的配置项都需要定义在这个标签内,并且遵循一定的顺序。以下是对`web.xml`主要配置项的详细介绍: #### 三、配置项详解 1. ****:该标签...

    在web.xml中设置错误处理页面.docx

    本文将讲述如何在 web.xml 文件中使用 `&lt;error-page&gt;` 元素来设置错误处理页面,并结合实验结果,分析 JSP 的基本语法和错误处理机制。 web.xml 文件的 `&lt;error-page&gt;` 元素 在 JavaWeb 应用程序中,web.xml 文件...

    Jboss启动报Failed to parse WEB-INFweb.xml; - nested throwable错误

    进一步分析错误日志,我们可以看到,Jboss抛出了一个 Duplicate throwable nesting of same base type的警告,这提示我们Jboss在解析web.xml文件时遇到了问题。 解决方案 要解决Failed to parse WEB-INF/web.xml; ...

    web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序

    ### web.xml文件中配置(servlet, spring, filter, listener)的加载顺序 在Java Web应用开发中,`web.xml`文件是整个Web应用程序的核心配置文件之一,它定义了Servlet容器如何启动、初始化以及配置各个组件如...

    Web.xml配置详解

    `web.xml` 文件是Java Web应用程序的核心配置文件之一,主要用于配置应用程序级别的各种初始化参数、监听器、过滤器、Servlet映射等。通过`web.xml`,开发者可以灵活地控制Java Web应用的运行方式。 #### 二、Web....

    web.xml sample

    在这个例子中,我们将详细分析给出的`web.xml`片段,探讨其中的关键配置元素。 1. **欢迎文件**: `&lt;welcome-file-list&gt;`元素定义了当用户访问应用的根目录时应显示的默认页面。在这个例子中,如果用户输入`...

    Tomcat(二) Tomcat实现:Servlet与web.xml介绍 以及 源码分析Tomcat实现细节1

    在web.xml文件中,你可以通过`&lt;listener&gt;`元素定义监听器,当Web应用启动时,Tomcat会实例化这些监听器并调用相应的初始化方法。 ```xml &lt;listener-class&gt;org.springframework.web.context....

    Spring全注解project示例 (无web.xml配置)

    标题 "Spring全注解project示例 (无web.xml配置)" 提供了我们即将探讨的核心主题:一个使用Spring框架,完全依赖注解配置的项目,且没有传统的web.xml部署描述符。这种配置方式在现代Spring应用中非常常见,因为它...

    weblogic.xml配置详解

    `weblogic.xml` 文件是 WebLogic Server 应用服务器中专门用于自定义和配置部署在该服务器上的 Web 应用程序的一个重要配置文件。它允许开发人员和系统管理员针对 WebLogic Server 的特定特性进行定制设置,以满足...

    java 导出Excel文件 网页方式打开 web.xml配置

    在Java开发中,导出Excel文件并以网页方式打开是一个常见的需求,特别是在Web应用程序中,例如数据分析、报表生成等场景。下面将详细讲解如何通过Java实现这个功能,并结合`web.xml`配置进行优化。 首先,Java导出...

    Servlet中Web.xml详解

    部署描述符文件如同所有的XML文件一样,必须以一个XML头开始。这个头声明指明了使用的XML版本及文件的字符编码。例如: ```xml &lt;?xml version="1.0" encoding="UTF-8"?&gt; ``` 紧接着是`DOCTYPE`声明,它指定了适用...

    struts2.5 web.xml配置

    `web.xml`是Java Web应用的核心配置文件之一,用于定义应用程序级别的配置信息,如过滤器、监听器、欢迎页面列表等。下面将详细介绍Struts2.5中的`web.xml`配置要点。 ### Struts2.5 Web.xml配置概述 #### 1. **...

    javax.xml.rpc

    不过,由于这是一个图像文件,我们无法直接从中获取文字信息,需要查看图片内容才能进一步分析。 总的来说,解决"javax.xml.rpc"找不到的问题通常需要检查项目配置,特别是类路径和构建设置,确保所有的依赖都被...

    weblogic部署到myeclipse后web.xml的问题

    ### WebLogic部署到MyEclipse后web.xml的问题分析与解决 #### 问题概述 在将Web应用程序通过MyEclipse部署到WebLogic服务器时遇到`web.xml`格式错误的问题。具体表现为:部署过程中提示`web.xml`文件格式不正确(`...

    web.xml的详解

    通过以上对`web.xml`文件的详细分析,我们可以看到`web.xml`在Java Web开发中的核心地位。它是配置Web应用的核心文件,通过合理地配置各个元素,开发者可以灵活地控制Web应用的行为和功能。掌握`web.xml`的配置细节...

    dwr.xml文件

    dwr.xml文件是DWR框架的核心配置文件之一,通过上述分析可以看出,它主要包含了DWR Servlet的配置信息,如Servlet的名称、类名、初始化参数以及映射的URL路径等。这些配置对于DWR框架正常运行至关重要,通过合理的...

    Web.xml配置文件[文].pdf

    本文将对Web.xml配置文件的各个元素进行详细的解释和分析。 1. `&lt;web-app&gt;`元素 `&lt;web-app&gt;`元素是Web.xml配置文件的根元素,用于定义Web应用程序的所有配置信息。 2. `&lt;display-name&gt;`元素 `&lt;display-name&gt;`元素...

    web.xml中的listen

    ### Web.xml中的Listen知识点详解 ...通过上述分析可以看出,`web.xml`中的监听器不仅可以帮助我们加载Spring配置文件,还可以实现更灵活的功能,如监听自定义Java类等。这对于扩展Web应用的功能非常有用。

    jeecg-bpm开发过程常见问题

    在jeecg-bpm的开发过程中,web.xml文件是一个非常重要的配置文件,但是有时候我们可能会遇到web.xml文件中的错误。下面是解决web.xml错误的步骤: 1. 删除标记&joa; 在web.xml文件中,删除&joa;标记,以便能够正确...

    Jmeter+ant实现测试报告build.xml和报告模板jmeter-results-newreport.xsl

    3. **build.xml文件详解**:`build.xml`是Ant的构建文件,其中包含了一系列的任务和目标。例如,你可以定义一个目标来启动JMeter,另一个目标来生成测试报告,甚至还有一个目标用来发送测试结果的邮件通知。在描述中...

Global site tag (gtag.js) - Google Analytics