一:读取配置文件中的参数信息
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("--------------------------"); EnumerationinitParameterNames = 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根元素下加入下面代码:
3.发布工程,浏览器中输入:http://localhost/myday03/servlet/ContextServlet1 控制台打印结果:url mysql:http://localhost:3306 address this is the context-param's address career coder,enger,ceo
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 { }}