一:讀取配置文件中的參數信息
1.新建servlet文件ContextServlet1,代碼爲:
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ContextServlet1 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String url1 = this.getServletConfig().getServletContext().getInitParameter("url");
String url2 = this.getServletContext().getInitParameter("url");//這兩行代碼效果相同
System.out.println("url1:" + url1);
System.out.println("url2:" + url2);
System.out.println("--------------------------");
Enumeration<String> initParameterNames = this.getServletContext().getInitParameterNames();
while(initParameterNames.hasMoreElements()){
String nextElement = initParameterNames.nextElement();
String elementValue = this.getServletContext().getInitParameter(nextElement);
System.out.println("elementsValue:" + elementValue);
}
}
}
2.配置web.xml:在web.xml根元素下加入下面代碼:
<context-param>
<param-name>url</param-name>
<param-value>mysql:http://localhost:3306</param-value>
</context-param>
<context-param>
<param-name>address</param-name>
<param-value>this is the context-param's address</param-value>
</context-param>
<context-param>
<param-name>career</param-name>
<param-value>coder,enger,ceo</param-value>
</context-param>
3.發佈工程,瀏覽器中輸入:http://localhost/myday03/servlet/ContextServlet1
控制檯打印結果:
url1:mysql:http://localhost:3306
url2:mysql:http://localhost:3306
--------------------------
elementsValue:coder,enger,ceo
elementsValue:mysql:http://localhost:3306
elementsValue:this is the context-param's address
二:程序寫入contextServlet參數,並取出<統計網頁訪問次數>:
1.新建servlet文件ContextServlet2代碼:import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//servletContext,session,request. jsp page.
//servletContext是一個域對象,那麼有兩個概念。
// 1.域就肯定是一個容器。(可以放任何對象)。
// 2.域是有一個作用範圍的。對於servletContext對象來說,它的作用範圍就是整個web應用範圍。
//
public class ContextServlet2 extends HttpServlet {
/*
* 首先通過init方法給其設置一個初始化的值。這個值設置了之後就是一個全局的。
* 通過servletContext對象設置的數據都是全局的。
* servletContext就當前的web應用。(non-Javadoc)
* @see javax.servlet.GenericServlet#init()
*/
public void init() throws ServletException {
int timesValue = 0;
//代碼中一般是鍵值對,鍵在前,值在後,
this.getServletContext().setAttribute("timesName", timesValue);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*爲了統計網站的訪問次數,首先將之前的全局的times給獲得。
* 每次來自客戶端的請求都將該全局的times+1
* 調用getAttribute("name")就可以獲得全局的times.
*
*/
int timesValue = (Integer) this.getServletContext().getAttribute("timesName");
timesValue++;
//將變化後的times重新添加到容器中,
this.getServletContext().setAttribute("timesName", timesValue);
System.out.println("本網站已被訪問:" + timesValue + "次!");
}
}
2.瀏覽器中輸入:http://localhost/myday03/servlet/ContextServlet2
控制檯輸出:
本網站已被訪問:1次!
本網站已被訪問:2次!//第二次刷新時得到的結果。每刷新一次就得到一個新的結果。
三:將上述在控制檯中輸出的結果改爲在瀏覽器中輸出:
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
public class RefreshServelet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//refresh頭實現定時刷新到某個頁面.
//一般可用作定時刷新,
//應用:股票,聊天室,
// response.setHeader("Refresh", "2;url=http://www.baidu.com");
response.setHeader("Refresh", "0");
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("每秒刷新一次:"+ new Date(System.currentTimeMillis()).toLocaleString());
// response.getWriter().print("每秒刷新一次:"+ DateFormat.format();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}