博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ServletContext
阅读量:5809 次
发布时间:2019-06-18

本文共 8140 字,大约阅读时间需要 27 分钟。

得到ServletContext的方式:

public class ServletDemo6 extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {				//得到ServletContext的方式1		ServletContext context = this.getServletConfig().getServletContext();				//得到ServletContext的方式2		context = this.getServletContext();	}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		doGet(request, response);	}}
通过ServletContext实现多个servlet的数据共享:

public class ServletDemo7 extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {				String data = "aaa";				this.getServletContext().setAttribute("data", data);			}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		doGet(request, response);	}}
/** * ServletContext域: * 1.这是一个容器 * 2.ServletContext域这句话说明了这个容器作用范围,也就是应用程序范围 *///用过ServletContext实现ServletDemo7和ServletDemo8的数据共享public class ServletDemo8 extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {				String value = (String)this.getServletContext().getAttribute("data");		System.out.println(value);	}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		doGet(request, response);	}}

获得Web应用的初始化参数:

web.xml配置文件:

data1
xxxx
data2
yyyy
data3
zzzz
Servlet代码:

//获取web应用的初始化参数public class ServletDemo9 extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {				String value =(String)this.getServletContext().getInitParameter("data1");		response.getOutputStream().write(("" + value + "").getBytes());				Enumeration e = this.getServletContext().getInitParameterNames();		while(e.hasMoreElements()){			String name = (String)e.nextElement();			String value1 = (String)this.getInitParameter(name);			System.out.println(name+" = "+value1);		}	}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		doGet(request, response);	}}
实现Servlet转发:

jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>      My JSP '1.jsp' starting page        	  	

<% //在jsp中,application就是ServletContext String data = (String)application.getAttribute("data"); out.write(data); %>

Servlet代码:
//通过ServletContext实现请求转发public class ServletDemo10 extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {				String data = "aaaaaaaaaa";				//把数据带给1.jsp (不能通过context域,要通过request域)		this.getServletContext().setAttribute("data", data);				//转发到1.jsp		RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/1.jsp");		rd.forward(request, response);			}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		doGet(request, response);	}}

利用ServletContext读取资源文件:

db.properties属性文件内容:

url=jdbc:mysql://localhost:3306/testusername=rootpassword=root

Servlet代码:

//通过ServletContext读取资源文件public class ServletDemo11 extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {				test5();		  	}		//通过servletContext的getRealPath得到资源的绝对路径后,再通过传统流读取资源文件	//此方法适用于下载,因为可以得到资源名称	private void test5() throws IOException {				String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");		String filename = path.substring(path.lastIndexOf("\\")+1);				System.out.println("当前读取到的资源名称是:"+filename);				FileInputStream in = new FileInputStream(path);		//java专门用来读取properties文件的对象		Properties props = new Properties();//内部用map保存数据		props.load(in);				System.out.println("当前读取到的资源数据是:");		String url = props.getProperty("url");		String username = props.getProperty("username");		String password = props.getProperty("password");				System.out.println(url);		System.out.println(username);		System.out.println(password);	}		//读取资源文件需要注意的问题:下面传统代码不可行,最好采用servletContext去读	private void test4() throws IOException {				FileInputStream in = new FileInputStream("classes/db.properties");		//java专门用来读取properties文件的对象		Properties props = new Properties();//内部用map保存数据		props.load(in);				String url = props.getProperty("url");		String username = props.getProperty("username");		String password = props.getProperty("password");				System.out.println(url);		System.out.println(username);		System.out.println(password);	}	//db.properties属性文件在Web应用(WebRoot)目录下		private void test3() throws IOException {		InputStream in = this.getServletContext().getResourceAsStream("/db.properties");		//java专门用来读取properties文件的对象		Properties props = new Properties();//内部用map保存数据		props.load(in);				String url = props.getProperty("url");		String username = props.getProperty("username");		String password = props.getProperty("password");				System.out.println(url);		System.out.println(username);		System.out.println(password);	}	//db.properties属性文件在cn.itcast包目录下	private void test2() throws IOException {		InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/db.properties");		//java专门用来读取properties文件的对象		Properties props = new Properties();//内部用map保存数据		props.load(in);				String url = props.getProperty("url");		String username = props.getProperty("username");		String password = props.getProperty("password");				System.out.println(url);		System.out.println(username);		System.out.println(password);	}	//db.properties属性文件在src目录下		private void test1() throws IOException {		InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");		//java专门用来读取properties文件的对象		Properties props = new Properties();//内部用map保存数据		props.load(in);				String url = props.getProperty("url");		String username = props.getProperty("username");		String password = props.getProperty("password");				System.out.println(url);		System.out.println(username);		System.out.println(password);	}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		doGet(request, response);	}}

