5
52free
Unregistered / Unconfirmed
GUEST, unregistred user!
一个简单的servlet但是总是有错,错误就在***处
就算将其余代码都除去只留一个空循环都不行
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class l1 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
throws IOException
{
boolean prevcookie=false;
Cookie mycookie=null;
Cookie[] cookies=req.getCookies();
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<HTML>");
pw.println("<BODY>");
for (int ctr=0;ctr<cookies.length;ctr++) ** 问题就出在cookies.length上这一点我确认,但为何不能获取cookies的length呢?
{
if (cookies[ctr].getName().equals("PrevLogin"))
{
prevcookie=true;
mycookie=cookies[ctr];
}
}
if (prevcookie)
{
pw.println("You logged Previously on");
pw.println(mycookie.getValue());
Calendar cl=Calendar.getInstance();
Date dt=cl.getTime();
String date=dt.toString();
mycookie.setValue(date);
res.addCookie(mycookie);
prevcookie=false;
}
else
{
pw.println("this is the first time you have logged on to the server");
Calendar cl=Calendar.getInstance();
Date dt=cl.getTime();
String date=dt.toString();
mycookie=new Cookie("PrevLogin",date);
res.addCookie(mycookie);
}
pw.println("Hello World 1");
pw.flush();
pw.println("</BODY>");
pw.println("</HTML>");
}
}
就算将其余代码都除去只留一个空循环都不行
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class l1 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
throws IOException
{
boolean prevcookie=false;
Cookie mycookie=null;
Cookie[] cookies=req.getCookies();
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<HTML>");
pw.println("<BODY>");
for (int ctr=0;ctr<cookies.length;ctr++) ** 问题就出在cookies.length上这一点我确认,但为何不能获取cookies的length呢?
{
if (cookies[ctr].getName().equals("PrevLogin"))
{
prevcookie=true;
mycookie=cookies[ctr];
}
}
if (prevcookie)
{
pw.println("You logged Previously on");
pw.println(mycookie.getValue());
Calendar cl=Calendar.getInstance();
Date dt=cl.getTime();
String date=dt.toString();
mycookie.setValue(date);
res.addCookie(mycookie);
prevcookie=false;
}
else
{
pw.println("this is the first time you have logged on to the server");
Calendar cl=Calendar.getInstance();
Date dt=cl.getTime();
String date=dt.toString();
mycookie=new Cookie("PrevLogin",date);
res.addCookie(mycookie);
}
pw.println("Hello World 1");
pw.flush();
pw.println("</BODY>");
pw.println("</HTML>");
}
}