JSP页面登录识别功能代码报错...

远逝的风 2012-03-13 07:34:31
初学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: }
...全文
156 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
成一粒 2012-03-14
  • 打赏
  • 举报
回复
你将username提交到welcome.jsp页面。但是你没有将username存入到Cookies中啊。怎么能获取到呢?
先将传递过来的username保存到cookies中才能获取到
tao_tao 2012-03-14
  • 打赏
  • 举报
回复

首先你确定 Cookies[]部位空,长度是大于0的吗..

其次你的26行貌似少了个 %> ...

远逝的风 2012-03-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liubag 的回复:]
session 不是可以记录信息么??
[/Quote]
其实刚开始代码是这样的:
<%@ 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>
但是报错仍然和提问一样,所以我就改了一些,还是没用!
远逝的风 2012-03-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 li_chun_tao 的回复:]
首先你确定 Cookies[]部位空,长度是大于0的吗..

其次你的26行貌似少了个 %> ...
[/Quote]
我是这样想的:
Login.jsp页面单负责获取Cookie取出UserName,而Welcom.jsp页面负责将上次访问登陆页面的用户名写到Cookie中并有Response返回给客户端!
  • 打赏
  • 举报
回复
[Quote=引用楼主 tingyuanshusheng 的回复:]
初学JSP,本想自己写代码实现页面下次登录时可以利用Cookie识别用户以及自动登录功能,但是出现了一点问题,学JSP的学长们可否斧正一下?代码如下:

<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ page import="java.util.*" %>
<%
String path = r……
[/Quote]
首先看看Cookie有没有add,如果没有设置Cookie值你怎么遍历?然后不要-1
丙寅 2012-03-14
  • 打赏
  • 举报
回复
session 不是可以记录信息么??
V不乖O 2012-03-13
  • 打赏
  • 举报
回复
for(int i=0;i<Cookies.length;i++){....}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