jsp3
普通傳值:
a1.jsp
<form action="a2.jsp" method="post">
用戶名:<input type="text" name="username" id="username"><br>
密碼:<input type="password" name="password"><br>
<input type="submit">
</form>
a2.jsp
<%
String username = request.getParameter("username");
String password = request.getParameter("username");
%>
歡迎<%=username %>
<form action="a2.jsp" method="post">
用戶名:<input type="text" name="username" id="username"><br>
密碼:<input type="password" name="password"><br>
<input type="submit">
</form>
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("admin")&&password.equals("123456")){
//登錄成功
response.sendRedirect("ok.jsp");
}else{
//登錄失敗
response.sendRedirect("error.jsp");
}
%>
在ok.jsp,已經無法取出傳給a2.jsp中的username
<form action="a2.jsp" method="post">
用戶名:<input type="text" name="username" id="username"><br>
密碼:<input type="password" name="password"><br>
<input type="submit">
</form>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("admin")&&password.equals("123456")){
//登錄成功
//不影響客戶端
//response.sendRedirect("ok.jsp");
//1、把請求轉發給ok.jsp,不響應客戶端,也不繼續處理客戶端請求
//讓ok.jsp對客戶端進行響應
request.getRequestDispatcher("ok.jsp").forward(request, response);
//問題,地址欄會發生改變嗎?
不會
//2、把數據也發一份給ok.jsp
}else{
//登錄失敗
response.sendRedirect("error.jsp");
}
%>
從頁面上獲取
<%
String username = request.getParameter("username");
%>
歡迎<%=username%>
在a2.jsp設置屬性
request.setAttribute("classname", "S145班"); //key(關鍵字),value
在ok.jsp中獲取屬性值
<%
String classname = request.getAttribute("classname").toString();
%>
<%=classname %>
EL表達式
${classname} //獲取屬性的值
${param.username} //從頁面上獲取參數的值
${param.password}
jsp3