Servlet(4)ServletContext接口 (获取web.xml中所有Servlet共有的信息)

来源:互联网 发布:apache bench post 编辑:程序博客网 时间:2024/05/18 23:57

【0  声明】

下面的:

1 把项目/工程(servlet)发布到到tomcat中启动(若已发布,就重新启动一下)

是为了避免tomcat项目发生不必要的异常


【1 什么是ServletContext?】

运行在Java虚拟机中的每一个Web应用程序都有一个与之相关的Servlet上下文,即ServletContext。

ServletContext提供对应用程序中所有Servlet所共有的各种资源和功能的访问。

ServletContext的API用于设置应用程序中所有Servlet共有的信息。运行于同一服务器的Servlet有时会共享资源,如JSP页面、文件和其他Servlet。

如何设置全局的配置信息?

<web-app>根标签中设置全局的配置信息:

下面的配置与servlet的配置是同级别的!


  <context-param>  <param-name>name</param-name>  <param-value>张三</param-value>  </context-param>  <context-param>  <param-name>地址</param-name>  <param-value>中国</param-value>  </context-param>

如何获取配置信息?

在获取全局的配置信息之前,需要先获取ServletContext对象

1、先获取ServletConfig对象
可以通过ServletConfig对象中的getServletContext()获取ServletContext对象
或者通过继承自GenericServlet对象的getServletContext()方法获取ServletContext对象

2、通过ServletConfig对象中方法获取初始化的配置信息
通过ServletContext对象中的getInitParameter(String name)方法获取初始化配置信息
或者通过ServletContext对象中的getInitParameterNames()方法获取所有的key的枚举类型,然后遍历枚举获取value值


DemoServlet.java完整代码如下:


package com.flying.servlet;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DemoServlet extends HttpServlet {protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {ServletContext context = this.getServletContext();String name = context.getInitParameter("name");String address = context.getInitParameter("address");System.out.println(name+"=="+address);}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}


1 把项目/工程(servlet)发布到到tomcat中启动(若已发布,就重新启动一下)
2 在地址栏中输入:http://localhost:8080/servlet/hello  回车确认

控制台显示:

张三==中国


【2 多个servlet共享数据】

由于一个web应用程序中的所有Servlet共享同一个ServletContxt对象,因此ServletContext中的属性可以被Web应用程序中的所有servlet访问。
ServletContext当做容器使用的时候,把ServletContext称为域(范围)对象。

在ServletContext中分别定义了用于 增加、删除、获取ServletContext属性的3个方法:

1、增加

setAttribute(String name, Object object) 
通过该方法可以将数据以key和value的形式存储到ServletContext中

2、删除

removeAttribute(String name) 
通过该方法可以将配置信息从ServletConfig中删除

3、获取

getAttribute(String name) :

通过key获取value

【示例】
需求:统计网站的访问量,并且响应到浏览器中。
分析:由于多个servlet之间可以实现数据共享,那么就可以设置一个数值类型的值存入ServletContext对象中,每次用户访问一个servlet就给这个值+1,然后响应到页面即可。


java中加入语句:

response.setContentType("text/html;charset=utf-8");

是为了防止浏览器访问网址时出现中文乱码现象

DemoServlet.java完整代码如下:

