頭大了。今天看的比較少,今天一覺睡到11點,再加上整理《網絡信息對抗》的考試重點,耽誤了java web的學習了!
1、使用<welcome-file-list>元素
歡迎頁面,文件名通常爲index.html
http://www.bhu.edu.cn/
在Tomcat中,如果訪問的URL是目錄,並且沒有特定的Servlet與這個URL模式匹配,那麼它將在該目錄中首先查找index.html文件,如果找不到將查找index.jsp文件
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>home.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
經過上述配置,如果客戶使用目錄訪問該應用程序,Tomcat將在指定的目錄中按<welcome-file>指定的文件的順序查找文件,如果找到則把該文件發送給客戶端。
2、ServletConfing接口
在Servlet初始化時,容器將調用init(ServletConfig)方法,併爲其傳遞一個ServletConfig對象,該對象稱爲Servlet配置對象。
使用該對象可以獲得Servlet初始化參數、Servlet名稱、ServletContext對象等。
如何得到ServletConfig接口對象
在Servlet中要得到ServletConfig接口對象有兩種方法:
(1)使用Servlet 的 getServletConfig()方法
ServletConfig config = getServletConfig();
(2)覆蓋Servlet 的 init(ServletConfig config)方法
格式如下:
public void init(ServletConfig config){
super.init(config); //必須調用超類的init()方法。
this.config = config;
}
ServletConfig接口共定義了下面4個方法:
public String getInitParameter(String name)
public Enumeration getInitParameterNames()
public String getServletName()
public ServletContext getServletContext()
如何聲明Servlet初始化參數
<servlet>
<servlet-name>configDemoServlet</servlet-name>
<servlet-class> com.demo.ConfigDemoServlet
</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>[email protected]</param-value>
</init-param>
<init-param>
<param-name>telephone</param-name>
<param-value>8899123</param-value>
</init-param>
</servlet>
3、ServletContext接口
什麼是ServletContext?
容器在啓動時會加載每個Web應用程序,併爲每個Web應用程序創建一個唯一的javax.servlet.ServletContext實例對象,一般稱爲Servlet上下文對象。
Servlet可以使用上下文對象
獲得Web應用程序的初始化參數
Sservlet容器的版本等信息
被Servlet用來與其他的Servlet共享數據。
4、得到ServletContext引用
在Servlet中可以有兩種方法得到ServletContext引用:
(1)直接調用Servlet的getServletContext()方法,例如:
ServletContext context = getServletContext();
(2)使用getServletConfig()方法得到ServletConfig引用,再調用它的getServletContext()方法,例如:
ServletContext context =
getServletConfig().getServletContext();
5、獲取應用程序的初始化參數
ServletContext也有初始化參數
可以使用ServletContext的下面兩個方法檢索Servlet上下文初始化參數:
public String getInitParameter(String name)
public Enumeration getInitParameterNames()
這些初始化參數是在DD中使用<context-param>元素定義的。
<web-app>
...
<context-param>
<param-name>adminEmail</param-name>
<param-value>[email protected]</param-value>
</context-param>
...
<web-app>
注意: <context-param>元素是<web-app>元素的直接子元素,並不嵌套在某個<servlet>元素中。其中定義的初始化參數是針對整個應用的。
要檢索DD中定義的email參數值,可以使用下面代碼:
public void init(){
ServletContext context = getServletContext();
String email =
context.getInitParameter(“adminEmail");
// 使用email
}
Servlet上下文初始化參數與Servlet的初始化參數是不同的!
Servlet上下文初始化參數是屬於Web應用程序的,可以被Web應用程序的所有的Servlet和JSP頁面訪問。
<context-param>
</context-param>
Servlet初始化參數是屬於定義它們的Servlet的,不能被Web應用程序的其他組件訪問。
<init-param>
</init-param>
6、通過ServletContext對象獲得資源
public URL getResource(String path)
返回由給定路徑指定的資源的URL對象。儘管路徑應該以“/”開頭,但它不是一個絕對路徑,而是相對於該Web應用程序的文檔根目錄。
public InputStream
getResourceAsStream(String path)
如果只想從資源上獲得一個InputStream對象,這是一個簡潔的方法,它等價於getResource(path).openStream()。
public String getRealPath(String path)
返回給定的虛擬路徑的真實路徑。
7、登錄日誌
使用log()方法可以將指定的消息寫到服務器的日誌文件中,格式爲:
public void log(String msg)
參數msg爲寫到日誌文件中的消息。默認情況下將把日誌信息寫到Tomcat安裝目錄的\logs\localhost.2009-03-17.log文件中,文件名中的日期爲寫入日誌的日期。
public void log(String msg,
Throwable throwable)
該方法將msg指定的消息和異常的棧跟蹤信息寫入日誌文件。