JSP页面登录识别功能代码报错...
初学JSP,本想自己写代码实现页面下次登录时可以利用Cookie识别用户以及自动登录功能,但是出现了一点问题,学JSP的学长们可否斧正一下?代码如下:<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ page import="java.util.*" %>
<%
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>下次登录识别功能测试</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>
<%!String UserName1; %>
<%Cookie Cookies[]=request.getCookies();//获取所有的Cookie
for(int i=0;i<Cookies.length-1;i++){
if(Cookies[i].getName().equals("UserName")){UserName1=Cookies[i].getValue();}
else UserName1="";
}
%>
<body>
<form action="Welcom.jsp" method="post">
<table width="200" align="center">
<tr>
<td align="left">用户名:</td>
<td><input type="text" name="UserName"}/><%=UserName1 %></td>
</tr>
<tr>
<td align="left">密码:</td>
<td align="left"><input type="password" name="Password"/></td>
</tr>
<tr>
<td><input type="submit" name="submit1" value="登录"/></td>
<td><input type="reset" name="reset1" value="取消"/></td>
</tr>
</table>
</form>
</body>
</html>
报错如下:
org.apache.jasper.JasperException: An exception occurred processing JSP page /ResponseTest/Login.jsp at line 27
24: </head>
25: <%!String UserName1; %>
26: <%Cookie Cookies[]=request.getCookies();//获取所有的Cookie
27: for(int i=0;i<Cookies.length-1;i++){
28: if(Cookies[i].getName().equals("UserName")){UserName1=Cookies[i].getValue();}
29: else UserName1="";
30: }
--------------------编程问答-------------------- for(int i=0;i<Cookies.length;i++){....} --------------------编程问答--------------------
首先你确定 Cookies[]部位空,长度是大于0的吗..
其次你的26行貌似少了个 %> ...
--------------------编程问答-------------------- 你将username提交到welcome.jsp页面。但是你没有将username存入到Cookies中啊。怎么能获取到呢?
先将传递过来的username保存到cookies中才能获取到 --------------------编程问答-------------------- session 不是可以记录信息么?? --------------------编程问答--------------------
首先看看Cookie有没有add,如果没有设置Cookie值你怎么遍历?然后不要-1 --------------------编程问答--------------------
我是这样想的:
Login.jsp页面单负责获取Cookie取出UserName,而Welcom.jsp页面负责将上次访问登陆页面的用户名写到Cookie中并有Response返回给客户端!
--------------------编程问答--------------------
其实刚开始代码是这样的:
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ page import="java.util.*,javax.servlet.http.Cookie" %>
<%
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>下次登录识别功能测试</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>
<%!String UserName1; %>
<%Cookie Cookies[]=request.getCookies();//获取所有的Cookie
for(int i=0;i<Cookies.length;i++){
if(Cookies[i].getName().equals("UserName")){UserName1=Cookies[i].getValue();}
else UserName1="";
}
%>
<body>
<form action="Welcom.jsp" method="post">
<table width="200" align="center">
<tr>
<td align="left">用户名:</td>
<td><input type="text" name="UserName"}/><%=UserName1 %></td>
</tr>
<tr>
<td align="left">密码:</td>
<td align="left"><input type="password" name="Password"/></td>
</tr>
<tr>
<td><input type="submit" name="submit1" value="登录"/></td>
<td><input type="reset" name="reset1" value="取消"/></td>
</tr>
</table>
</form>
</body>
</html>
但是报错仍然和提问一样,所以我就改了一些,还是没用!
补充:Java , Web 开发