JavaWeb_数据传输_原
本节目录:
1、如何从Servlet向JSP传送数据:(setAtrribute和getAtrribute)
2、jsp如何输入表达数据以及传数据到servlet(FormAction去向和InputType输入)
1、如何从Servlet向JSP传送数据:
Servlet中的doget和dopost中设置:(使用request.setAttribute和 request.getRequestDispatcher说明传递的数据和要传递的页面)
//CatServlet的servlet
public class CatServlet extends HttpServlet {
private BaseDAO<Cat> baseDAO = new BaseDAO<Cat>();
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
request.setAttribute("catList", baseDAO.list(" from Cat "));
request.getRequestDispatcher("/listCat.jsp").forward(request, response);
}
}
表示将baseDAO.list(" from Cat ")的数据存到一个名为catList的变量中;
然后跳转到 listCat.jsp 页面,同时将数据catList也传统过来啦。
然后在listCat.jsp中就可以使用catList的数据了(使用request.getAttribute);
<body>
<%-- <h4>${ msg }</h4> --%>
所有 Cat 列表 [
<a href="catServlet?action=initAdd">添加 Cat</a>
][
<a href="catServlet?action=list">Cat 列表</a>
]
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Mother</th>
<th>Operation</th>
</tr>
<%
@SuppressWarnings("unchecked")
List<Cat> catList = (List<Cat>) request.getAttribute("catList");//将数据catList取出来的命令;存在本页面;然后才有后续的操作;
for (Cat cat : catList) {
out.write("<tr>");
out.write(" <td>" + cat.getId() + "</td>");
out.write(" <td>" + cat.getName() + "</td>");
out.write(" <td>" + cat.getDescription() + "</td>");
String motherString = "";
Cat mother = cat.getMother();
while(mother != null){
if(motherString.trim().length() == 0)
motherString = mother.getName();
else
motherString = mother.getName() + " / " + motherString;
mother = mother.getMother();
}
out.write(" <td>" + motherString + "</td>");
out.write(" <td>");
out.write(" <a href=catServlet?action=delete&id=" + cat.getId() + " onclick=\"return confirm('确定删除?'); \">删除</a>");
out.write(" <a href=catServlet?action=edit&id=" + cat.getId() + ">修改</a>");
out.write("</td>");
out.write("</tr>");
}
%>
</table>
</body>
2、jsp如何输入表达数据以及传数据到servlet:一般就是在JSP页面的Form
(1)比如下面这种,Form表示下面的表单,作用范围是<Form>**** </Form>包含的区域
(2)Form Action=给出了数据转向的方向,可以是servlet程序,也可以是另外一个jsp文件;
比如下面的将表单填写的数据给到servlet程序去处理:
<form action="catServlet" method="post">
<input type="hidden" name="action" value="${ param.action == 'initAdd' ? 'add' : 'save' }">
<input type="hidden" name="id" value="${ cat.id }">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="${ cat.name }"/></td>
</tr>
<tr>
<td>Mother:</td>
<td><select name="motherId">
<option value="0">---请选择---</option>
<%
@SuppressWarnings("unchecked")
List<Cat> catList = (List<Cat>) request.getAttribute("catList"); for (Cat cat : catList) {
out.write(" <option value=" + cat.getId() + ">"); String name = cat.getName();
Cat mother = cat.getMother();
while(mother != null){
name = mother.getName() + " / " + name;
mother = mother.getMother();
}
out.write("" + name + "");
out.write("</option>");
}
%>
</select>
<script type="text/javascript">document.getElementsByName('motherId')[0].value = '${ 0 + cat.mother.id }'; </script>
</td>
</tr>
<tr>
<td>Description:</td>
<td><textarea name="description">${ cat.description }</textarea></td>
</tr>
<tr>
<td></td>
<td><input type=submit value="${ param.action == 'initAdd' ? ' 提交 ' : ' 保存 ' }" /></td>
</tr>
</table>
</form> </body>
Form Type的几个属性:
| 值 | 描述 |
|---|---|
| button | 定义可点击按钮(多数情况下,用于通过 JavaScript 启动脚本)。 |
| checkbox | 定义复选框。 |
| file | 定义输入字段和 "浏览"按钮,供文件上传。 |
| hidden | 定义隐藏的输入字段。 |
| image | 定义图像形式的提交按钮。 |
| password | 定义密码字段。该字段中的字符被掩码。 |
| radio | 定义单选按钮。 |
| reset | 定义重置按钮。重置按钮会清除表单中的所有数据。 |
| submit | 定义提交按钮。提交按钮会把表单数据发送到服务器。 |
| text | 定义单行的输入字段,用户可在其中输入文本。默认宽度为 20 个字符。 |
见html测试网址:http://www.w3school.com.cn/tags/att_input_type.asp
Hidden的效果见下面:

