注冊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資源。