jsp 与 servlet 之间传值

3 篇文章 0 订阅

jsp -> servlet

1. input

jsp:   定义name  <input type="text" name="cardnum" id="cardnum"> 

servlet:     通过获取 name 的 值     request.getParameter(*name*);

2019/06/27补充:有一些值可以放在隐藏域中,作为局部变量还是很好用的!

2.session

jsp:   在jsp页面设置值 session.setAttribute("定义传值的名称",所传的值)

servlet:   在servlet 中 通过 request.getSession().getAttribute("参数名称");

    注:1. 接收值如果是 String 则需要在前面加(String)进行强转

          2. 若接受值为 int 则在后面加.toString() 先转化为 String ,然后在下方在通过 Integer.parseInt(**)转换为int类型。

3 . ( 转载)

b、url传值

比如这里的 <a>标签的 href属性与 <form>标签的 action属性的值 "JspServlet?action=toServlet",在 servlet同样用 request.getParameter("action")获取;

 

servlet-> jsp

 

1. 转发 (个人偏爱)

servlet:

jsp:  

 

 

    注:1. 接收值如果是 String 则需要在前面加(String)进行强转

          2. 若接受值为 int 则在后面加.toString() 先转化为 String ,然后在下方在通过 Integer.parseInt(**)转换为int类型。

2. 重定向

servlet:

//重定向传值,通过Session传值
request.getSession().setAttribute("值的名称", 值);
//重定向界面

response.sendRedirect("/AccountSys/pages/ShowDetails.jsp");

jsp:  重定向 通过Session 获得值

String path=(String) request.getSession().getAttribute("path");

注: 转发和重定向的地址问题。

 

3. session

request.getSession().setAttribute("值的名称", 值);

String path=(String) request.getSession().getAttribute("path");

注: 注意session 可以jsp 与 servlet之间相互传值 ,注意它们的区别。

 

 

标记(转载): 重定向与转发的区别:

https://blog.csdn.net/lijun102542/article/details/78518733

 

 

  • 18
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值