package com.flying.servlet;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DemoServlet extends HttpServlet {protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");ServletContext context = this.getServletContext();Integer cishu = (Integer) context.getAttribute("cishu");if (cishu==null) {cishu=1;}else {cishu++;}context.setAttribute("cishu", cishu);response.getWriter().print("网站访问量是:"+cishu);}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}

1 把项目/工程(servlet)发布到到tomcat中启动(若已发布,就重新启动一下)
2 在地址栏中输入:http://localhost:8080/servlet/hello  回车确认

浏览器显示:

网站访问量是:1

再每刷新一次,1就会自增1次


若再创建一个DemoServlet02.java

里面的代码和DemoServlet.java的一样(复制记得改一下类名,两个类名不同哦)

再开启浏览器输入DemoServlet02.java所对应的地址假如是http://localhost:8080/servlet/hello2  回车确认


交替访问网址会发现里面显示的访问量是交替增加1次,说明里面的信息是通过ServletContext实现数据共享。


【3 ServletContext读取文件路径】


在web开发中,可能会使用去读取一些项目中的文件,而这些文件的位置也可能会发生改变。
比如在web工程发布到tomcat之后,那么web工程中一些文件的路径就可能改变。

那么如何在web应用去读取到这些文件呢?

读取web工程路径的方法如下:

在web工程中创建a.txt\b.txt\c.txt\d.txt分别放在不同的位置

a.txt   位置:项目下内容:哎哎哎aaa
b.txt 位置:scr/b.txt内容:不不不bbb
c.txt 位置:WEBContent/c.txt内容:猜猜猜ccc
d.txt  位置:WEB-INF/d.txt内容:大大大ddd


getRealPath(String path)
返回的是绝对路径,工程发布后,在电脑中的的实际路径

DemoServlet.java完整代码如下:

package com.flying.servlet;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DemoServlet extends HttpServlet {protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");ServletContext context = this.getServletContext();String apath = context.getRealPath("a.txt");System.out.println(apath);readFile(apath);String cpath = context.getRealPath("c.txt");System.out.println(cpath);readFile(cpath);String bpath = context.getRealPath("WEB-INF/classes/b.txt");System.out.println(bpath);readFile(bpath);String dpath = context.getRealPath("WEB-INF/d.txt");System.out.println(dpath);readFile(dpath);}private void readFile(String cpath) throws FileNotFoundException,IOException {FileInputStream in = new FileInputStream(cpath);InputStreamReader reader = new InputStreamReader(in);BufferedReader br = new BufferedReader(reader);String line = null;while ((line = br.readLine()) != null) {System.out.println(line);}br.close();}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}

1 把项目/工程(servlet)发布到到tomcat中启动(若已发布,就重新启动一下)
2 在地址栏中输入:http://localhost:8080/servlet/hello  回车确认

浏览器显示:(异常信息:找不到a.txt文件)

控制台显示:(异常信息同上)


修改代码如下:

package com.flying.servlet;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DemoServlet extends HttpServlet {protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");ServletContext context = this.getServletContext();String realPath = context.getRealPath(" ");System.out.println(realPath);String cpath = context.getRealPath("c.txt");System.out.println(cpath);readFile(cpath);String bpath = context.getRealPath("WEB-INF/classes/b.txt");System.out.println(bpath);readFile(bpath);String dpath = context.getRealPath("WEB-INF/d.txt");System.out.println(dpath);readFile(dpath);}private void readFile(String cpath) throws FileNotFoundException,IOException {FileInputStream in = new FileInputStream(cpath);InputStreamReader reader = new InputStreamReader(in);BufferedReader br = new BufferedReader(reader);String line = null;while ((line = br.readLine()) != null) {System.out.println(line);}br.close();}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}


1 把项目/工程(servlet)发布到到tomcat中启动(若已发布,就重新启动一下)
2 在地址栏中输入:http://localhost:8080/servlet/hello  回车确认

浏览器显示:(空:什么都不显示)

控制台显示:

D:\Study\apache-tomcat-7.0.69\webapps\day01\ 
D:\Study\apache-tomcat-7.0.69\webapps\day01\c.txt
猜猜猜ccc
D:\Study\apache-tomcat-7.0.69\webapps\day01\WEB-INF\classes\b.txt
不不不bbb
D:\Study\apache-tomcat-7.0.69\webapps\day01\WEB-INF\d.txt
大大大ddd

【解释】a.txt文件放在项目下不能读取,其他3个文件的路径均改为了发布到tomcat后的路径(D:\Study\apache-tomcat-7.0.69\ 为我的tomcat路径)

 
getContextPath()
获取项目的相对路径

DemoServlet.java完整代码如下:

package com.flying.servlet;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DemoServlet extends HttpServlet {protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");ServletContext context = this.getServletContext();String contextPath = context.getContextPath();System.out.println(contextPath);}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}

1 把项目/工程(servlet)发布到到tomcat中启动(若已发布,就重新启动一下)
2 在地址栏中输入:http://localhost:8080/servlet/hello  回车确认

浏览器显示:(空:什么都不显示)

控制台显示:

/servlet


【4 总结】


ServletConfig: Servlet的初始化信息的封装对象,this.getServletConfig()方法为获得当前的Servlet在web.xml重配置的信息。
ServletContext是web应用程序的上下文对象,可以通过该对象设置多个servlet之间的共享数据。

常用方法:
getAttribute(String name):通过key获取value
getAttributeNames():获取所有的key的枚举类型,通过遍历获取对应的value
setAttribute(String name, Object object):设置共享数据
getContextPath():获取相对路径
getRealPath(String path):获取绝对路径
getMimeType(String file):获取文件的MIME类型,一般在下载文件的时候用(在浏览器中用以指定打开不同类型的文件)
 


阅读全文
0 0
原创粉丝点击