1.共享数据
我在这个Servlet中保存的数据,可以在另外一个servlet中拿到
在当前的类中存入username
public class ServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
javax.servlet.ServletContext servletContext = this.getServletContext();//servlet上下文
String s = "apang";
servletContext.setAttribute("username",s);//将一个数据保存到了ServletContext中,名字为username,值为s
}
}
通过getAttribute获取存入的名称
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username = (String) servletContext.getAttribute("username");
resp.ge