String value = java.net.URLEncoder.encode("中文","utf-8");
Cookie cookie = new Cookie("chinese_code",value);
cookie.setMaxAge(60*60*24*6);
response.addCookie(cookie);
encode() 只有一個(gè)參數(shù)的已經(jīng)過(guò)時(shí)了,現(xiàn)在可以設(shè)置編碼格式, 取cookie值的時(shí)候 也不用解碼了。
我們注意到它里面包含了這段配置:<load-on-startup>1</load-on-startup>,那么這個(gè)配置有什么作用呢?
貼一段英文原汁原味的解釋如下:
Servlet specification:
The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.
翻譯過(guò)來(lái)的意思大致如下:
1)load-on-startup元素標(biāo)記容器是否在啟動(dòng)的時(shí)候就加載這個(gè)servlet(實(shí)例化并調(diào)用其init()方法)。
2)它的值必須是一個(gè)整數(shù),表示servlet應(yīng)該被載入的順序
2)當(dāng)值為0或者大于0時(shí),表示容器在應(yīng)用啟動(dòng)時(shí)就加載并初始化這個(gè)servlet;
3)當(dāng)值小于0或者沒(méi)有指定時(shí),則表示容器在該servlet被選擇時(shí)才會(huì)去加載。
4)正數(shù)的值越小,該servlet的優(yōu)先級(jí)越高,應(yīng)用啟動(dòng)時(shí)就越先加載。
5)當(dāng)值相同時(shí),容器就會(huì)自己選擇順序來(lái)加載。
所以,<load-on-startup>x</load-on-startup>,中x的取值1,2,3,4,5代表的是優(yōu)先級(jí),而非啟動(dòng)延遲時(shí)間。
如下題目:
2.web.xml中不包括哪些定義(多選)
a.默認(rèn)起始頁(yè)
b.servlet啟動(dòng)延遲時(shí)間定義
c.error處理頁(yè)面
d.jsp文件改動(dòng)后重新載入時(shí)間
答案:b,d
通常大多數(shù)Servlet是在用戶第一次請(qǐng)求的時(shí)候由應(yīng)用服務(wù)器創(chuàng)建并初始化,但<load-on-startup>n</load-on-startup> 可以用來(lái)改變這種狀況,根據(jù)自己需要改變加載的優(yōu)先級(jí)!
研究發(fā)現(xiàn):屬性(變量)可分為三類(對(duì)象屬性、全局變量和局部變量)
對(duì)象屬性:聲明時(shí)以“this.”開(kāi)頭,只能被“類的實(shí)例”即對(duì)象所調(diào)用,不能被“類內(nèi)部(對(duì)外不對(duì)內(nèi))”調(diào)用;全局變量:聲明時(shí)直接以變量名開(kāi)頭,可以任意調(diào)用(對(duì)內(nèi)對(duì)外);局部變量:只能被
“類內(nèi)部(對(duì)內(nèi)不對(duì)外)”調(diào)用。
JS函數(shù)的聲明與訪問(wèn)原理
<script type="text/javas
//類
var testClass = function(){
//對(duì)象屬性(對(duì)外不對(duì)內(nèi),類調(diào)用)
this.age ="25";
//全局變量(對(duì)內(nèi)對(duì)外)
name="jack";
//局部變量(對(duì)內(nèi)不對(duì)外)
var address = "beijing";
//全局函數(shù)(對(duì)內(nèi)對(duì)外)
add = function(a,b){
//可訪問(wèn):全局變量和局部變量
multiply(a,b);
return a+b;
}
//實(shí)例函數(shù)(由類的對(duì)象調(diào)用)
this.minus = function(a,b){
//可以訪問(wèn):對(duì)象屬性、全局變量和局部變量
return a-b;
}
//局部函數(shù)(內(nèi)部直接調(diào)用)
var multiply = function(a,b){
//只能訪問(wèn):全局變量和局部變量
return a*b;
}
}
//類函數(shù)(由類名直接調(diào)用)
testClass.talk= function(){
//只能訪問(wèn):全局變量和全局函數(shù)
this.what = function(){
alert("What can we talk about?");
about();
}
var about = function(){
alert("about name:"+name);
alert("about add(1,1):"+add(1,1));
}
}
//原型函數(shù)(由類的對(duì)象調(diào)用)
testClass.prototype.walk = function(){
//只能訪問(wèn):全局變量和全局函數(shù)
this.where = function(){
alert("Where can we go?");
go();
}
var go = function(){
alert("go name:"+name);
alert("go add(1,1):"+add(1,1));
}
}
</script>
下面看看如何調(diào)用:
<script type="text/javas
//獲取一個(gè)cbs類的實(shí)例
var cbs= new testClass();
//調(diào)用類的對(duì)象屬性age
alert("age:"+cbs.age);
//獲取類函數(shù)talk的實(shí)例
var talk = new testClass.talk();
//調(diào)用類函數(shù)的實(shí)例函數(shù)
talk.what();
//獲取原型函數(shù)walk的實(shí)例
var walk = new cbs.walk();
//調(diào)用原型函數(shù)的實(shí)例函數(shù)
walk.where();
</script>
早起的國(guó)內(nèi)互聯(lián)網(wǎng)都使用GBK編碼,久之,所有項(xiàng)目都以GBK來(lái)編碼了。對(duì)于J2EE項(xiàng)目,為了減少編碼的干擾通常都是設(shè)置一個(gè)編碼的Filter,強(qiáng)制將Request/Response編碼改為GBK。例如一個(gè)Spring的常見(jiàn)配置如下:
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
毫無(wú)疑問(wèn),這在GBK編碼的頁(yè)面訪問(wèn)、提交數(shù)據(jù)時(shí)是沒(méi)有亂碼問(wèn)題的。但是遇到Ajax就不一樣了。Ajax強(qiáng)制將中文內(nèi)容進(jìn)行UTF-8編碼,這樣導(dǎo)致進(jìn)入后端后使用GBK進(jìn)行解碼時(shí)發(fā)生亂碼。網(wǎng)上的所謂的終極解決方案都是扯淡或者過(guò)于復(fù)雜,例如下面的文章:
這樣的文章很多,顯然:
如果提交數(shù)據(jù)的時(shí)候能夠告訴后端傳輸?shù)木幋a信息是否就可以避免這種問(wèn)題?比如Ajax請(qǐng)求告訴后端是UTF-8,其它請(qǐng)求告訴后端是GBK,這樣后端分別根據(jù)指定的編碼進(jìn)行解碼是不是就解決問(wèn)題了。
有兩個(gè)問(wèn)題:
解決了上述問(wèn)題,來(lái)看具體實(shí)現(xiàn)方案。 列一段Java代碼:
import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.util.ClassUtils; import org.springframework.web.filter.OncePerRequestFilter; /** 自定義編碼過(guò)濾器 * @author imxylz (imxylz#gmail.com) * @sine 2011-6-9 */ public class MutilCharacterEncodingFilter extends OncePerRequestFilter { static final Pattern inputPattern = Pattern.compile(".*_input_encode=([\\w-]+).*"); static final Pattern outputPattern = Pattern.compile(".*_output_encode=([\\w-]+).*"); // Determine whether the Servlet 2.4 HttpServletResponse.setCharacterEncoding(String) // method is available, for use in the "doFilterInternal" implementation. private final static boolean responseSetCharacterEncodingAvailable = ClassUtils.hasMethod(HttpServletResponse.class, "setCharacterEncoding", new Class[] { String.class }); private String encoding; private boolean forceEncoding = false; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String url = request.getQueryString(); Matcher m = null; if (url != null && (m = inputPattern.matcher(url)).matches()) {//輸入編碼 String inputEncoding = m.group(1); request.setCharacterEncoding(inputEncoding); m = outputPattern.matcher(url); if (m.matches()) {//輸出編碼 response.setCharacterEncoding(m.group(1)); } else { if (this.forceEncoding && responseSetCharacterEncodingAvailable) { response.setCharacterEncoding(this.encoding); } } } else { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding && responseSetCharacterEncodingAvailable) { response.setCharacterEncoding(this.encoding); } } } filterChain.doFilter(request, response); } public void setEncoding(String encoding) { this.encoding = encoding; } public void setForceEncoding(boolean forceEncoding) { this.forceEncoding = forceEncoding; } }
解釋下:
http://confluence.redsaga.com/pages/viewpage.action?pageId=1643
JavaScript API :http://confluence.redsaga.com/display/BUFFALO/JavaScript+API?修改后的結(jié)果以post方式提交給/fck/servlet/ContextServlet,該url對(duì)應(yīng)的即為ContextServlet。
ContextServlet負(fù)責(zé)讀取FCKeditor里的內(nèi)容,并賦予給session中的ContextBean。doPost()方法用于實(shí)現(xiàn)該功能
需要注意兩個(gè)問(wèn)題,
其一:FCKeditor內(nèi)的中文信息讀取是可能出現(xiàn)亂碼,需要額外的處理:
?? String?name=new?String(request.getParameter("EditorDefault").getBytes("ISO-8859-1"),"UTF-8");
其二:由于servlet處于FacesContext范圍外,因此不能通過(guò)FacesContext.getCurrentInstance()來(lái)得到當(dāng)前FacesContext,因此ContextServlet定義了自己的方法用于獲取FacesContext:
?? ContextServlet處理完了FCKeditor內(nèi)容后,將跳轉(zhuǎn)到form.jsp。
這樣一個(gè)簡(jiǎn)單的編輯功能就完成了。
3.遺留問(wèn)題:
?? 我在上傳文件時(shí)還是會(huì)出現(xiàn)中文亂碼的問(wèn)題,按照其他人說(shuō)的那樣把網(wǎng)頁(yè)的charset=utf-8改成gb2312一樣會(huì)有問(wèn)題,還請(qǐng)各位高手賜教^_^
另外,關(guān)于整個(gè)demo的源代碼如果大家需要,可以留言給我,我用郵箱發(fā)給您。不足之處,還請(qǐng)各位多多指點(diǎn)
開(kāi)發(fā)環(huán)境: FCKeditor 版本 FCKeditor_2.3.1 FCKeditor.Java 2.3 下載地址: http://www.fckeditor.net/download/default.html 開(kāi)始: 解壓 FCKeditor_2.3.1 包中的 fckconfig.js、fckeditor.js、fckstyles.xml、fcktemplates.xml 文件夾到項(xiàng)目中的 WebContent 目錄 解壓 FCKeditor-2.3.zip 包中的 \web\WEB-INF\lib 下的兩個(gè) jar 文件到項(xiàng)目的 WebContent\WEB-INF\lib 目錄 解壓 FCKeditor-2.3.zip 包中的 \src 下的 FCKeditor.tld 文件到項(xiàng)目的 WebContent\WEB-INF 目錄 刪除 WebContent\edit 目錄下的 _source 文件夾 修改 web.xml 文件,加入以下內(nèi)容
代碼
< servlet > 新建一個(gè)提交頁(yè) jsp1.jsp 文件和一個(gè)接收頁(yè) jsp2.jsp 文件 jsp1.jsp 代碼如下: 代碼 <%@ page contentType = "text/html;charset=UTF-8" language = "java" %> 在 WebContent 目錄下新建 UserFiles 文件夾,在此文件夾下新建 File,Image,Flash 三個(gè)文件夾。 ok現(xiàn)在運(yùn)行一下看看吧! |
試用了一下FCKeditor,根據(jù)網(wǎng)上的文章小結(jié)一下:
1.下載
FCKeditor.java 2.3 (FCKeditot for java)
FCKeditor 2.2 (FCKeditor基本文件)
2.建立項(xiàng)目:tomcat/webapps/TestFCKeditor.
3.將FCKeditor2.2解壓縮,將整個(gè)目錄FCKeditor復(fù)制到項(xiàng)目的根目錄下,
目錄結(jié)構(gòu)為:tomcat/webapps/TestFCKeditor/FCKeditor
然后將FCKeditor-2.3.zip(java)壓縮包中\(zhòng)web\WEB-INF\lib\目錄下的兩個(gè)jar文件拷到項(xiàng)目的\WEB-INF\lib\目錄下。把其中的src目錄下的FCKeditor.tld文件copy到TestFCKedit/FCKeitor/WEB-INF/下
4.將FCKeditor-2.3.zip壓縮包中\(zhòng)web\WEB-INF\目錄下的web.xml文件合并到項(xiàng)目的\WEB-INF\目錄下的web.xml文件中。
5. 修改合并后的web.xml文件,將名為SimpleUploader的Servlet的enabled參數(shù)值改為true,
以允許上傳功能,Connector Servlet的baseDir參數(shù)值用于設(shè)置上傳文件存放的位置。
添加標(biāo)簽定義:
<taglib>
<taglib-uri>/TestFCKeditor</taglib-uri>
<taglib-location>/WEB-INF/FCKeditor.tld</taglib-location>
</taglib>
運(yùn)行圖:
6. 上面文件中兩個(gè)servlet的映射分別為:/editor/filemanager/browser/default/connectors/jsp/connector
和/editor/filemanager/upload/simpleuploader,需要在兩個(gè)映射前面加上/FCKeditor,
即改為/FCKeditor/editor/filemanager/browser/default/connectors/jsp/connector和
/FCKeditor/editor/filemanager/upload/simpleuploader。
7.進(jìn)入skin文件夾,如果你想使用fckeditor默認(rèn)的這種奶黃色,
那就把除了default文件夾外的另兩個(gè)文件夾直接刪除.
8.刪除/FCKeditor/目錄下除fckconfig.js, fckeditor.js, fckstyles.xml, fcktemplates.xml四個(gè)文件以外的所有文件
刪除目錄/editor/_source,
刪除/editor/filemanager/browser/default/connectors/下的所有文件
刪除/editor/filemanager/upload/下的所有文件
刪除/editor/lang/下的除了fcklanguagemanager.js, en.js, zh.js, zh-cn.js四個(gè)文件的所有文件
9.打開(kāi)/FCKeditor/fckconfig.js
修改 FCKConfig.DefaultLanguage = 'zh-cn' ;
把FCKConfig.LinkBrowserURL等的值替換成以下內(nèi)容:
FCKConfig.LinkBrowserURL
= FCKConfig.BasePath + "filemanager/browser/default/browser.html?Connector=connectors/jsp/connector" ;
FCKConfig.ImageBrowserURL
= FCKConfig.BasePath + "filemanager/browser/default/browser.html?Type=Image&Connector=connectors/jsp/connector" ;
FCKConfig.FlashBrowserURL
= FCKConfig.BasePath + "filemanager/browser/default/browser.html?Type=Flash&Connector=connectors/jsp/connector" ;
FCKConfig.LinkUploadURL = FCKConfig.BasePath + 'filemanager/upload/simpleuploader?Type=File' ;
FCKConfig.FlashUploadURL = FCKConfig.BasePath + 'filemanager/upload/simpleuploader?Type=Flash' ;
FCKConfig.ImageUploadURL = FCKConfig.BasePath + 'filemanager/upload/simpleuploader?Type=Image' ;
10.其它
fckconfig.js總配置文件,可用記錄本打開(kāi),修改后將文件存為utf-8 編碼格式。找到:
FCKConfig.TabSpaces = 0 ; 改為FCKConfig.TabSpaces = 1 ; 即在編輯器域內(nèi)可以使用Tab鍵。
如果你的編輯器還用在網(wǎng)站前臺(tái)的話,比如說(shuō)用于留言本或是日記回復(fù)時(shí),那就不得不考慮安全了,
在前臺(tái)千萬(wàn)不要使用Default的toolbar,要么自定義一下功能,要么就用系統(tǒng)已經(jīng)定義好的Basic,
也就是基本的toolbar,找到:
FCKConfig.ToolbarSets["Basic"] = [
['Bold','Italic','-','OrderedList','UnorderedList','-',/*'Link',*/'Unlink','-','Style','FontSize','TextColor','BGColor','-',
'Smiley','SpecialChar','Replace','Preview'] ] ;
這是改過(guò)的Basic,把圖像功能去掉,把添加鏈接功能去掉,因?yàn)閳D像和鏈接和flash和圖像按鈕添加功能都能讓前臺(tái)
頁(yè)直接訪問(wèn)和上傳文件, fckeditor還支持編輯域內(nèi)的鼠標(biāo)右鍵功能。
FCKConfig.ContextMenu = ['Generic',/*'Link',*/'Anchor',/*'Image',*/'Flash','Select','Textarea','Checkbox','Radio','TextField','HiddenField',
/*'ImageButton',*/'Button','BulletedList','NumberedList','TableCell','Table','Form'] ;
這也是改過(guò)的把鼠標(biāo)右鍵的“鏈接、圖像,F(xiàn)LASH,圖像按鈕”功能都去掉。
找到: FCKConfig.FontNames = 'Arial;Comic Sans MS;Courier New;Tahoma;Times New Roman;Verdana' ;
加上幾種我們常用的字體
FCKConfig.FontNames
= '宋體;黑體;隸書(shū);楷體_GB2312;Arial;Comic Sans MS;Courier New;Tahoma;Times New Roman;Verdana' ;
7.添加文件 /TestFCKeditor/test.jsp:
<%@ page language="java" import="com.fredck.FCKeditor.*" %>
<%@ taglib uri="/TestFCKeditor" prefix="FCK" %>
<script type="text/javascript" src="/TestFCKeditor/FCKeditor/fckeditor.js"></script>
<%--
三種方法調(diào)用FCKeditor
1.FCKeditor自定義標(biāo)簽 (必須加頭文件 <%@ taglib uri="/TestFCKeditor" prefix="FCK" %> )
2.script腳本語(yǔ)言調(diào)用 (必須引用 腳本文件 <script type="text/javascript" src="/TestFCKeditor/FCKeditor/fckeditor.js"></script> )
3.FCKeditor API 調(diào)用 (必須加頭文件 <%@ page language="java" import="com.fredck.FCKeditor.*" %> )
--%>
<%--
<form action="show.jsp" method="post" target="_blank">
<FCK:editor id="content" basePath="/TestFCKeditor/FCKeditor/"
width="700"
height="500"
skinPath="/TestFCKeditor/FCKeditor/editor/skins/silver/"
toolbarSet = "Default"
>
input
</FCK:editor>
<input type="submit" value="Submit">
</form>
--%>
<form action="show.jsp" method="post" target="_blank">
<table border="0" width="700"><tr><td>
<textarea id="content" name="content" style="WIDTH: 100%; HEIGHT: 400px">input</textarea>
<script type="text/javascript">
var oFCKeditor = new FCKeditor('content') ;
oFCKeditor.BasePath = "/TestFCKeditor/FCKeditor/" ;
oFCKeditor.Height = 400;
oFCKeditor.ToolbarSet = "Default" ;
oFCKeditor.ReplaceTextarea();
</script>
<input type="submit" value="Submit">
</td></tr></table>
</form>
<%--
<form action="show.jsp" method="post" target="_blank">
<%
FCKeditor oFCKeditor ;
oFCKeditor = new FCKeditor( request, "content" ) ;
oFCKeditor.setBasePath( "/TestFCKeditor/FCKeditor/" ) ;
oFCKeditor.setValue( "input" );
out.println( oFCKeditor.create() ) ;
%>
<br>
<input type="submit" value="Submit">
</form>
--%>
添加文件/TestFCKeditor/show.jsp:
<%
String content = request.getParameter("content");
out.print(content);
%>
8.瀏覽http://localhost:8080/TestFCKeditor/test.jsp
ok!
<display-name>元素定義這個(gè)web應(yīng)用的名字,Java Web 服務(wù)器的Web管理工具將用這個(gè)名字來(lái)標(biāo)志W(wǎng)eb應(yīng)用。
<description>元素用來(lái)聲明Web應(yīng)用的描述信息
<context-param>元素用來(lái)配置外部引用的,在servlet中如果要獲得該元素中配置的值,String param-value = getServletContext().getInitParameter("param-name")
<filter>
<filter-name>SampleFilter</filter-name>
<filter-class>com.lpdev.SampleFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SampleFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
以上是配置了一個(gè)servlet過(guò)濾器,對(duì)于servlet容器收到的客戶請(qǐng)求以及發(fā)出的響應(yīng)結(jié)果,servlet都能檢查和修改其中的信息,以上代碼指名當(dāng)客戶請(qǐng)求訪問(wèn)Web應(yīng)用中的所有JSP文件時(shí),將觸發(fā)SampleFilter過(guò)濾器工作,具體的過(guò)濾事務(wù)在由<filter-class>中指定的類來(lái)完成
<servlet>
<servlet-name>IncludeServlet</servlet-name>
<servlet-class>com.lpdev.IncludeServlet</servlet-class>
<init-param>
<param-name>copyright</param-name>
<param-value>/foot.jspf</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
</servlet>
配置Servlet,<servlet-name>是servlet的名字,<servlet-class>是實(shí)現(xiàn)這個(gè)Servlet的類,<init-param>定義Servlet的初始化參數(shù)(參數(shù)名和參數(shù)值),一個(gè)Servlet可以有多個(gè)<init-param>,在Servlet類中通過(guò)getInitParameter(String name)方法訪問(wèn)初始化參數(shù)
<servlet-mapping>
<servlet-name>IncludeServlet</servlet-name>
<url-pattern>/IncludeServlet</url-pattern>
</servlet-mapping>
配置Servlet映射,<servlet-mapping>元素用來(lái)設(shè)定客戶訪問(wèn)某個(gè)Servlet的URL,這里只需給出對(duì)于整個(gè)web應(yīng)用的相對(duì)的URL路徑,<url-pattern>中的“/”表示開(kāi)始于Web應(yīng)用的根目錄例如,如果你在你本地機(jī)器上使用Tomcat4.1.x,并且創(chuàng)建了名為“myapp”的應(yīng)用程序,<url-pattern>/IncludeServlet</url-pattern>該Servlet的完整web地址就是http://localhost:8080/myapp/IncludeServlet
<session-config>元素用來(lái)設(shè)定HttpSession的生命周期,該元素只有一個(gè)<session-timeout>屬性,時(shí)間單位是“秒”。
<welcome-file-list>當(dāng)用戶訪問(wèn)web時(shí),如果僅僅給出web應(yīng)用的Root URL,沒(méi)有指定具體文件名,容器調(diào)用該配置,該元素可以包含多個(gè)<welcome-file>屬性。
<taglib>元素用來(lái)設(shè)置web引用的tag library,例示定義了一個(gè)“/mytaglib”標(biāo)簽庫(kù),它對(duì)應(yīng)的tld文件為:/WEB_INF/mytaglib.tld
<taglib>
<taglib-url>/mytaglib</taglib-url>
<taglib-locationg>/WEB-INF/mytaglib.tld</taglib-location>
</taglib>
<resource-ref>如果web應(yīng)用由Servlet容器管理的某個(gè)JNDI Resource,必須在web.xml中聲明對(duì)這個(gè)JNDI Resource的引用。
<resource-ref>
<description>DB Connection</description> //說(shuō)明
<res-ref-name>jdbc/sampleDB</res-ref-name> //引用資源的JNDI名字
<res-type>javax.sql.DataSource</res-type> //引用資源的類名字
<res-auth>Container</res-auth> //管理引用資源的Manager
</resource-ref>
<security-constraint>用來(lái)為Web應(yīng)用定義安全約束
<security-constraint>
<web-resource-collection>//聲明受保護(hù)的web資源
<web-resource-name>ResourceServlet</web-resource-name>//標(biāo)識(shí)受保護(hù)web資源
<url-pattern>/ResourceServlet</url-pattern>//指定受保護(hù)的URL路徑
<http-method>GET</http-method>//指定受保護(hù)的方法
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>//可以訪問(wèn)受保護(hù)資源的角色
<description>this applies only to admin secrity role</description>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>元素指定當(dāng)Web客戶訪問(wèn)受保護(hù)資源時(shí),系統(tǒng)彈出的登陸對(duì)話框的類型。例示配置了基于表單驗(yàn)證的登陸界面
<login-config>
<auth-method>FORM</auth-method>//BASIC(基本驗(yàn)證法),DIGEST(摘要驗(yàn)證),F(xiàn)ORM(表單驗(yàn)證)
<real-name>設(shè)定安全域的名稱</realname>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
<security-role>指明這個(gè)Web應(yīng)用引用的所有角色名字
<security-role>
<description>描述</description>
<role-name>admin</role-name>
</security-role>