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 ...
随机推荐
- Apache下安装配置mod_pagespeed模块,轻松完成网站提速
mod_pagespeed是一个开源的Apache module,它由谷歌开发,通过优化你的网页来减少响应延迟和带宽占用.作用参考ngx_pagespeed功能:http://blog.linuxey ...
- SQL2008-字符转数字CAST和CONVERT
语法 使用CAST: CAST(expression AS data_type) 使用CONVERT: CONVERT(data_type[(length)],expression,[style])例 ...
- unigui多页签UI框架
procedure TMainForm.openForm(Caption, FormClassName: string);var i: integer; sheet: TUniTabSheet;beg ...
- jQuery的遍历方法
1.jQuery中的map使用方法 <!DOCTYPE html> <html> <head> <style>p { color:red; }</ ...
- 【Away3D代码解读】其它一些的记录(持续更新)
查看当前正在使用的AGAL代码可以在程序开始时添加下面的代码,AGAL代码会被trace出来: Debug.active = true; 具体的输出是在MaterialPassBase类的update ...
- 使用hexdump 查看二进制文件
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- Java DESede用C++ Openssl实现
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- postfix反垃圾邮件说明
参考地址:http://guailele.blog.51cto.com/1156442/780223 1.打开 smtp 的认证模块 在/etc/postfix/main.cf文件最后加上: sm ...
- iOS stretchableImageWithLeftCapWidth 图片放大不变形
转载自:http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html - (UIImage *)stretchableImageWithLe ...
- iOS——UIButton响应传参数
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 方法是 ...