为什么我login_check.jsp的
request.getRequestDispatcher("login_check.jsp").forward(request, response);
代码出错,要实现第二次免登录,但是第一次可以登录,第二次就出问题
全部代码:
login.jsp
<%@ page language="java" import="java.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!
String name="";
String pwd="";
%>
<%
Cookie [] cookies=request.getCookies();
if(cookies!=null && cookies.length>0){
for(Cookie c:cookies){
if(c.getName().equals("name")){
// System.out.print(name);
name=c.getValue();
}
if(c.getName().equals("pwd")){
pwd=c.getValue();
}
if(c.getName().equals("name")){
System.out.print(name);
request.getRequestDispatcher("login_check.jsp").forward(request, response);
}
}
}
%>
<table border="1" align="center">
<form name="form1" action="login_check.jsp" method="post">
<tr>
<td align="center" colspan="2">用户登陆</td>
</tr>
<tr>
<td>账号:</td>
<td><input name="name" type="text" value="<%=name%>"></td>
</tr>
<tr>
<td>密码:</td>
<td><input name="pwd" type="password" value="<%=pwd%>"></td>
</tr>
<tr>
<td>保存登录</td>
<td><input type="checkbox" name="save" value="yes"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登陆">
<input type="reset" value="重置"></td>
</tr>
</form>
</table>
</body>
</html>
login_check.jsp
<%@ page language="java" import="java.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login_check.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<h1 align="center">登陆操作</h1><hr>
<body>
<%
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
System.out.print(name+"uh");
String save=request.getParameter("save");
String [][] tab={{"iot","iot"},{"admin","admin"}};
if(save!=null&&save.equals("yes")){
Cookie cookie1=new Cookie("name",name);
Cookie cookie2=new Cookie("pwd",pwd);
cookie1.setMaxAge(600*24);
cookie2.setMaxAge(600*24);
response.addCookie(cookie1);
response.addCookie(cookie2);
}
if(name==null || pwd==null){
%>
<h4 align="center">用户名或密码为空!请重新<a href="login.jsp">登录</a>系统!</h4>
<%
}
for(int i=0;i<tab.length;i++){
if(name.equals(tab[i][0])==true && pwd.equals(tab[i][1])==true){
session.setAttribute("name",name);
response.setHeader("refresh", "3;welcome.jsp");
out.print("<h4 align='center'>"+"用户登录成功,三秒后跳转到欢迎页面!"+"</h4>");
out.print("<h4 align='center'>"+"如果没有跳转,请点击"+"<a href='welcome.jsp'>"+"这里!"+"</a></h3>");
break;
}
else if(i==tab.length-1){
out.println("<h4 align='center'>"+"用户名或密码出错!请重新"+"<a href='login.jsp'>"+"登录"+"</a>"+"系统!"+"</h3>");
}
}
%>
<%
if(name==null){
System.out.println("123");
Cookie cookies[]=request.getCookies();
for(int i=0;i<cookies.length;i++)
{
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
}
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
welcome.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'welcome.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<%
int i=0;
if(application.getAttribute("num")==null)
application.setAttribute("num","1");
else{
if(session.isNew()){
String str=application.getAttribute("num").toString();
i=Integer.parseInt(str);
i++;
application.setAttribute("num",i);
}
}
%>
</head>
<body>
<h2 align="center">登陆成功</h2>
<h4 align="center">欢迎<%=session.getAttribute("name") %>光临本系统,您是第<%=application.getAttribute("num") %>位访客!
<% request.setAttribute("name",session.getAttribute("name") ); %>
<a href="over.jsp">注销</a></h4>
</body>
</html>
over.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'over.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<% session.getAttribute("name");
session.invalidate();
Cookie cookies[]=request.getCookies();
for(int i=0;i<cookies.length;i++)
{
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
}
%>
<h4 align="center">您已成功退出本系统,3秒后跳转回首页!</h4>
<%
response.setHeader("refresh", "3;URL=login.jsp");
%>
<h4 align="center">如果没有跳转,请按<a href="login.jsp">这里</a></h4>
</body>
</html>