存入数据
package cn.lishimin.day03;
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 Eservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext appction = this.getServletContext(); //创建ServletContext 对像
appction.setAttribute("name", "李四"); //存入键值对,或也叫属性名,和值
}}
取出数据
package cn.lishimin.day03;
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 Dservlet extends HttpServlet {
private static final long serialVersionUID = 1L;@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/*
* 创建servletContext
*/
ServletContext application = this.getServletContext();
String name = (String)application.getAttribute("name"); //取出属性,String 强转成字符串
System.out.println(name);
}