普通传值:
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}