欢迎来到代码驿站!

JSP代码

当前位置:首页 > 软件编程 > JSP代码

jsp 使用request为页面添加静态数据的实例

时间:2020-11-12 08:07:19|栏目:JSP代码|点击:

一,在div中插入要显示的数据!

1,以下是原网页插入数据代码:

<div>
 <ul class="list-content">
<li><a href="">aaaaaaaaaaaaaaaaaaa</a><span>[06-29]</span></li>
<li><a href="">aaaaaaaaaaaaaaaaaaa</a><span>[06-29]</span></li>
<li><a href="">aaaaaaaaaaaaaaaaaaa.</a><span>[06-29]</span></li>
<li><a href="">aaaaaaaaaaaaaaaaaaa</a><span>[06-29]</span></li>
<li><a href="">aaaaaaaaaaaaaaaaaaa.</a><span>[06-29]</span></li>
<ul>
<div>
<div>
<ul class="list-content">
<li><a href="">aaaaaaaaaaaaaaaaaaa</a><span>[06-29]</span></li>
 <li><a href="">aaaaaaaaaaaaaaaaaaa</a><span>[06-29]</span></li>
<li><a href="">aaaaaaaaaaaaaaaaaaa...</a><span>[06-29]</span></li>
<li><a href="">aaaaaaaaaaaaaaaaaaa</a><span>[06-29]</span></li>
<li><a href="">aaaaaaaaaaaaaaaaaaa...</a><span>[06-29]</span></li>
<ul>
</div>

2,在src中创建servlet类:

使用request.setAttribute发送数据

public static ArrayList<String> list;
 static{
 list=new ArrayList<String>();
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee"); 
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee"); 
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee");
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee"); 
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee"); 
   public static ArrayList<String> listdc;
 static{
 listdc=new ArrayList<String>();
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee");
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee");
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee");
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee");
 listdc.add("dcrewrwe3eeeeeeeeeeeeeeeee");
 }
 public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  request.setCharacterEncoding("UTF-8");
  response.setContentType("text/html");
  ArrayList<String>list=getData();
  
  request.setAttribute("new", list);
  request.setAttribute("dc", listdc);
  request.getRequestDispatcher("/page/default.jsp").forward(request, response);
 }

3,在需要插入数据的地方加入jsp脚本:

使用request.getAttribute接收数据

通过for循环打印数据

<div>
     <ul class="list-content">
     
      <%
      ArrayList<String> list =(ArrayList<String>)request.getAttribute("new");
      if(list!=null)
       for(int i=0;i<list.size();i++){
      %>
       <li>
      <a href="" style=" rel="external nofollow" rel="external nofollow" width:644px">
        <% out.print(list.get(i)); %>
        </a><span>
        [06-29]
        </span>
       </li>
      <% }%>
     </ul>
    </div>
    <div>
     <ul class="list-content">
     <%ArrayList<String> listdc=(ArrayList<String>)request.getAttribute("dc");
      if(listdc!=null)
      for(int i=0;i<listdc.size();i++){
     %>
     <li>
     <a href="" style=" rel="external nofollow" rel="external nofollow" width:644px">
     <% out.print(listdc.get(i)); %>
     </a><span>
     [06-29]</span>
     </li>
     <% } %>
     </ul>
    </div>

上一篇:JSP Spring中Druid连接池配置详解

栏    目:JSP代码

下一篇:jsp中使用javabean实例介绍

本文标题:jsp 使用request为页面添加静态数据的实例

本文地址:http://www.codeinn.net/misctech/21670.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有