内容简介:下面小编就为大家分享一篇jsp 使用request为页面添加静态数据的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
一,在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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 理解实例方法、类方法、静态方法
- 静态方法、实例化方法与线程安全
- c# – 是否可以通过静态方法访问实例变量?
- 静态库遇到静态库
- 全局变量,静态全局变量,局部变量,静态局部变量
- Android NDK秘籍--编译静态库、调用静态库
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
UNIX 时间戳转换
UNIX 时间戳转换
RGB HSV 转换
RGB HSV 互转工具