请问我这里写的哪里有问题?怎么获取不到cookie呢...
大家好,我是刚刚接触jsp的新手,我有几个问题不太明白,想借此机会一起问问各位大神,谢谢了。1、我看jsp教程中都是pageEncoding="utf-8",为什么我用utf-8的时候经常出现乱码呢?
2、我现在看书的时候学到cookie的地方,但是我怎么也读取不到cookie,麻烦帮我看下我哪儿写错了好么,谢谢。我是想读取名为username的cookie的值....
<check.jsp>界面
<%@ page contentType="text/html;charset=gb2312"%>
<%
String name=request.getParameter("username"); //传递form表单中文本框的值
String pass=request.getParameter("password");
String aaa=request.getMethod(); //传递form的属性值是get还是post
boolean bbb=request.isSecure(); //判断网页是否使用了安全协议
if (name.equals(""))
{
out.println("对不起,请输入用户名!");
}
else
{
out.println("您的用户名是"+name+"<br>");
out.println("您的密码是"+pass+"<br>");
out.println(aaa+"<br>");
out.print(bbb);
Cookie cookie=new Cookie("username",name);
cookie.setMaxAge(-123);
response.addCookie(cookie);
}
%>
<a href="check2.jsp">提交</a>
<check2.jsp>界面
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<title>读取Cookie信息</title>
</head>
<body>
<%
Cookie getcookie[]=request.getCookies();
if (getcookie!=null)
{
for (int i=0;i<getcookie.length;i++)
{
Cookie cookie=Cookie[i];
if (cookie.getName()==("usename"))
{
String name=cookie.getName();
String value=cookie.getValue();
out.print(name+"<br>");
out.print(value+"<br>");
}
else
out.print("cookie名称没找到");
}
}
%>
</body>
</html>
我找了半宿也没找到是哪儿的问题,请大神帮我看下,谢谢,以下是错误提示:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 18 in the jsp file: /check2.jsp
Cookie cannot be resolved to a variable
15: {
16: for (int i=0;i<getcookie.length;i++)
17: {
18: Cookie cookie=Cookie[i];
19: if (cookie.getName()==("usename"))
20: {
21: String name=cookie.getName();
Stacktrace:
org.apache.(DefaultErrorHandler.java:97)
org.apache.(ErrorDispatcher.java:330)
org.apache.(JDTCompiler.java:457)
org.apache.(Compiler.java:374)
org.apache.(Compiler.java:352)
org.apache.(Compiler.java:339)
org.apache.jasper.(JspCompilationContext.java:601)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)