option下拉菜单的效果:
JavaWeb_数据传输_原的更多相关文章
- ALSA声卡10_从零编写之数据传输_学习笔记
1.引言 (1)应用程序使用声卡的时候,数据流程是:应用程序把数据发送给驱动,驱动把数据发送给硬件声卡,声卡把数据转换成声音数据播放出去. (2)可以使用两种方式发送数据 第一种:app发数据,等驱动 ...
- Oracle批量更改所有表的字段取值_类型_原字段名
CREATE PROCEDURE 存储过程名称 is cursor c_tab is select * from user_tab_columns t r_tab user_tab_columns%r ...
- 《数据结构与算法分析:C语言描述_原书第二版》CH3表、栈和队列_reading notes
表.栈和队列是最简单和最基本的三种数据结构.基本上,每一个有意义的程序都将明晰地至少使用一种这样的数据结构,比如栈在程序中总是要间接地用到,不管你在程序中是否做了声明. 本章学习重点: 理解抽象数据类 ...
- 《数据结构与算法分析:C语言描述_原书第二版》CH2算法分析_课后习题_部分解答
对于一个初学者来说,作者的Solutions Manual把太多的细节留给了读者,这里尽自己的努力给出部分习题的详解: 不当之处,欢迎指正. 1. 按增长率排列下列函数:N,√2,N1.5,N2,N ...
- HTML5游戏源码 飞翔的字母 可自定义内容
相信大家都玩过飞翔的小鸟吧,当然,可能已经有很多人因为这个游戏砸了不少手机.吼吼. 废话不多说,回到主题,源码如下. 博客园上传空间大小有限制,没法上传了,需要打包源码的朋友们请留言邮箱地址.当然还有 ...
- 整数压缩编码 ZigZag
在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...
- 反射 实现不同模型相同属性赋值 第二集(automapper)
前言: 两年前写过一个 反射实现不同模型相同属性赋值 只能简单的实现两个model 相同属性名,相同类型赋值 最近又遇到这个问题,需要对相同属性名或者指定属性名 不同类型(复杂对象,如:List< ...
- 仅使用处理单个数字的I/O例程,编写一个过程以输出任意实数(可以是负的)
题目取自:<数据结构与算法分析:C语言描述_原书第二版>——Mark Allen Weiss 练习1.3 如题. 补充说明:假设仅有的I/O例程只处理单个数字并将其输出到终端,我们将这 ...
- 阶乘之和 & 程序运行时间 & 算法分析
实例:输入n,计算S = 1! + 2! + 3! + 4! + ... + n!的末六位(不含前导0).其中 n ≤ 106. 分析:考虑到数据溢出后程序如下: #include <stdio ...
随机推荐
- Android实例-拍摄和分享照片、分享文本(XE8+小米2)
结果: 1.分享文本不好使,原因不明.有大神了解的,请M我,在此十分感谢. 2.如果想支持图片编辑,将Action事件的Editable改为True. 相关资料: 官网地址:http://docwik ...
- iOS: ARC和非ARC下使用Block属性的问题
1. Block的声明和线程安全 Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的,可以参考之前的文章(iOS: 非AR ...
- android最快的模拟器
https://www.genymotion.com/ genymotion Genymotion是一套完整的工具,它提供了Android虚拟环境.它简直就是开发者.测试人员.推销者甚至是游戏玩家的福 ...
- .NET设计模式(1):开篇
转载:http://terrylee.cnblogs.com/archive/2005/12/09/293465.html .NET设计模式开篇 --.NET设计模式系列之一 Terrylee,200 ...
- (二)Bootstrap CSS 概览
在这一章中,我们将讲解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML 5 文档类型(Doctype) Bootstrap 使用了一些 H ...
- 教你50招提升ASP.NET性能(十三):精选技巧集合
(19)A selection of tips 招数19: 精选技巧集合 Including height and width in <img /> tags will allow you ...
- Codeforces Round #268 (Div. 1) A. 24 Game 构造
A. 24 Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/468/problem/A D ...
- Win8 安装Oracle 10g 提示“程序异常终止”的解决方案
这段时间在做DRP,需要安装Oracle 10g的数据库,我的系统是win8企业版,安装Oracle 10g的时候,选择“高级安装”,提示“程序异常终止”,下面是几个解决方案,跟大家分享一下. 错误提 ...
- 让DataGridView显示行号
http://www.cnblogs.com/JuneZhang/archive/2011/11/21/2257630.html 为了表示行号,我们可以在DataGridView的RowP ...
- 安卓模拟器Android SDK Manager 无法获取SDK列表的解决办法
1.打开运行Android SDK Manager ,Tool菜单,选择Options,打开设置菜单,勾选“Force https://...sources to be fetched using h ...