zoukankan      html  css  js  c++  java
  • ServletContext对象

    自我理解:

      ServletContext类似字节码文件对象,在web创建的时候就自动生成了,并且是唯一的,跟随着项目和服务器共存亡了。通过这个对象,我们可以向里面存数据(键值对),也可以通过别的Servlet来获取这个数据;也可以根据相对(服务器)路径继来获取绝对路径。根据这个信息我们可以在以后创建文件的过程中,将静态资源的文件尽量创建在web-content文件夹下,而项目文件、配置文件创建在src下。不要直接创建在web文件夹下(不会在服务器上生成)。

    一、ServletContext对象:

      ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象内部封装是该web应用的信息,ServletContext对象一个web应用只有一个。

    二、ServletContext对象的生命周期:

    创建:该web应用被加载且服务器开启时创建;

    销毁:web应用被卸载(移除该项目应用)或者服务器关闭。

    三、获得ServletContext对象:

    1、Servlet的init方法中获得ServletConfig 

    ServletContext servletContext = config.getServletContext ()

    2、

    ServletContext  servletContext = this.getservletContext ()

    四、ServletContext的作用:

    1、获得web应用全局的初始化参数;

    2、获得web应用中任何资源的绝对路径:

    (通过服务器的相对路径,得到一个在服务器上的绝对路径)

    String path = context.getRealPath(相对于该web应用的相对地址);
    package com.oracle.web;
    
    import java.io.IOException;
    
    import javax.servlet.ServletConfig;
    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 Servlet extends HttpServlet {
    	
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//获取ServletContext对象
    		ServletContext context=getServletConfig().getServletContext();
    		/*//1.获取ServletConfig对象
    		ServletConfig config=getServletConfig();
    		//2.获取ServletContext对象
    		ServletContext context=config.getServletContext();*/
    		//获取相对路径的绝对路径
    		String patha=context.getRealPath("a.txt");
    		System.out.println(patha);
    		String pathb=context.getRealPath("WEB-INF/b.txt");
    		System.out.println(pathb);
    		String pathc=context.getRealPath("WEB-INF/classes/c.txt");
    		System.out.println(pathc);
    		response.getWriter().write("hello dandan...");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    

      

    3、ServletContext是一个域对象(存储数据的区域):

    ServletContext域对象的作用范围:整个web

    (所有的web资源都可以随意向 ServletContext 域中存取数据,数据可以分享)。

    域对象的通用方法:

    package com.oracle.web;
    
    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 Servlet03 extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//hqServletContextdx
    		ServletContext context=getServletContext();
    		//取值
    		String value=(String)context.getAttribute("name");
    		response.getWriter().write(value);
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }package com.oracle.web;
    
    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 Servlet02 extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//hqServletContextdx
    		ServletContext context=getServletContext();
    		//存值
    		context.setAttribute("name","zhangsan");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    

     登录统计:

    package com.oracle.web;
    
    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;
    
    import com.oracle.service.UserService;
    
    public class LoginServlet extends HttpServlet {
    	private UserService userService=new UserService();
    	@Override
    	public void init() throws ServletException {
    		int num=0;
    		//获取ServletContextdx
    		ServletContext context=getServletContext();
    		context.setAttribute("num", num);
    	}
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//获取用户名和密码
    		String name=request.getParameter("username");
    		System.out.println(name);
    		String pwd=request.getParameter("pwd");
    		//调用service层方法
    		int count=userService.login(name, pwd);
    		if (count>0) {
    			//获取ServletContextdx
    			ServletContext context=getServletContext();
    			int num=(int)context.getAttribute("num");
    			num++;
    			context.setAttribute("num", num);
    			response.getWriter().write("you are the "+num+"success");
    		}else {
    			response.getWriter().write("fail");
    		}
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    

      

  • 相关阅读:
    ajax请求超时
    tp5去重统计某字段的数量
    html本地存储 localStorge
    json、obj转换
    关于数组的合并arr.push() arr.push.apply()
    curl内容
    js 回车键登录
    tp5 前置操作
    STL容器
    c++文件的读写
  • 原文地址:https://www.cnblogs.com/zqy6666/p/12356869.html
Copyright © 2011-2022 走看看