全国统一服务热线:400-633-9193

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

    网络     2018-01-18    1303

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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发送数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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循环打印数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<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>


  分享到:  
0.1920s