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("要跳轉的頁面地址");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM