jsp 与 servlet 之间传值

jsp -> servlet

1. input

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

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


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



猜你喜欢

转载自blog.csdn.net/qq_41902618/article/details/81040510