本节目录:

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 列表 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[
<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_数据传输_原的更多相关文章

  1. ALSA声卡10_从零编写之数据传输_学习笔记

    1.引言 (1)应用程序使用声卡的时候,数据流程是:应用程序把数据发送给驱动,驱动把数据发送给硬件声卡,声卡把数据转换成声音数据播放出去. (2)可以使用两种方式发送数据 第一种:app发数据,等驱动 ...

  2. Oracle批量更改所有表的字段取值_类型_原字段名

    CREATE PROCEDURE 存储过程名称 is cursor c_tab is select * from user_tab_columns t r_tab user_tab_columns%r ...

  3. 《数据结构与算法分析:C语言描述_原书第二版》CH3表、栈和队列_reading notes

    表.栈和队列是最简单和最基本的三种数据结构.基本上,每一个有意义的程序都将明晰地至少使用一种这样的数据结构,比如栈在程序中总是要间接地用到,不管你在程序中是否做了声明. 本章学习重点: 理解抽象数据类 ...

  4. 《数据结构与算法分析:C语言描述_原书第二版》CH2算法分析_课后习题_部分解答

    对于一个初学者来说,作者的Solutions Manual把太多的细节留给了读者,这里尽自己的努力给出部分习题的详解: 不当之处,欢迎指正. 1.  按增长率排列下列函数:N,√2,N1.5,N2,N ...

  5. HTML5游戏源码 飞翔的字母 可自定义内容

    相信大家都玩过飞翔的小鸟吧,当然,可能已经有很多人因为这个游戏砸了不少手机.吼吼. 废话不多说,回到主题,源码如下. 博客园上传空间大小有限制,没法上传了,需要打包源码的朋友们请留言邮箱地址.当然还有 ...

  6. 整数压缩编码 ZigZag

    在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...

  7. 反射 实现不同模型相同属性赋值 第二集(automapper)

    前言: 两年前写过一个 反射实现不同模型相同属性赋值 只能简单的实现两个model 相同属性名,相同类型赋值 最近又遇到这个问题,需要对相同属性名或者指定属性名 不同类型(复杂对象,如:List< ...

  8. 仅使用处理单个数字的I/O例程,编写一个过程以输出任意实数(可以是负的)

    题目取自:<数据结构与算法分析:C语言描述_原书第二版>——Mark Allen Weiss   练习1.3 如题. 补充说明:假设仅有的I/O例程只处理单个数字并将其输出到终端,我们将这 ...

  9. 阶乘之和 & 程序运行时间 & 算法分析

    实例:输入n,计算S = 1! + 2! + 3! + 4! + ... + n!的末六位(不含前导0).其中 n ≤ 106. 分析:考虑到数据溢出后程序如下: #include <stdio ...

随机推荐

  1. 跟着Android官网学习Activity

    1.Activity介绍 An Activity is an application component that provides a screen with which users can int ...

  2. define

    define('player',['videoplay'],function(videoplay){ var wrap_player = $('#live_SWF'), obj_player = '' ...

  3. ASP.NET- 合并HTML的表格相同项单元格

    我对控件的依懒是比较少的.有几个控件我比较喜欢用,例如REPEATER显示列表的控件,能提供很大的方便. 使用REPEATER有一个比较方便的功能是通过几句代码就实现了相同项合并单元格合并,这个虽然平 ...

  4. 实现Linux下的ls -l命令

    基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现: /************************************************** ...

  5. 剑指OFFER之合并有序链表(九度OJ1519)

    题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则.(hint: 请务必使用链表.) 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测 ...

  6. ASP.Net Core-TagHelpers

    当我们新建了一个.Net Core类型的Project时,我们会看到页面上有类似于这样的代码: 当我们运行项目,查看源代码会发现,浏览器中的就是Html代: 那么,为什么我们在页面写的代码会转化为ht ...

  7. New Features in C# 3.0, 4.0 and 5.0 (英文差的免入)

    What’s New in C# 3.0   Language Integrated Query(LINQ) - LINQ enables queries to be written in C# pr ...

  8. 【ALearning】第三章 Android基本常见控件

    本章主要介绍主要的寻常较多使用的控件,包含TextView.EditView.ImageView.Button等.本章将介绍相关控件基本属性的使用,为以后章节的进阶学习提供基础.案例中引用的Linea ...

  9. tcp_tw_recycle和tcp_timestamps的文章汇总

        临近年关,人会变得浮躁,期间写的代码可谓乱七八糟.不过出来混始终是要还的,这不最近就发现一个PHP脚本时常连不上服务器. 遇到这类问题,我习惯于先用strace命令跟踪了一下看看: shell ...

  10. valgrind 生成mysqld调用图之 select now()跟踪

    1.mysqld起动方式: 1.mysqld以root用户运行 valgrind --tool=callgrind --separate-threads=yes  --trace-children=y ...