欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-28 22:51:49
一,在div中插入要显示的数据! 1,以下是原网页插入数据代码:
    ...

一,在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 使用request为页面添加静态数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。