Scope和JSTL、El表达式

本文介绍了Servlet的ServletContext对象,包括其作用、特性及共享全局数据的方法。接着讨论了数据共享范围request、session和servletContext,并强调了使用小范围共享的推荐做法。此外,文章探讨了JSP的基本语法和JSTL/EL的引入,强调JSTL是为了替代JSP中的Java代码,提供更优的编写体验。EL表达式的语法和使用方法也在文中进行了说明,展示了如何从不同Scope中查询和输出数据。最后,提到了JSTL的核心标签库,如c:if、c:forEach等,及其在页面渲染中的作用。
摘要由CSDN通过智能技术生成

Servlet/JSP

ServletContext

Context: 上下文,当前事物存在的前后环境场景。
ServletContext:当前Servlet存在的运行环境。代表Web容器。

在这里插入图片描述

1. 任何获取 ServletContext 的方法获取的对象都是同一个ServletContext对象的引用
1. 调用从 HttpServlet继承的方法获取ServletContext 
	
		ServletContext ctx1 = getServletContext();

2. 调用request对象getServletContext()方法获取ServletContext
	
		ServletContext ctx2 = request.getServletContext();
2. 可以使用ServletContext提供的方法与当前Web容器进行通讯
1. ctx1.getServerInfo() 获取当前容器版本信息
2. ctx1.getRealPath()   获取当前程序的实际路径,硬盘物理位置
	- 使用这个方法可以获取图片等资源的磁盘位置,进而使用IO API进行操作。
3. ctx1.getContextPath() 获取当前应用程序的上下文路径
	- 用于拼接web绝对路径,解决路径问题。
3. ServletContext对象可以绑定共享当前应用的全局数据。
- setAttribute()
- getAttribute()
- removeAttribute()
- 建议在修改期间进行同步(加锁)处理

案例:显示Web容器相关信息:

	/**
	 * 测试ServletContext
	 */
	public class DemoServlet extends HttpServlet {
   
		private static final long serialVersionUID = 1L;
	
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
			//1. 调用从 HttpServlet继承的方法获取
			//   ServletContext 
			ServletContext ctx1 = getServletContext();
			//2. 调用request对象getServletContext()方法
			//   获取ServletContext
			ServletContext ctx2 = request.getServletContext();
			//3. ...
			//“任何”方法获取的ServletContext都是同一个对象
			
			//ServletContext 代表当前Servlet的运行容器
			//显示当前Web容器的信息
			System.out.println(ctx1.getServerInfo());
			//重要:显示当前Web容器的实际路径(磁盘位置)
			System.out.println(ctx1.getRealPath("/")); 
			//重要:显示当前Web程序的上下文路径(应用程序名)
			System.out.println(ctx1.getContextPath()); 
			
			response.setContentType(
					"text/html;charset=UTF-8");
			response.getWriter().print("OK"); 
		}
	
	}

	  <servlet-mapping>
	    <servlet-name>DemoServlet</servlet-name>
	    <url-pattern>/demo</url-pattern>
	  </servlet-mapping>
	  <servlet>
	    <description></description>
	    <display-name>SetValueServlet</display-name>
	    <servlet-name>SetValueServlet</servlet-name>
	    <servlet-class>web.SetValueServlet</servlet-class>
	  </servlet>

案例:利用ServletContext共享数据
在这里插入图片描述
代码:

	/**
	 * 利用ServletContext共享数据
	 */
	public class SetValueServlet extends HttpServlet {
   
		private static final long serialVersionUID = 1L;
	
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
			ServletContext ctx=getServletContext();
			synchronized (ctx) {
   
				ctx.setAttribute("test", "Hello World!");
			}
			response.setContentType(
					"text/html; charset=UTF-8");
			response.getWriter().print(
					"添加数据到ServletContext");
		}
	
	}

	/**
	 * 读取 ServletContext中的数据
	 */
	public class GetValueServlet extends HttpServlet {
   
		private static final long serialVersionUID = 1L;
	       
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
			ServletContext ctx = getServletContext();
			String msg = null;
			synchronized (ctx) {
   
				msg = (String)ctx.getAttribute("test")
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值