Fork me on GitHub

JSP 页面传值

 使用session会话传值并重定向页面

 //得到用户提交的值
 String name = request.getParameter("username");
 String pwd = request.getParameter("password");
 //创建HttpSessio对象
 HttpSession s = request.getSession();
 //将值绑定在session会话中
 s.setAttribute("Sname",name);
 s.setAttribute("Spwd",pwd);
 //重定向
 response.sendRedirect("yinyu.jsp");

在yinyu.jsp页面中得到值

//使用session会话取值
String username = (String)session.getAttribute("Sname");
 String password = (String)session.getAttribute("Spwd");
或者
${Sname}; ${Spwd};

 使用request方式传值并页面转发

 //得到用户提交的值
 String name = request.getParameter("username");
 String pwd = request.getParameter("password");
 //将值绑定在request中
 request.setAttribute("Rname",name);
 request.setAttribute("Rpwd",pwd);
 //页面转发
 RequestDispatcher rd = request.getRequestDispatcher("yinyu.jsp");
 rd.forward(request,response);

在yinyu.jsp页面中得到值

 //使用request协助相关取值
String username = (String)request.getAttribute("Rname");
 String password = (String)request.getAttribute("Rpwd");
或者

${Sname};
 ${Spwd};

 

setAttribute() 设置绑定属性值setAttribute("自定义命名",要绑定的引用);
getAttribute() 取出得到属性值getAttribute("setAttribute中的自定义命名");
sendRedirect() 重定向跳转页面sendRedirect("要跳转的页面地址");
posted @ 2015-02-03 02:03  森林森  阅读(3275)  评论(3编辑  收藏  举报