java数据传输到云应用_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 baseDAO = new BaseDAO();

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);

${ msg } --%>所有 Cat 列表     [添加 Cat][Cat 列表]

IDNameDescriptionMotherOperation

List catList = (List) request.getAttribute("catList");//将数据catList取出来的命令;存在本页面;然后才有后续的操作;for(Cat cat : catList) {

out.write("

");

out.write("

" + cat.getId() + "");

out.write("

" + cat.getName() + "");

out.write("

" + cat.getDescription() + "");

String motherString= "";

Cat mother=cat.getMother();while(mother != null){if(motherString.trim().length() == 0)

motherString=mother.getName();elsemotherString= mother.getName() + " / " +motherString;

mother=mother.getMother();

}

out.write("

" + motherString + "");

out.write("

");

out.write(" 删除");

out.write(" 修改");

out.write("

");

out.write("

");

}%>

2、jsp如何输入表达数据以及传数据到servlet:一般就是在JSP页面的Form

(1)比如下面这种,Form表示下面的表单,作用范围是

**** 包含的区域

f14fb9b009226407506f0237bc47e53a.png

(2)Form Action=给出了数据转向的方向,可以是servlet程序,也可以是另外一个jsp文件;

比如下面的将表单填写的数据给到servlet程序去处理:

Name:
Mother:

---请选择---

List catList = (List) request.getAttribute("catList");for(Cat cat : catList) {

out.write(" ");

String name=cat.getName();

Cat mother=cat.getMother();while(mother != null){

name= mother.getName() + " / " +name;

mother=mother.getMother();

}

out.write("" + name + "");

out.write("");

}%>

Description:${ cat.description }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值