关于数据库中数据显示在jsp中

通过调用getServletContext()方法从容器获得了ServletConext,然后创建了一个Map用于保存数据,再将这个Map放置到ServletContext中,在实际开发中,往往是把数据库中的数据放置到ServletContext里。

存储数据:
public void contextInitialized(ServletContextEvent sce)  {
         // TODO Auto-generated method stub
     ServletContext servletContext = sce.getServletContext();
     Map<String, String> countries = new HashMap<String,String>();
     countries.put("ca", "canada");
     countries.put("zn", "china");
     servletContext.setAttribute("countries", countries);
    }

显示在页面:
<ul>
 <c:forEach items="${countries }" var="country">
  <li>${country.value }</li>
 </c:forEach>
</ul>

此段是在监听器部分学习的,但是理论过程应该都是一样的,

猜你喜欢

转载自blog.csdn.net/m943917709/article/details/80202506
今日推荐