Servlet调用其他程序,在其他程序中通过类装载器读取资源文件(文件不能太大):

Servlet代码:

//Servlet调用其他程序,在其他程序中如何读取资源文件(通过类装载器,文件不能太大)public class ServletDemo12 extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		UserDao dao = new UserDao();		dao.update();	}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		doGet(request, response);	}}
//如果读取资源文件的程序不是Servlet的话,就只能通过类装载器去读了public class UserDao {		private static Properties dbconfig = new Properties();		//静态代码块	static{		try{			InputStream in = UserDao.class.getClassLoader().getResourceAsStream("db.properties");			dbconfig.load(in);		}		catch(Exception e){			//抛出初始化错误			throw new ExceptionInInitializerError(e);		}			}		public void update(){		System.out.println(dbconfig.getProperty("url"));	}		public void find()throws IOException{				//以下代码虽然可以读取资源文件的数据,但是无法获取更新后的数据,		//因为类装载器只装载一次资源文件到内存中,即使修改了资源文件,这种方法仍然读取内存中的原资源文件数据		//所以此方法适用于数据不会更新的情况//		Properties dbconfig = new Properties();//		InputStream in = UserDao.class.getClassLoader().getResourceAsStream("db.properties");//		dbconfig.load(in);//		System.out.println(dbconfig.getProperty("url"));				//如果要读取更新后的资源文件数据,则先用类装载器得到资源路径,再用传统文件流方式读取资源文件数据		String path = UserDao.class.getClassLoader().getResource("db.properties").getPath();		FileInputStream in = new FileInputStream(path);		Properties dbconfig = new Properties();		dbconfig.load(in);		System.out.println(dbconfig.getProperty("url"));	}		public void delete(){			}}

 

转载于:https://www.cnblogs.com/coderookie0820/archive/2013/04/21/4367501.html

你可能感兴趣的文章
记一次Git异常操作:将多个repository合并到同一repository的同一分支
查看>>
CodeIgniter 3.0 新手捣鼓源码(一) base_url()
查看>>
Chrome 广告屏蔽功能不影响浏览器性能
查看>>
vSphere 6将于2月2日全球同步发表
查看>>
Android状态栏实现沉浸式模式
查看>>
让你的APP实现即时聊天功能
查看>>
iOS 绝对路径和相对路径
查看>>
使用Openfiler搭建ISCSI网络存储
查看>>
应用新安全组 - 每天5分钟玩转 OpenStack(116)
查看>>
4.3. 键盘设置
查看>>
iOS - UIViewController
查看>>
准备 overlay 网络实验环境 - 每天5分钟玩转 Docker 容器技术(49)
查看>>
IntPtr 转 string
查看>>
一文搞懂各种 Docker 网络 - 每天5分钟玩转 Docker 容器技术(72)
查看>>
学生名单
查看>>
(转) 多模态机器翻译
查看>>
【官方文档】Nginx负载均衡学习笔记(三) TCP和UDP负载平衡官方参考文档
查看>>
矩阵常用归一化
查看>>
Oracle常用函数总结
查看>>
【聚能聊有奖话题】Boring隧道掘进机完成首段挖掘,离未来交通还有多远?
查看>>