jsp获取cookie的问题
我的jsp有一段程序是:<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%><%StringU...
我的jsp有一段程序是:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String UserName = "";
String UserEmail = "";
String LoginDate = "";
Cookie temp = null;
Cookie cookies[] = request.getCookies();
for(int i = 0;i<cookies.length;i++){
temp = cookies[i];
if(temp.getName().equals("Name"))
{
UserName = temp.getValue();
}
else if(temp.getName().equals("Email"))
{
UserEmail = temp.getValue();
}
else if(temp.getName().equals("Date"))
{
LoginDate = temp.getValue();
}
int temLen = LoginDate.length();
boolean FirstLogin;
if(temLen != 0)
{
FirstLogin = true;
}
else
{
FirstLogin = false;
}
}
%>
但是运行时出现了如下错误:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /testLogin.jsp at line 8
5: String LoginDate = "";
6: Cookie temp = null;
7: Cookie cookies[] = request.getCookies();
8: for(int i = 0;i<cookies.length;i++){
9: temp = cookies[i];
10: if(temp.getName().equals("Name"))
11: {
我该如何办呢? 展开
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String UserName = "";
String UserEmail = "";
String LoginDate = "";
Cookie temp = null;
Cookie cookies[] = request.getCookies();
for(int i = 0;i<cookies.length;i++){
temp = cookies[i];
if(temp.getName().equals("Name"))
{
UserName = temp.getValue();
}
else if(temp.getName().equals("Email"))
{
UserEmail = temp.getValue();
}
else if(temp.getName().equals("Date"))
{
LoginDate = temp.getValue();
}
int temLen = LoginDate.length();
boolean FirstLogin;
if(temLen != 0)
{
FirstLogin = true;
}
else
{
FirstLogin = false;
}
}
%>
但是运行时出现了如下错误:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /testLogin.jsp at line 8
5: String LoginDate = "";
6: Cookie temp = null;
7: Cookie cookies[] = request.getCookies();
8: for(int i = 0;i<cookies.length;i++){
9: temp = cookies[i];
10: if(temp.getName().equals("Name"))
11: {
我该如何办呢? 展开
3个回答
推荐于2018-05-14 · 知道合伙人互联网行家
关注
展开全部
在第一行中加上加一行if(cookies!=null)就行
具体如下:
Cookie cookies[]=request.getCookies(); //读出用户硬盘上的Cookie,并将所有的Cookie放到一个cookie对象数组里面
Cookie sCookie=null;
for(int i=0;i<cookies.length-1;i++){ //用一个循环语句遍历刚才建立的Cookie对象数组
sCookie=cookies[i]; //取出数组中的一个Cookie对象
if(sCookie!=null){
if(("cookieName").equals(sCookie.getName())){
pageContext.setAttribute("SavedUserName",sCookie.getValue());
}
}
//保存用户名到cookies
String user_name=request.getParameter("user_name");
if(!"".equals(user_name) && request.getParameter("RmbUser")!=null){
Cookie cookie=new Cookie("usernamecookie",user_name);
cookie.setMaxAge(365*24*60*60); //保存365天
response.addCookie(cookie); //写COOKIE
}
具体如下:
Cookie cookies[]=request.getCookies(); //读出用户硬盘上的Cookie,并将所有的Cookie放到一个cookie对象数组里面
Cookie sCookie=null;
for(int i=0;i<cookies.length-1;i++){ //用一个循环语句遍历刚才建立的Cookie对象数组
sCookie=cookies[i]; //取出数组中的一个Cookie对象
if(sCookie!=null){
if(("cookieName").equals(sCookie.getName())){
pageContext.setAttribute("SavedUserName",sCookie.getValue());
}
}
//保存用户名到cookies
String user_name=request.getParameter("user_name");
if(!"".equals(user_name) && request.getParameter("RmbUser")!=null){
Cookie cookie=new Cookie("usernamecookie",user_name);
cookie.setMaxAge(365*24*60*60); //保存365天
response.addCookie(cookie); //写COOKIE
}
展开全部
7: Cookie cookies[] = request.getCookies();
加一行:
if(cookies!=null)
8: for(int i = 0;i<cookies.length;i++){
9: temp = cookies[i];
10: if(temp.getName().equals("Name"))
11: {
加一行:
if(cookies!=null)
8: for(int i = 0;i<cookies.length;i++){
9: temp = cookies[i];
10: if(temp.getName().equals("Name"))
11: {
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
异常了 cookies可能为空
在8行前加
if(cookies==null){cookies="";}
最好用try catch模式~
在8行前加
if(cookies==null){cookies="";}
最好用try catch模式~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询