随笔 - 5  文章 - 4  trackbacks - 0

留言簿

文章分类(2)

文章档案(7)

搜索

  •  

最新评论

注册servlet/映射servlet
 web.xml中

<servlet>
    <servlet-name>HelloWorldServlet</servlet-name>   //注册
    <servlet-class>testservlet.HelloWorldServlet</servlet-class>
</servlet>
</servlet-mapping><servlet-mapping>                  //映射
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/helloworld</url-pattern>
</servlet-mapping>
</servlet-mapping><servlet-mapping>                 //多个映射
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/hd</url-pattern>
</servlet-mapping>

可以通过 http://host:port/context path/servlet/registered-servlet-name (貌似这种没调试出来,汗。。。)
使用通配符 *
<url-pattern>/login/*</url-pattern>  //url地址后的login都将用这个servlet来请求
<url-pattern>/*</url-pattern> //所以非servlet的页面和其他路径都将用这个servlet来请求,如果有这样的servlet,将被优先。
<url-pattern>/servlet/*</url-pattern> //用来匹配 http://host:port/context path/servlet/registered-servlet-name 访问servlet模式

甚至可以 <url-pattern>*.jsp</url-pattern> <url-pattern>*.html</url-pattern> 这叫扩展映射
在servlet中转发请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        out.println("Login ...");
        RequestDispatcher dispather = null;
        String param = request.getParameter("go");
        if(param == null ){
            throw new ServletException("Missing parameter!");  //不知道这些异常在何处捕获
        }else if(param.equals("h")){
            dispather = request.getRequestDispatcher("/helloworld"); //定义将要转发位置
              //或者 dispather = getServletContext().getNamedDispatcher(“注册的类名”);
        }else{
            throw new ServletException("wrong parameter");
        }
        if(dispather != null){
            dispather.forward(request,response);  //进行转发
        }else{
            throw new ServletException("dispather = null!");
        }
    } 
为web应用程序创建欢迎文件

在web.xml中配置
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>


限制对某些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资源。
 

posted on 2006-12-08 11:48 googlor 阅读(523) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: