javascript
JSP中传值事件
在制作網(wǎng)頁時(shí)候,我們用到表單的時(shí)候通常會有action來連接到其他地方:
在表單中還有一個(gè)method屬性:
?
默認(rèn)為get,get是有地址欄的。
?
當(dāng)然你也可以改成post:
?
當(dāng)你換成post的時(shí)候:
?
?
普通傳值代碼:
普通傳值:
先創(chuàng)建一個(gè)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>
接著創(chuàng)建一個(gè)a2.jsp文件
<%
String username = request.getParameter("username");
String password = request.getParameter("username");
%>
歡迎<%=username %>
這里有個(gè)問題:
這是因?yàn)?/strong>
在ok.jsp,已經(jīng)無法取出傳給a2.jsp中的username
?
?
其原理就是:
?
?
把原來的a2.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>
?
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");
}
?
?
?這里還是輸出不了,再把a(bǔ)2.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>
?
?
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("admin")&&password.equals("123456")){
??? //登錄成功
??? //不影響客戶端
??? //response.sendRedirect("ok.jsp");
??? //1、把請求轉(zhuǎn)發(fā)給ok.jsp,不響應(yīng)客戶端,也不繼續(xù)處理客戶端請求
??? //讓ok.jsp對客戶端進(jìn)行響應(yīng)
??? request.getRequestDispatcher("ok.jsp").forward(request, response);
??? //問題,地址欄會發(fā)生改變嗎?
???
???
??? //2、把數(shù)據(jù)也發(fā)一份給ok.jsp
???
}else{
??? //登錄失敗
??? response.sendRedirect("error.jsp");
}
%>
?
?
登錄成功<br>
?
<%
String username = request.getParameter("username");
%>
?
歡迎<%=username%>
?
?
?
登錄成功<br>
${classname}
${param.username}
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/wudashuai/p/9144211.html
總結(jié)
- 上一篇: Android子线程中更新UI的4种方法
- 下一篇: windows安装jenkins及ant