我在一个登录页面(login,jsp),代码如下:
............
<FORM name="LoginForm" method="POST" action="switch.jsp">
账号:<input name="username" type="text" maxlength="10" size=15>
密码:<input name="password" type="password" maxlength="10" size=15>
<input name="Submit" type="submit" value="登录">
</FORM>
...........
//----------------------------------------------------------------------登录验证:switch.jsp
............................
<%
String Username = userBean.getUsername();
String Password = userBean.getPassword();
int loginResult = DataModule.CheckUser(Username,Password);
if (loginResult==1)
{
%>
<jsp:forward page="welcome.jsp"></jsp:forward>
<% }
else if (loginResult==0)
{
%>
<jsp:forward page="loginerror.jsp"></jsp:forward>
<% }
else
{
%>
<jsp:forward page="fail.jsp"></jsp:forward>
<%
}
%>
..........................
//----------------------------------------------------------------------如果用户输入的账号和密码正确,页面能显示welcome.jsp内容,但此时IE地址栏中却显示为
http://127.0.0.1:8080/myweb/switch.jsp
而不是http://127.0.0.1:8080/myweb/welcome.jsp请问为何?
............
<FORM name="LoginForm" method="POST" action="switch.jsp">
账号:<input name="username" type="text" maxlength="10" size=15>
密码:<input name="password" type="password" maxlength="10" size=15>
<input name="Submit" type="submit" value="登录">
</FORM>
...........
//----------------------------------------------------------------------登录验证:switch.jsp
............................
<%
String Username = userBean.getUsername();
String Password = userBean.getPassword();
int loginResult = DataModule.CheckUser(Username,Password);
if (loginResult==1)
{
%>
<jsp:forward page="welcome.jsp"></jsp:forward>
<% }
else if (loginResult==0)
{
%>
<jsp:forward page="loginerror.jsp"></jsp:forward>
<% }
else
{
%>
<jsp:forward page="fail.jsp"></jsp:forward>
<%
}
%>
..........................
//----------------------------------------------------------------------如果用户输入的账号和密码正确,页面能显示welcome.jsp内容,但此时IE地址栏中却显示为
http://127.0.0.1:8080/myweb/switch.jsp
而不是http://127.0.0.1:8080/myweb/welcome.jsp请问为何?
String Username = userBean.getUsername();
String Password = userBean.getPassword();
int loginResult = DataModule.CheckUser(Username,Password);
if (loginResult==1)
{
response.sendRedirect("welcome.jsp");
}
else if (loginResult==0)
{
response.sendRedirect("loginerror.jsp");
}
else
{
response.sendRedirect("fail.jsp"); }
%>再试试吧
response.sendRedirect:代表重定向。login.jsp提交到switch.jsp服务器处理完请求之后,它会发出信息给用户说,我处理完毕了,你需要再发一个请求到welcome.jsp。因此用户前后共发出了2次HTTP请求。
由此就可以解释此时IE地址栏中却显示为
http://127.0.0.1:8080/myweb/switch.jsp
而不是http://127.0.0.1:8080/myweb/welcome.jsp关于重定向和转发的细节,你可以自己写示例,然后用可以抓取HTTP协议包的软件,看看两种方式的HTTP头有什么不同。这样或许会加深你的理解。